In this tutorial, we will write a java program to check whether a given number is Neon number or not. We will also write a program to find all the Neon numbers in a given range.
What is a Neon Number?
A number is called Neon number if sum of digits of its square is equal to the number itself. For example 9 is a neon number because:
Original Number = 9 Square of 9 = 9 X 9 = 81 Sum of digits of its square = 8+1 = 9 = Original Number
Similarly 1 is also a neon number because:
Original number = 1 Square of 1 = 1 X 1 = 1 Sum of digits of its square = 1 = original number
Java Program to check if a number is Neon Number
The steps to check for Neon number are:
- Read the number input by user.
- Calculate the square of the input number.
- Calculate the sum of digits of square.
- Compare the sum of digits of square and number, if both the equal then display “Neon Number” else display “Not a Neon Number“.
import java.util.*; public class JavaExample { public static void main(String args[]) { int sumSquareDigits=0, num; Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); //Reading user input and storing into variable num num = scanner.nextInt(); scanner.close(); int square = num * num; //Finding the sum of digits of square while(square != 0) { //getting last digit of square int lastDigit = square % 10; //adding the last digit to the sumSquareDigits sumSquareDigits = sumSquareDigits + lastDigit; //removing last digit from square to repeat the process //for second last digit and so on square = square / 10; } //comparing the number with sum of digits of square if(num == sumSquareDigits) System.out.println(num + " is a Neon Number."); else System.out.println(num + " is not a Neon Number."); } }
Output:
Enter a number: 9 9 is a Neon Number.
Program to find all Neon Numbers in a Given Range
In this program, we are finding all the Neon numbers in a given range. The user enters the start and end range, we then run a loop from start range till end range and for each number between this range, we call the checkNeon()
method. This method checks whether the number is Neon number or not. If the number is Neon it gets displayed else it gets skipped.
import java.util.*; public class JavaExample { static boolean checkNeon(int num) { int square = num * num; int sumSquareDigits = 0; while (square != 0) { sumSquareDigits = sumSquareDigits + square % 10; square = square / 10; } return sumSquareDigits == num; } public static void main(String args[]) { int startRange, endRange; Scanner scanner = new Scanner(System.in); System.out.print("Enter the Starting Range: "); startRange = scanner.nextInt(); System.out.print("Enter the Ending Range: "); endRange = scanner.nextInt(); System.out.print("All Neon numbers between "+startRange+ " and "+endRange+" are: "); for (int i = startRange; i <= endRange; i++) if (checkNeon(i)) System.out.print(i+" "); } }
Output:
Enter the Starting Range: 1 Enter the Ending Range: 500 All Neon numbers between 1 and 500 are: 1 9
Leave a Reply