KnowledgeBoat Logo
|

Computer Applications

Write a program to input a number and print whether the number is a special number or not. (A number is said to be a special number, if the sum of the factorial of the digits of the number is same as the original number).

Java

Java Iterative Stmts

14 Likes

Answer

import java.util.Scanner;

public class KboatSpecialNum
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter number: ");
        int num = in.nextInt();

        int t = num;
        int sum = 0, fact;
        while (t != 0) {
            int d = t % 10;
            fact = 1;
            for (int i = 1; i <= d; i++) 
                fact *= i;
            sum += fact;
            t /= 10;
        }

        if (sum == num)
            System.out.println(num + " is a special number");
        else
            System.out.println(num + " is not a special number");

    }
}

Output

BlueJ output of Write a program to input a number and print whether the number is a special number or not. (A number is said to be a special number, if the sum of the factorial of the digits of the number is same as the original number).

Answered By

4 Likes


Related Questions