Neon Number in Java
In this tutorial, we will check whether a number is a Neon number or not. Neon numbers are numbers that have a square digit sum equivalent to the number itself. A neon number is one for which the sum of its square's digits equals the value of the number.
Algorithm
Step 1: Begin
Step 2: Declare an integer named my input.
Step 3: Request an integer value from the user or hardcode the integer
Step 4: Read the values in
Step 5: Square the input and store the result in the variable input square.
Step 6: Add up the digits in input square, then compare the outcome to my input.
Step 7: The input number is a neon number if the input matches.
Step 8: The input is not a neon number if it is not.
Step 9: Show the outcome
Example of Neon Number
Check whether 9 and 45 are neon numbers by using an example.
(9)2 = 81
8+1 = 9
9 = 9
Here, 9 is Neon number.
45
(45)2 = 2025
2+0+2+5 = 9
45 is not a Neon number.
How to check a Neon Number
To check, either initialize a number (n) or get an integer from the user.
Determine the square of the provided integer (n), then save the result in the sq variable.
- Find the square's digits' sum (sq), then put it in the variable (sum).
- Compare n with the value given. The provided number is a neon number if both are equal; otherwise, it is not a neon number.
Let's put the above steps actions into a Java program.
Program
The reasoning is very basic. We must first determine the square of the provided number. Calculate the square's digit sum after that.
Let's write a Java program that determines whether or not the provided number is neon.
Filename: NeonNumberExample1.java
import java.util.*;
public class NeonNumberExample1
{
public static void main(String args[])
{
int sum = 0, n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number to check: ");
// takes a user-provided integer.
n = sc.nextInt();
// square the number that is supplied.
int square = n * n;
// loop continues to run until the condition is false.
while(square != 0)
{
// find the square's final digit.
int digit = square % 10;
// increases the variable total by one.
sum = sum + digit;
// removes the variable square's final digit.
square = square / 10;
}
// compares the supplied number (n) to the total
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.
Let's search for every neon number that falls inside a given range.
Filename: NeonNumberExample2.java
import java.io.*;
public class NeonNumberExample2
{
// Neon Number Checking Function
static boolean isNeon(int num)
{
// determine the square of the specified number.
int sq = num * num;
// save the whole number of digits
int sumOfdigits = 0;
// until the condition is false, then
while (sq != 0)
{
// the variable sum is increased by the final digit of the variable sq.
sumOfdigits = sumOfdigits + sq % 10;
// the final dogit of the variable sq is eliminated.
sq = sq / 10;
}
// returns a boolean value based on the comparison of the sumOgDigits with num.
return (sumOfdigits == num);
}
//driver's Code
public static void main(String args[])
{
System.out.print("Neon Numbers between the given range are: ");
// The lower and upper limits are 0 and 100000 respectively.
for (int i = 0; i <= 100000; i++)
// The range's minimum and maximum values are 0 and 100000 respectively.
if (isNeon(i))
// prints all neon numbers within the specified range.
System.out.print(i+" ");
}
}
Output :
Neon Numbers between the given range are: 0 1 9