Read Also : Java Program to Check Magic Number
What is Neon Number
A number is called as Neon number if sum of digits of the square of the number is equal to the number.
Examples :
Number to check : 9
Square of a given number : 9 * 9 = 81
Sum of digits of square number : 8 + 1 = 9 // 9 is a Neon number
Number to check : 1
Square of a given number : 1 * 1 = 1
Sum of digits of square number : 1 // 1 is a Neon number
Below are examples of numbers which are NOT Neon numbers
Number to check : 13
Square of a given number : 13 * 13 = 169
Sum of digits of square number : 1 + 6 + 9 = 16 // 13 is NOT a Neon number
Number to check : 8
Square of a given number : 8 * 8 = 64
Sum of digits of square number : 6 + 4 = 10 // 8 is NOT a Neon number
List of Neon numbers from 0 to 100 are :
0, 1, 9
Java Program for Neon Number
import java.util.*; public class JavaHungry { public static void main(String args[]) { System.out.println("Enter any number : "); Scanner scan = new Scanner(System.in); int inputNumber = scan.nextInt(); boolean result = checkNeonNumber(inputNumber); if(result == true) System.out.println(inputNumber + " is a Neon number"); else System.out.println(inputNumber + " is NOT a Neon number"); } public static boolean checkNeonNumber(int inputNumber) {
// Calculate square of inputNumber
int square = inputNumber * inputNumber;
// Initialize sumOfSquareDigits
int sumOfSquareDigits = 0; while(square > 0) { sumOfSquareDigits = sumOfSquareDigits + square % 10; square = square / 10; }
/* If sumOfSquareDigits is equal to inputNumber then the number is Neon number, otherwise not */
return (sumOfSquareDigits == inputNumber); } }
Output :
Enter any number : 27
27 is NOT a Neon number
Algorithm for Neon Number
1. Calculate the square of the given number(inputNumber).2. Initialize sumOfSquareDigits variable value to 0. It will represent the sum of digits of the square number.
3. Using while loop
a. Get the rightmost digit of variable square by using (square % 10) and add its value to the variable sumOfSquareDigits.
b. Get rid of the rightmost digit by dividing the square variable with 10. Store the resulting value in square variable.
c. Continue till square becomes 0.
4. Check whether the variable sumOfSquareDigits is equal to inputNumber.
a. If both are equal then the inputNumber is a Neon number.
b. Otherwise, the inputNumber is not a Neon number.
That's all for the day, please mention in comments if you know any other way to implement Neon number program in java.