Java Program to Check Neon Number
A neon number is a number where the sum of digits of its square is equal to the number itself.
For example, 9 is a neon number because 9² = 81 and 8 + 1 = 9.
1. Understanding the Problem
Determine whether a given number is a neon number.
Number: 9 → Neon Number: 1 → Neon Number: 10 → Not Neon
2. Using Basic Loop
Java
Check neon number using loop
import java.util.Scanner;
public class NeonNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int square = num * num;
int sum = 0;
while (square != 0) {
int digit = square % 10;
sum += digit;
square /= 10;
}
if (sum == num)
System.out.println("Neon Number");
else
System.out.println("Not a Neon Number");
}
}
3. Optimized Approach
Java
Check neon number with simplified logic
import java.util.Scanner;
public class NeonOptimized {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int square = num * num;
int sum = 0;
for (; square > 0; square /= 10) {
sum += square % 10;
}
if (sum == num)
System.out.println("Neon Number");
else
System.out.println("Not a Neon Number");
}
}
4. Common Mistakes
1. Forgetting to square the number.
2. Incorrect digit extraction from square.
3. Not resetting sum properly.
4. Confusing with Armstrong number logic.
5. Applications
1. Helps in understanding number transformations.
2. Useful for practicing loops and arithmetic operations.
3. Common beginner-level coding problem.
Conclusion
Neon number programs are simple and useful for practicing digit-based logic.
They help build a strong foundation in loops and number manipulation.
Codecrown