Pseudo code for checking neon number in Java:
Let’s look at the pseudo-code for checking if a number is a neon number in Java:
function isNeonNumber(number):
square = number * number
sum = 0
while square != 0:
digit = square % 10
sum = sum + digit
square = square / 10
if sum == number:
return true
else:
return false
// Main program
input number
if isNeonNumber(number):
print number + " is a neon number"
else:
print number + " is not a neon number"
Let's discuss the pseudo-code in detail:
1. We define a function called `isNeonNumber` that takes a number as input.
2. Inside the function:
- We calculate the square of the number by multiplying it with itself and store it in the `square` variable.
- We initialize a variable `sum` to 0. This variable will store the sum of the digits.
3. We start a while loop that continues until `square` becomes 0:
- We extract the last digit of `square` using the modulo operator `%` and store it in the `digit` variable.
- We add the `digit` to the `sum` variable.
- We divide `square` by 10 to remove the last digit.
4. After the loop ends, we check if `sum` is equal to the original `number`:
- If they are equal, we return `true`, indicating that the number is a neon number.
- Otherwise, we return `false`.
5. In the main program:
- We input a number.
- We call the `isNeonNumber` function with the input number.
- If the function returns `true`, we print that the number is a neon number.
- If the function returns `false`, we print that the number is not a neon number.
Algorithm to find neon number in Java
Now, we will discuss the algorithm in detail or in step by step manner :
1. Input a number to check if it is a neon number.
2. Square the number by multiplying it by itself.
3. Initialize a variable `sum` to 0. This variable will store the sum of the digits.
4. Start a loop that continues until the squared number becomes 0:
- Extract the last digit of the squared number using the modulo operator `%`.
- Add the extracted digit to the `sum` variable.
- Divide the squared number by 10 to remove the last digit.
5. After the loop ends, compare the `sum` with the original input number.
6. If the `sum` is equal to the original number, it is a neon number. Otherwise, it is not a neon number.
7. Output the result indicating whether the number is a neon number or not.
Here's the algorithm in a more concise form:
1. Input number
2. square = number * number
3. sum = 0
4. while square != 0:
digit = square % 10
sum = sum + digit
square = square / 10
5. if sum == number:
print "Neon Number"
else:
print "Not a Neon Number"
This algorithm provides a clear step-by-step approach to checking if a number is a neon number. It handles the squaring of the number, extracting digits, calculating the sum of digits, and comparing the sum with the original number.
Implementation of this algorithm
Java
public class NeonNumberExample {
public static boolean isNeonNumber(int num) {
int square = num * num;
int sum = 0;
while (square != 0) {
int digit = square % 10;
sum += digit;
square /= 10;
}
return sum == num;
}
public static void main(String[] args) {
int number = 9;
if (isNeonNumber(number)) {
System.out.println(number + " is a neon number.");
} else {
System.out.println(number + " is not a neon number.");
}
}
}

You can also try this code with Online Java Compiler
Run Code
In this code, we have a method called `isNeonNumber()` that takes an integer `num` as input.
Let’s see how this program actually worked :
1. We calculate the square of the number `num` and store it in the `square` variable.
2. We initialize a variable `sum` to keep track of the sum of the digits.
3. We start a loop that continues as long as `square` is not equal to 0.
4. Inside the loop, we extract the last digit of `square` using the modulo operator `%` and store it in the `digit` variable.
5. We add the `digit` to the `sum` variable.
6. We divide `square` by 10 to remove the last digit.
7. After the loop ends, we check if the `sum` is equal to the original number `num`. If they are equal, we return `true`, indicating that the number is a neon number. Otherwise, we return `false`.
In the `main()` method, we provide a sample number (in this case, 9) and call the `isNeonNumber()` method to check if it is a neon number. The program will output whether the number is a neon number or not.
Output
9 is a neon number.
Dry run
Let's perform a dry run of the algorithm to find a neon number step by step. We'll use the number 9 as an example input to illustrate the process.
Input: number = 9
Step 1: Square the number.
square = number * number
square = 9 * 9 = 81
Step 2: Initialize the sum variable to 0.
sum = 0
Step 3: Start the loop to extract digits and calculate the sum.
Iteration 1:
- digit = square % 10 = 81 % 10 = 1
- sum = sum + digit = 0 + 1 = 1
- square = square / 10 = 81 / 10 = 8
Iteration 2:
- digit = square % 10 = 8 % 10 = 8
- sum = sum + digit = 1 + 8 = 9
- square = square / 10 = 8 / 10 = 0
Step 4: Check if the sum is equal to the original number.
sum == number
9 == 9
The condition is true.
Step 5: Output the result.
9 is a neon number.
So, in this dry run, we can see how the algorithm works step by step. We square the number 9 to get 81, initialize the sum to 0, and then start the loop to extract digits and calculate the sum. In the first iteration, we extract the last digit 1 from 81 and add it to the sum. In the second iteration, we extract the last digit 8 from 8 and add it to the sum. After the loop ends, we compare the sum (9) with the original number (9), and they are equal. Therefore, we conclude that 9 is a neon number.
This dry run demonstrates how the algorithm flows and how the variables change at each step. It helps us understand the logic behind the algorithm and ensures that it produces the correct result.
Recursive approach for finding neon number in Java
In addition to the iterative approach we discussed earlier, we can also solve the neon number problem using a recursive approach. Here's how we can implement a recursive solution in Java:
Java
public class NeonNumberRecursive {
public static int sumOfDigits(int num) {
if (num == 0) {
return 0;
}
return (num % 10) + sumOfDigits(num / 10);
}
public static boolean isNeonNumber(int num) {
int square = num * num;
int sum = sumOfDigits(square);
return sum == num;
}
public static void main(String[] args) {
int number = 9;
if (isNeonNumber(number)) {
System.out.println(number + " is a neon number.");
} else {
System.out.println(number + " is not a neon number.");
}
}
}

You can also try this code with Online Java Compiler
Run Code
In this recursive approach, we have two methods:
1. `sumOfDigits(int num)`: This method calculates the sum of digits recursively.
- If `num` becomes 0, we have reached the base case and return 0.
- Otherwise, we recursively call `sumOfDigits` with `num / 10` to process the remaining digits and add the last digit (`num % 10`) to the sum.
2. `isNeonNumber(int num)`: This method checks if a number is a neon number.
- We calculate the square of the number `num` and store it in the `square` variable.
- We call the `sumOfDigits` method with `square` to calculate the sum of digits recursively.
- We compare the sum with the original number `num` and return `true` if they are equal, indicating a neon number, or `false` otherwise.
In the `main` method, we provide a sample number (in this case, 9) and call the `isNeonNumber` method to check if it is a neon number. The program will output whether the number is a neon number or not.
Output:
9 is a neon number.
The recursive approach breaks down the problem into smaller subproblems. In this case, we recursively calculate the sum of digits by dividing the number by 10 in each recursive call until we reach the base case of 0. The recursive calls stack up, and the sum is calculated as the recursive calls return.
Frequently Asked Questions
What is a neon number?
A neon number is a number whose sum of digits of its square is equal to the number itself.
Can a negative number be a neon number?
No, negative numbers cannot be neon numbers. Neon numbers are always non-negative.
Is 0 a neon number?
Yes, 0 is a neon number because the square of 0 is 0, and the sum of digits of 0 is also 0.
Conclusion
In this article, we discussed the concept of neon numbers in Java. We learned that a neon number is a number whose sum of digits of its square equals the number itself. We also explained the algorithm to check if a number is a neon number and provided a step-by-step explanation through a dry run. We also looked at the pseudo-code and a recursive approach to solve the problem.
You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.