A neon number is a number where the sum of digits of the square of the number is equal to the number.
Example: 0,1 and 9
Say for example, if you have 9 then square of 9 is 81.
8+1 = 9
As it can be seen that sum of digits of the square of the number is equal to the number. Hence, it is a NEON number.
Let's see the Java program to determine if a number is NEON Number or not.
import java.util.Scanner;
class NEON
{
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, square;
square = n*n;
while(square!=0)
{
sum+=square%10;
square/=10;
}
if(sum == n)
{
System.out.println("It is a NEON number");
}
else
{
System.out.println("It is not a NEON number");
}
}
}
Demonstration:
1. First, Scanner class is imported.
2. A class namely NEON 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 in the variable square. First, it is initialized with 0.
b) temp - This variable will hold the number given by user on temporary basis.
c) square - This variable will store the square of the number given by user.
Now, let us see the working:
square = n*n;
This above operation will evaluate the square of the number given by user. Say for example, user gave 9. Here, first square of 9 will be evaluated using this statement.
while(square!=0)
This loop will continue its iteration until value of square doesn't become equals to 0.
Square of 9 is 81. So, first 1 will be extracted, then 8 will get extracted as it is in index 0. Refer to the below diagram for clear understanding.
sum+ = square%10;
Here, we need to understand few things.
First, individual digits from number is getting extracted using square%10.
As in case of 81, the first extracted number using square%10 will be 1.
Then, this number will get added to the variable sum.
Similarly, second number using square%10 will be 8 and it will also be added to sum.
Now, sum is containing 1+8 = 9
square/=10;
In previous operation, we have seen that the first extracted number using square%10 was 1. Immediately, after this operation, square/=10; will be executed which means 81 is now going to be divided by 10 resulting to 8.1. This operation will permanently delete this 1 from square variable. so that in next iteration, 1 doesn't gets added to the sum variable again. Like this, it will keep on deleting the digits from square variable.
Here is the final outcome:
sum is now containing sum of square of number i.e. 8+1 = 9
n variable is containing the number i..e 9
Finally, with the conditional statement using if-else, it is determined if sum is equal to n or not as it can be seen in the program. If sum is equal to n, then it is displaying that the number is a NEON number. Otherwise, it is displaying that the number is not a NEON number.
Now, write this program and test it with various numbers and increase your knowledge.
No comments:
Post a Comment