Computer Applications
Write a Java program to input a number. Calculate and display the factorial of each digit.
Sample Input: 365
Sample Output:
Factorial of 5 = 120
Factorial of 6 = 720
Factorial of 3 = 6
Java
Java Iterative Stmts
72 Likes
Answer
import java.util.Scanner;
public class KboatDigitFactorial
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int n = in.nextInt();
while (n != 0) {
int d = n % 10;
n /= 10;
int f = 1;
for (int i = 1; i <= d; i++) {
f *= i;
}
System.out.println("Factorial of " + d + " = " + f);
}
}
}
Output

Answered By
26 Likes
Related Questions
Rewrite the following do while program segment using for:
x = 10; y = 20; do { x++; y++; } while (x<=20); System.out.println(x * y );
To execute a loop 10 times, which of the following is correct?
- for (int i=11;i<=30;i+=2)
- for (int i=11;i<=30;i+=3)
- for (int i=11;i<20;i++)
- for (int i=11;i<=21;i++)
How many times will the following loop execute?
int a = 5; while (a > 0) { System.out.println(a-- + 2); if (a % 2 == 0) break; }
Define a class to accept a number and check whether it is a SUPERSPY number or not. A number is called SUPERSPY if the sum of the digits equals the number of the digits.
Example1:
Input: 1021 output: SUPERSPY number [SUM OF THE DIGITS = 1+0+2+1 = 4, NUMBER OF DIGITS = 4 ]
Example2:
Input: 125 output: Not an SUPERSPY number [1+2+5 is not equal to 3]