Computer Science, asked by dishant4, 1 year ago

wap to check a no. is spy no. or not in java

Answers

Answered by siddhartharao77
5
Try this :

      int a,b,c,d;       
        b = 1;     
       c = 0;     

   Scanner e = new Scanner(System.in); 
     
  System.out.println("Enter number : "); 
 
    a = e.nextInt();   
    while(a != 0)     
   {     
        d = a % 10;   
        b = b * d;         
        c = c + d;         
        a = a / 10;     
   }       
 if(c == b)   
    {     
System.out.println("Number is Spy");   
    }      
  else       
   {     
      System.out.println("Number is not spy");   
     } }}
Answered by muakanshakya
9
\textbf{\underline{\underline{Program\:to\:check\:whether\:a\:no\:is\:spy\:or\:not\:}}}

import java.util.*;

class spy

{

public static void main(String args []) //main

function

{

Scanner br = new Scanner(System.in);// making

object 'br' of scanner to help in input data

in num, sum = 0, prod = 1, digit = 0, temp = 0;

System.out.println("Enter a number:");

num = br.nextInt();.

// this statement inputs a number in variable 'num'

temp = num;

//copy or assign value in variable 'num' to another variable 'temp'

while(temp>0)

{

digit = temp %10;

sum = sum+ digit;

prod = prod *digit;

temp = temp / 10;

}

// while loop closes here

if (sum==prod)
//checking sum of digits is equal to product of digits or not

System.out.println(num+" is a spy number");

else

System.out.println(num+"is not a spy number");

}. // end of the main function

} // end of the class

//The following is a computer generated output:

Enter a number: 1124

1124 I a spy number
Similar questions