Using Constructor to initialize a variable and Check whether the Number is spy number or not Number
If sum of sum of the digits and product of the digits are equal
Input: 59
Sum of digits: 14
Product of digits: 45
Output: 59 is a spy number
import java.util.*;
class Ab
{
int n,m;
Ab(int x)
{
n=x;
}
public void show()
{
m= sp (n);
if(m==n)
System.out.println(“Spy Number:”);
else
System.out.println(“Not Spy Number:”);
}
private int sp(int n)
{
int s=0,p=1,d,i;
for(i=n;i>0;i=i/10)
{
d=i%10;
s=s+d;
p=p*d;
}
return (s+p);
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n;
System.out.println("Enter the number:");
n=sc.nextInt();
Ab obj=new Ab(n);
obj.show();
}
}
No comments:
Post a Comment