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).

Example:
145 is a special number, because 1! + 4! + 5! = 1 + 24 + 120 = 145.
(Where ! stands for factorial of the number and the factorial value of a number is the product of all integers from 1 to that number, example 5! = 1 * 2 * 3 * 4 * 5 = 120)

Java

Java Iterative Stmts

ICSE 2011

95 Likes

Answer

import java.util.Scanner;

public class KboatSpecialNum
{
    public static int fact(int y) {
        int f = 1;
        for (int i = 1; i <= y; i++) {
            f *= i;
        }
        return f;
    }

    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;
        while (t != 0) {
            int d = t % 10;
            sum += fact(d);
            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). Example: 145 is a special number, because 1! + 4! + 5! = 1 + 24 + 120 = 145. (Where ! stands for factorial of the number and the factorial value of a number is the product of all integers from 1 to that number, example 5! = 1 * 2 * 3 * 4 * 5 = 120)

Answered By

33 Likes


Related Questions