Q) 3자리 정수를 요구하고 각 자리를 동시에 보여주는 프로그램을 작성하시오. 예를 들어, 38145가 주어진다면 다음과 같이 출력되도록 하시오.


 First digit: 3
 Second digit: 8
 Third digit: 1
 Fourth digit: 4
 Fifth digit: 5

 

Ex3_6.txt

public class Ex3_6 {

	public static void main(String[] args) {

		int number = 38145;

		int digit10000 = number / 10000; // 38145/10000
		int digit1000 = number % 10000 / 1000; // 8145/1000
		int digit100 = number % 10000 % 1000 / 100; // 145/100
		int digit10 = number % 10000 % 1000 % 100 / 10; // 45/10
		int digit1 = number % 10000 % 1000 % 100 % 10; // 5

		System.out.println("First: " + digit10000);
		System.out.println("Second: " + digit1000);
		System.out.println("Third: " + digit100);
		System.out.println("Fourth: " + digit10);
		System.out.println("Fifth: " + digit1);
	}
}

posted by 쪼재