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 number whose sum of digits of square of the number is equal to number itself.

Also read – armstrong number in java

For example: number 9 is a neon number.

Square of number 9 : 9 * 9 = 81

Sum of square : 8 + 1 = 9

Now let’s see example on how to check if given number is neon number or not.

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 – java program to find largest of three numbers