Java program to check neon number

Let’s learn java program to check neon number.

Java program to check neon number

Neon number program in java is one of the frequently asked interview question. A neon number is a positive number whose sum of digits of its square is equal to number itself.

For example: number 9 is a neon number.

Square of number 9 : 9 * 9 = 81

Sum of square : 8 + 1 = 9

In the above example digit 9 square is equal to sum of square. Hence 9 is a neon number.

In the below program first user enters a number to check neon number or not using nextInt() method of Scanner class. Store this number in integer variable ‘number’.

Now calculate square of user entered number and store in integer variable in ‘square’. After declaring and initializing integer variable sum = 0, using while loop loop until variable ‘square’ is not equal to zero.

Finally compare user entered number with integer variable ‘sum’. If it is equal then the number is neon number else it is not a neon number. Now let’s see neon number program in java.

import java.util.Scanner;
public class NeonNumberInJava
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter number to check neon number or not: ");
      int number = sc.nextInt();
      int square = number * number;
      int sum = 0;
      while(square != 0)
      {
         int n = square % 10;
         sum = sum + n;
         square = square / 10;
      }
      if(sum == number)
      {
         System.out.println(number + " is a neon number.");
      }
      else
      {
         System.out.println(number + " is not a neon number.");
      }
      sc.close();
   }
}


Output:

Please enter number to check neon number or not:
1
1 is a neon number.

Please enter number to check neon number or not:
9
9 is a neon number.

Please enter number to check neon number or not:
5
5 is not a neon number.


Also read – comments in java