Neon Number in Java

In this section, we will discuss what is the neon numbers and also create a Java program to check if the given number is neon or not. Also, we will find all the neon numbers between a specified range.

Neon Number

A positive integer whose sum of digits of its square is equal to the number itself is called a neon number.

Example of Neon Number

Let’s take an example and check 9 and 45 are neon numbers or not.

Neon Number in Java

Steps to Find Neon Number

  1. Read an integer from the user or initialize a number (n) to check.
  2. Calculate the square of the given number (n) and store it in variable sq.
  3. Find the sum of the digits of the square (sq) and store the sum in the variable (sum).
  4. Compare the given number n with If both are equal, the given number is a neon number, else, not a neon number.

Let’s implement the above steps in a Java program.

Neon Number Java Program

The logic is very simple. First, we have to calculate the square of the given number. After that, calculate the sum of digits in the square.

Let’s create a Java program that checks if the given number is neon or not.

NeonNumberExample1.java

  1. import java.util.*;  
  2. public class NeonNumberExample1  
  3. {  
  4. public static void main(String args[])  
  5. {  
  6. int sum = 0, n;      
  7. Scanner sc = new Scanner(System.in);  
  8. System.out.print(“Enter the number to check: “);  
  9. //raeds an integer from the user  
  10. n = sc.nextInt();  
  11. //calculate square of the given number  
  12. int square = n * n;  
  13. //loop executes until the condition becomes false  
  14. while(square != 0)  
  15. {  
  16. //find the last digit of the square      
  17. int digit = square % 10;  
  18. //adds digits to the variable sum  
  19. sum = sum + digit;  
  20. //removes the last digit of the variable square  
  21. square = square / 10;  
  22. }  
  23. //compares the given number (n) with sum  
  24. if(n == sum)  
  25. System.out.println(n + ” is a Neon Number.”);  
  26. else  
  27. System.out.println(n + ” is not a Neon Number.”);  
  28. }  
  29. }  

Output:

Enter the number to check: 9
9 is a Neon Number.

Let’s find all the neon numbers between a specified range.

NeonNumberExample2.java

  1. import java.io.*;   
  2. public class NeonNumberExample2   
  3. {   
  4. //function to check Neon Number   
  5. static boolean isNeon(int num)   
  6. {   
  7. //calculate the square of the given number  
  8. int sq = num * num;   
  9. //stores the sum of digits  
  10. int sumOfdigits = 0;  
  11. //executes until the condition becomes false  
  12. while (sq != 0)   
  13. {   
  14. //finds the last digit of the variable sq and adds it to the variable sum     
  15. sumOfdigits = sumOfdigits + sq % 10;   
  16. //removes the last dogit of the variable sq  
  17. sq = sq / 10;   
  18. }   
  19. //compares the sumOgDigits with num and returns the boolean value accordingly  
  20. return (sumOfdigits == num);   
  21. }   
  22. //driver Code   
  23. public static void main(String args[])  
  24. {   
  25. System.out.print(“Neon Numbers between the given range are: “);      
  26. // 0 is the lowe limit and 100000 is the upper limit  
  27. for (int i = 0; i <= 100000; i++)   
  28. //calling the user-defined number  
  29. if (isNeon(i))   
  30. //prints all the neon numbers between given range  
  31. System.out.print(i+” “);           
  32. }   
  33. }   

Output:

Neon Numbers between the given range are: 0 1 9

We get only three neon numbers between 0 to 100000. So, there is a probability that 1 Trillion also includes 0, 1, 9 as the neon numbers.

Leave a Comment