A number is called SUPERSPY if the sum of the digits equals the number of the digits.
Example:
1021 is a SUPERSPY number
1+0+2+1 = 4
Number of digits = 4
Let's start the code of determining if a number is SUPERSPY or not.
Below is the code for determing if a number is SUPERSPY.
import java.util.Scanner;
class SUPERSPY
{
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int n = sc.nextInt();
int sum = 0,temp = n, no = 0;
while(temp!=0)
{
sum+=temp%10;
temp/=10;
no++;
}
if(sum == no)
{
System.out.println("It is a SUPERSPY number");
}
else
{
System.out.println("It is not a SUPERSPY number");
}
}
}
class SUPERSPY
{
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int n = sc.nextInt();
int sum = 0,temp = n, no = 0;
while(temp!=0)
{
sum+=temp%10;
temp/=10;
no++;
}
if(sum == no)
{
System.out.println("It is a SUPERSPY number");
}
else
{
System.out.println("It is not a SUPERSPY number");
}
}
}
Demonstration:
2. A class namely SUPERSPY have been defined.
3. A main method have been defined.
4. Object of Scanner class have been created.
5. A Number has been demanded from user by giving a message.
6. To accept and store the number, a variable namely n has been defined of int data type
7. Then, three different variables namely
a) sum - This variable will store the sum of digits. First, it is initialized with 0.
b) temp - This variable will hold the number given by user on temporary basis. Actually, entire operation will be done with temp variable. temp = n means the value stored in n namely variable is getting assigned to temp. It means that throughout the entire operation, original value stored in n namely variable will remain safe.
c) no - This variable will count the total number of digits in a number given by user.
Now, let us see the working:
while(temp!=0)
This loop will continue its iteration until value of temp doesn't become equals to 0. Say for example, user gave 128. Here, first 8 will be extracted, then 2 and at last, 1 will get extracted as it is in index 0. Refer to the below diagram for clear understanding.
sum+ = temp%10;
Here, we need to understand few things.
First, individual digits from given number is getting extracted using temp%10.
As in case of 128, the first extracted number using temp%10 will be 8.
Then, this number will get added to the variable sum.
Similarly, second number using temp%10 will be 2 and it will also be added to sum.
At last, third number using temp%10 will be 1 and it will also be added to sum.
Now, sum is containing 8+2+1 = 11temp/=10;
In previous operation, we have seen that the first extracted number using temp%10 was 8. Immediately, after this operation, temp/=10; will be executed which means 128 is now going to be divided by 10 resulting to 12.8. This operation will permanently delete this 8 from temp variable. so that in next iteration, 8 doesn't gets added to the sum variable again. Like this, it will keep on deleting the digits from temp variable.
no++;
In each iteration, value of no will get incremented by 1. In case of 128, the loop will iterate for 3 times and every time, value of no will increment by 1 and therefore, final value of no will become 3.
Here is the final outcome:
sum is now containing sum of digits i.e. 1+2+8 = 11
no variable is now containing count of digits i..e 128 = 3
Finally, with the conditional statement using if-else, it is determined if sum is equal to no or not as it can be seen in the program. If sum is equal to no, then it is displaying that the number is a SUPERSPY number. Otherwise, it is displaying that the number is not a SUPERSPY number.
Now, write this program and test it with various numbers and increase your knowledge.
Happy Coding.......