Java Program to Check Neon Number

If the sum of digits of the square of the number is equal to the same number then the number is called Neon number. Here we will develop a program to find the neon number using Java.

Example:- 9
Square of 9 = 92= 81
Sum of the digits of the square = 8+1= 9
So, 9 is a Neon Number.

Another Example:- 1
Square of 1= 12= 1
Sum of the digits of the square = 1
S0, 1 is a Neon Number.

Another Example:- 6
Square of 6 = 62 = 36
Sum of the digits of the square = 3+6 = 9
Here, 6 is not equal to 9.
So, 6 is not a Neon number.

Procedure to develop Java method to check neon number,

  1. Take a number
  2. Declare the variable sum and initialize it with 0
  3. Find the square value of the number and store it in a variable “square”
  4. Now, traverse all the digits of the square
    • Find its last digit of the square
    • Add the last digit into the variable sum
    • Remove the last digit of the square
  5. Repeat 4th step until the square becomes 0
  6. Compare sum and number. If both are equal then the number is neon number.

Java method to check Neon number

public static boolean isNeon(int number) {

   // declare variables
   int sum = 0, lastDigit = 0, square = 0;

   // calculate square value of the number
   square = number * number;

   // traverse all the digits of square
   while(square != 0) {

      // find last digit
      lastDigit = square % 10;

      // add last digit to sum
      sum += lastDigit;

      // remove last digit
      square /= 10;
   }

   // compare sum and number
   if(sum == number) 
      return true; // neon number
   return false; // not a neon number
}

In this method, we can use for loop instead of while loop. Using for loop the number of lines will be reduced but beginners may feel complicated.

public static boolean isNeon(int number) {

   // declare variables
   int sum = 0;

   // traverse all digits of square
   for(int i=(number*number); i!=0;i/=10) 
      sum += i%10;

   // compare sum and number
   return (sum == number);
}

Also see:- Special numberMagic numberArmstrong numberPerfect numberEvil NumberSpy NumberSunny number in Java

Java program to check given number is neon number or not

Based on the above-discussed points we will develop a Java program to check the given number is a Neon number or not.

import java.util.Scanner;

public class NeonNumber {

  public static boolean isNeon(int number) {
     // declare variables
     int sum = 0;

     // traverse all digits of square of number
     for(int i= (number*number); i!=0; i/=10) 
       sum += i%10;

     // compare sum and number
     return (sum == number);
  }

  public static void main(String[] args) {

     // declare variables
     int number = 0;
     boolean result = false;

     // create Scanner class object
     Scanner scan = new Scanner(System.in);

     // read inputs
     System.out.print("Enter an integer number:: ");
     number = scan.nextInt();

     // check number is Neon number or not
     result = isNeon(number);
     if(result == true)
        System.out.println(number+
              " is a neon number");
     else
      System.out.println(number+
                " is not a neon number");

     // close Scanner class object
     scan.close();
  }
}

The output for different test-case:-

Enter an integer number:: 9
9 is a neon number

Enter an integer number:: 10
10 is not a neon number

Java program to find the Neon number in a given range

Using loops we can write a Java program to find all the Neon numbers which belong to a given range. We will take the min and max value of the Range and then check all the numbers.

import java.util.Scanner;

public class NeonNumber {

  public static boolean isNeon(int number) {

     // declare variables
     int sum = 0;

     // traverse all digits of square of number
     for(int i= (number*number); i!=0; i/=10) 
        sum += i%10;

     // compare sum and number
     return (sum == number);
  }

  public static void main(String[] args) {

     // declare variables
     int minRange = 0, maxRange = 0;

     // create Scanner class object
     Scanner scan = new Scanner(System.in);

     // read inputs
     System.out.print("Enter min value of range:: ");
     minRange = scan.nextInt();
     System.out.print("Enter max value of range:: ");
     maxRange = scan.nextInt();

     // check number 
     System.out.println("Neon numbers from "+
            minRange+" to "+ maxRange+" are:: ");

     for(int i=minRange; i<=maxRange; i++) {
        if(isNeon(i))
            System.out.print(i+" ");
     }

     // close Scanner class object
     scan.close();
  }
}

Output:-

Enter min value of range:: 0
Enter max value of range:: 1000000
Neon numbers from 1 to 1000000 are::
0 1 9

Note:- 0, 1, and 9 are only known Neon numbers.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *