Computer Science, asked by RiyaWani, 1 year ago

write a program to check if the number is a special no or not using bufferedReader

Answers

Answered by samayitabhattacharya
6
I'll show u with scanner class too

Attachments:
Answered by Rajdeep11111
6
Hello there!

What is a Special Number?

A Special number is a number which is equal to the sum of the factorial of its digits.

Consider the number 145.
1! + 4! + 5! = 1 + 24 + 120 = 145

Here's the code:

import java.io.*;

public class SpecialNumber

{

    public static void main (String args[]) throws IOException

    {

        BufferedReader buff = new BufferedReader (new InputStreamReader (System.in));

        int n, r, s = 0;

        System.out.println("Enter the number: ");

        n = Integer.parseInt(buff.readLine());

        int p = n;

        SpecialNumber obj = new SpecialNumber();

        while (n > 0)

        {

            r = n % 10;

            s = s + obj.fact(r);

            n = n / 10;

        }

        if (s == p)

       

            System.out.println("It is a special number");

       

        else

           System.out.println("It is not a special number");

    }

   

    public int fact (int n)

    {

        int i = 2, fn = 1;

        for (i = 2; i <= n; i++)

        {

            fn = fn * i;

        }

        return fn;

    }

}


Attachments:
Similar questions