Help needed
Please write a program to check whether a number is special number or not .
100 points
Answers
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");
}
}
}
→ The special number is a number whose sum of factorial of each digit is equal to the number .
→ We will use digit extraction to extract the digits .
→ Then inside the while-loop we run another loop for calculating the factorial .
→ We will add the factorial .
→ Then we will check whether the sum is equal to the number or not .
→ If it is equal , print that the number is special otherwise it is not special .
→ A copy variable is required in the coding .
import java.util.*;
class special
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n,i,d,sum=0,fact=0;
System.out.print(“Enter an integer:”);
n=sc.nextInt();
i=n;
while(i>0)
{
d=i%10;
i/=10;
for(int a=1;a<=d;a++)
{
fact*=a;
}
sum+=fact;
fact=0
}
if(sum==n)
System.out.println(“Special Number”);
else
System.out.println(“Not a Special
Number”);
}
HOPE IT HELPS YOU !!