Write a program in JAVA to check whether a number is special number or not !
Give output also please ...........
Answers
What is a special number ?
A special number is a number whose sum of factorials of each of the digit is equal to the number itself .
=============================================================
CODE :
import java.util.*;
class special_number
{
public void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number to check whether it is special number or not");
int n=sc.nextInt();
int d=0;
int cpy=n;
int s=0;
while(n>0)
{
d=n%10;
int f=1;
for(int i=1;i<=d;i++)
{
f=f*i;
}
s=s+f;
n=n/10;
}
if(s==cpy)
{
System.out.println("Special number");
}
else
{
System.out.println("Not a special number");
}
}
}
========================================================
OUTPUT :
Enter a number to check whether it is special number or not
145
Special number
Hope it helps :)
_____________________________________________________________________
Answer:
import java.util.*;
class special_number
{
public void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number to check whether it is special number or not");
int n=sc.nextInt();
int d=0;
int cpy=n;
int s=0;
while(n>0)
{
d=n%10;
int f=1;
for(int i=1;i<=d;i++)
{
f=f*i;
}
s=s+f;
n=n/10;
}
if(s==cpy)
{
System.out.println("Special number");
}
else
{
System.out.println("Not a special number");
}
}
}
Explanation: