Define a class to accept a number and check whether it is a SUPERSPY number or not. A number is called SUPERSPY if the sum of the digits equals the number of the digits.
Example 1:Input: 1021
Output: SUPERSPY number [SUM OF THE DIGITS = 1 + 0 + 2 + 1 = 4, NUMBER OF DIGITS = 4]
Example 2:
Input: 125
Output: Not a SUPERSPY number [1 + 2 + 5 is not equal to 3]
Solution:
import java.util.Scanner;
public class SuperSpy
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number:");
int n = sc.nextInt(); //Accept the number from the user
int c=0,s=0; //for counter and sum
while(n>0)
{
int d=n%10; //extracting each digits
s=s+d; //adding the digits
c=c+1; //counting the digits
n=n/10; //reducing the number
}
if(s==c)
System.out.println("Superspy Number");
else
System.out.println("Non-Superspy Number");
}
}