In this section, we will explore the concept of neon numbers and develop a Java program to determine whether a given number is a neon number or not. Additionally, we will identify all the neon numbers within a given range.
What is Neon Number?
A neon number is defined as a positive integer whose sum of digits, when squared, is equal to the original number itself.
Example of Neon Number
Let’s verify whether the numbers 9 and 45 are neon numbers or not.
Steps to Find Neon Number
To determine whether a number is a neon number, follow these steps:
- Read an integer from the user or initialize a number (n) to check.
- Calculate the square of the given number (n) and store it in a variable called sq.
- Find the sum of the digits of the square (sq) and store the sum in a variable called sum.
- Compare the sum with the given number (n). If they are equal, the given number is a neon number. Otherwise, it is not a neon number.
Neon Number Java Program
Certainly! Here’s a Java program that checks if a given number is a neon number or not:
NeonNumberExample1.java
import java.util.*; public class NeonNumberExample { public static void main(String args[]) { int sum = 0, n; Scanner sc = new Scanner(System.in); System.out.print("Enter the number to check: "); //raeds an integer from the user n = sc.nextInt(); //calculate square of the given number int square = n * n; //loop executes until the condition becomes false while(square != 0) { //find the last digit of the square int digit = square % 10; //adds digits to the variable sum sum = sum + digit; //removes the last digit of the variable square square = square / 10; } //compares the given number (n) with sum if(n == sum) System.out.println(n + " is a Neon Number."); else System.out.println(n + " is not a Neon Number."); } }
Output:
Enter the number to check: 9 9 is a Neon Number.
NeonNumberExample2.java
import java.io.*; public class NeonNumberExample1 { //function to check Neon Number static boolean isNeon(int num) { //calculate the square of the given number int sq = num * num; //stores the sum of digits int sumOfdigits = 0; //executes until the condition becomes false while (sq != 0) { //finds the last digit of the variable sq and adds it to the variable sum sumOfdigits = sumOfdigits + sq % 10; //removes the last dogit of the variable sq sq = sq / 10; } //compares the sumOgDigits with num and returns the boolean value accordingly return (sumOfdigits == num); } //driver Code public static void main(String args[]) { System.out.print("Neon Numbers between the given range are: "); // 0 is the lowe limit and 100000 is the upper limit for (int i = 0; i <= 100000; i++) //calling the user-defined number if (isNeon(i)) //prints all the neon numbers between given range System.out.print(i+" "); } }
Output:
Neon Numbers between the given range are: 0 1 9
The concept of neon numbers is such that only a few exist within a given range. Let’s consider the range from 0 to 1 trillion (inclusive) and check if the numbers 0, 1, and 9 are neon numbers within this range.
To learn more like Neon Number in Java, make it a habit to regularly follow tutorials.freshersnow.com.