Here in this tutorial you will learn how to check a neon number in Java.
What is a Neon Number?
Neon numbers are those numbers whose sum of the digits when squared is exactly the same as the number itself.
For example, the number 9.
Now the square of the number, 9 is 81. The sum of the digits in the square, 81 is 8 + 1 = 9, which is equal to the number itself.
Hence, the number 9 is a Neon number.
However, let the number is 11.
Now the square of the number, 11 is 121. The sum of the digits in the square, 121 is 1 + 2 + 1 = 4, which is not equal to the number itself.
Hence, the number 11 is NOT a Neon number.
Below is a program that inputs a number, and checks whether it is a Neon number or not.
Java Program to Check Neon Number
import java.util.Scanner; public class NeonNumber { static void checkForNeon(int number) { int square = number*number; int sumOfDigits = 0; while(square != 0) { sumOfDigits += square % 10; square = square/10; } if(sumOfDigits == number) System.out.println("The number is a Neon number"); else System.out.println("The number is not a Neon number"); } public static void main (String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a Number: "); int number = input.nextInt(); checkForNeon(number); } }
Output:
Enter a Number: 9
The number is a Neon number
The program runs a while loop to calculate the sum of the digits of the input number’s square.
Remember, sumOfDigits += square / 10; is the same as sumOfDigits = sumOfDigits + square / 10;
The program then just compares and see if the sum is in fact equal to the number itself.
There are only three neon numbers though: 0, 1 and 9.