top of page
Writer's pictureSatyam Gupta

Special Number in JAVA

Updated: Mar 11, 2021

Special Number

Special Number : A number is said to be Special Number if the sum of factorial of its digits is equal to the number itself. 


Example : 145 is a Special Number.

Sum of factorial of digits : 1! + 4!+ 5! = 1 + 24 + 120 = 145 


Input     : 145

Output  : It is a Special Number


Input     : 220

Output  : It is not a Special Number

 

Algorithm and Program given below :-

Algorithm :-

  • Input a number

  • Store it in another variable

  • Now using while loop extract last digit of the number

  • Than by using for loop store the factorial of the digit

  • Than in another variable store the sum of factorial

  • initialize sum variable =1 again

  • update the value of number by storing it without its last digit

  • After the condition satifies completely

  • Compare the original number with the sum of factorial of digits

  • If both are equal than display "It is a special number"

  • Else display "It is not a special number

 

Program :-


import java.io.*; import java.util.*; class Special {     public static void main(String args[])     {         Scanner in = new Scanner(System.in);         int n,p,i,num,sum=1,s=0;         System.out.println("Enter Number ");         n= in.nextInt(); //Input Number         p=n; // Storing the input number         while(n>0)         {             num=n%10; // extracting last digit of the number             for(i=num;i>=1;i--)             {                 sum=sum*i; //storing factorial of the extracted digit             }             s= s+sum; // storing the sum of factorials             sum=1; //again initializing sum=1             n=n/10; // updating value of the number by storing it without its last digit.         }         if(s==p)// comparing sum of factorial with original number         {             System.out.println("It is a Special Number : "+p);         }         else         {             System.out.println("It is not a Special Number : "+p);         }     } // end of main method } // end of class 

 

Note : The program is written in the easiest way possible so try to understand it. If you face any problem in understanding this program than feel free to contact/comment.


Source Code : Click here

Program Video : Watch now




All the Best :)

Keep Learning :)

810 views0 comments

Recent Posts

See All

Comments


Post: Blog2_Post
bottom of page