How to check Spy number in java

Checking Spy number

    Steps to do this:-
1.take sum=0,prod=1,r, n (here we have to initialize sum and prod , r is for remainder and n is for use input)

2.start while loop (n>0) then do 
r=n%10; //it will store the last number
sum=sum+r; //it will add the number with sum
prod=prod*r; // it will multiply the number
n=n/10;// it will eliminate the last number

3. check if(sum==prod) the print " spy number !" else print "not a spy number !" 

4.save and run

The Source code

import java.util.Scanner;
class spy
{
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        int n,r,sum=0,prod=1;
        System.out.println("enter the number");
        n=s.nextInt();
        while(n>0)
        {
            r=n%10;
            sum=sum+r;
            prod=prod*r;
            n=n/10;
        }
        if(sum==prod)
        {
            System.out.println("this is a spy number !");
        }
        else
        {
            System.out.println("this is not a spy number !");
        }
    }
}

OUTPUT 

WATCH MY VIDEO 
 

Comments

Popular Posts