KnowledgeBoat Logo

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

55 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

BlueJ output of 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

Answered By

19 Likes