Saturday, 31 October 2020

Write a menu driven program : i) To check and display a number is Spy number or not, ii) To check and display a number is Special number or not.

[A number is said to be spy number , if sum of all the digits is equals the product of its digits] Example: Input: 1124 Sum of digits = 1+1+2+4=8 Product of digits=1*1*2*4=8 So 1124 is a Spy number ]
[A number is said to be special if the sum of the factorial of the digits is same as the original number.e.g.,145 is a special number 5! + 4! +1! =120+24+1=145. ]  



import java.util.*;
public class Special_Spy  

    public static void main(String[] args) 
    { 
        Scanner in = new Scanner(System.in);
        
        System.out.println("Enter 1 for spy number, 2 for special number");
        System.out.println("Enter your choice");
        String choice = in.next(); 
        
 
        
        switch(choice) 
        { 
            case "1": 
                System.out.print("Enter Number: ");
        int num = in.nextInt();
        int orgNum = num;        
        int digit, sum = 0, prod = 1;        
        while (num > 0) 
       {  
            digit = num % 10;          
            sum += digit;
            prod *= digit;
            num /= 10;
        }
        
        if (sum == prod)
            System.out.println(orgNum + " is Spy Number");
        else
            System.out.println(orgNum + " is not Spy Number");
            
                        break; 
                        case "2": 
                        System.out.println("Enter the number to be checked.");
        int num1=in.nextInt();
        int sum1=0;
        int temp=num1;
        while(temp!=0)
        {
            int a=temp%10;int fact=1;
            for(int i=1;i<=a;i++)
            {
                fact=fact*i;
            }
            sum1=sum1+fact;
            temp=temp/10;
        }
        if(sum1==num1)
        {
            System.out.println(num1+" is a Special Number.");
        }
        else
        {
            System.out.println(num1+" is not a Special Number.");
        }
                            break;
                            default: 
                            System.out.println("no match"); 
        } 
    } 






Output:

Enter 1 for spy number, 2 for special number
Enter your choice
2
Enter the number to be checked.
145
145 is a Special Number.
Enter 1 for spy number, 2 for special number
Enter your choice
1
Enter Number: 132
132 is Spy Number

No comments:

Post a Comment