a) Write a program to accept a three digit number and display all the digits by
arithmetical operators.
Answers
Answered by
0
Answer:Program
import java.util.Scanner;
public class DisplayDigits {
public static void main(String[ ] args) {
System.out.print("Enter a number - ");
int number = new Scanner(System.in).nextInt( );
System.out.println("Digits - ");
for (int i = number; i != 0; i /= 10)
System.out.println(i % 10);
}
}
Algorithm
Accepting the number using Scanner class.
Using ( % 10 ) to obtain the last digit and ( / 10 ) to remove the last digit.
Printing last digit obtained after each iteration and removing the last digit until the number becomes zero.
Hence, all digits printed on separate lines using arithmetic operators.
Explanation:
Similar questions