Write a program that inputs a five-digit integer, spearates the integer into its digits and prints them seperated by three spaces each. [Hint: Use the ineger division and remainder operators.]

Respuesta :

Explanation:

The source code and a sample output have been attached to this response.

The code has been written in Java and it contains comments explaining important parts of the code.

A few things that are worth noting are in the for loop used in the code;

The loop goes from i = 4 to i = 0

When i = 4;

=> (int) Math.pow(10, i) = (int) Math.pow(10, 4) = 10000

Then the fiveDigit is divided by 10000. Since this is an integer division, the first digit of the 5-digit number will be stored in digit which is then printed to the console.

Next, the remainder is calculated and stored in fiveDigit

When i = 3;

=> (int) Math.pow(10, i) = (int) Math.pow(10, 3) = 1000

Then the fiveDigit (which is the remainder when i = 4) is divided by 1000. Since this is an integer division, the second digit of the 5-digit number will be stored in digit which is then printed to the console.

Next, the remainder is calculated and stored in fiveDigit

When i = 2;

(int) Math.pow(10, i) = (int) Math.pow(10, 2) = 100

Then the fiveDigit (which is the remainder when i = 3) is divided by 100. Since this is an integer division, the third digit of the 5-digit number will be stored in digit which is then printed to the console.

Next, the remainder is calculated and stored in fiveDigit

When i = 1;

(int) Math.pow(10, i) = (int) Math.pow(10, 1) = 10

Then the fiveDigit (which is the remainder when i = 2) is divided by 100. Since this is an integer division, the fourth digit of the 5-digit number will be stored in digit which is then printed to the console.

Next, the remainder is calculated and stored in fiveDigit

When i = 0;

(int) Math.pow(10, i) = (int) Math.pow(10, 0) = 1

Then the fiveDigit (which is the remainder when i = 1) is divided by 1000. Since this is an integer division, the fifth digit of the 5-digit number will be stored in digit which is then printed to the console.

Next, the remainder is calculated and stored in fiveDigit and then the program ends.

Ver imagen stigawithfun
Ver imagen stigawithfun
ACCESS MORE