7
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.
Example1:
Show SolutionHide Solution↓
Input: 1021
output: SUPERSPY number [SUM OF THE DIGITS = 1+0+2+1 = 4,
NUMBER OF DIGITS = 4 ]
Example2:
Input: 125
output: Not an SUPERSPY number [1+2+5 is not equal to 3]
import java.util.Scanner;
public class KboatSuperSpy
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = in.nextInt();
int sum = 0;
int dc = 0;
int orgNum = num;
while (num > 0) {
int d = num % 10;
sum += d;
dc++;
num /= 10;
}
if (sum == dc) {
System.out.println(orgNum + " is a SUPERSPY number");
}
else {
System.out.println(orgNum + " is not a SUPERSPY number");
}
}
}
