Computer Science, asked by Ashvi16, 1 year ago

WAP to accept a number using scanner class method and check whether the number is a special number or not, if the number is special then print the message number is special otherwise number is not special.


QGP: What do you mean by a special number?
ritikaraj967: first explain special number as its mentioned in the question
Ashvi16: A no. is said to be a special no. When its individual digits factorial sum is equal to the no. Itself.
QGP: Okay, I'm writing the program
QGP: Should I type help text?
QGP: Oh, now I can't answer till one answer is deleted
QGP: I will still keep the program ready
Ashvi16: K am waiting

Answers

Answered by ritikaraj967
3
System.out,print("enter a number");
int n=sc.nextInt();
int F=1,i,c,s=0,M=n;
int d=n%10;
for(i=n;i!=0;i=i/10)
{
for(c=1;c<=d;c++)
{
F=F*c;
s=s+F;
}
if(s==M)
System.out.println("special number");
}

ritikaraj967: just before if-statement put one more curly bracket
ritikaraj967: and after if-statement to display not a special number put an else statement
Answered by QGP
9
import java.util.*;
public class SPECIAL_NUMBER
{
     public static void main(String[] args)
     {
         Scanner sc = new Scanner(System.in);
         int m,n,i,d,f=1,sum=0;
         
         System.out.print("Enter a number: ");
         n = sc.nextInt();
         m=n;
         
         while(m!=0)
         {
             d = m%10;
             
             for(i=1;i<=d;i++)
             {
                 f=f*i;
             }
  
          sum = sum + f;
          f=1;
          m=m/10;
         }

     if (sum==n)
     {
       System.out.println(n+" is a special number.");
     }
     else
     {
        System.out.println(n+" is not a special number.");
     }
 }
}


QGP: Just copy-paste it to your IDE
QGP: I had typed in my IDE, the checked it. Its running.
QGP: The formatting here isn't compatible with what I actually typed
QGP: Never mind. I retyped it here.
QGP: Completely working. I fixed some minor bugs, and its working now
Similar questions