Google News
logo
Java program to Neon Number
A neon number is a number where the sum of the digits of its square is equal to the number itself.

For example, 9 is a neon number, because the square of 9 is 81 and 8 + 1 = 9.

Here is a Java program to check if a given number is a neon number or not :
Program :
import java.util.Scanner;

public class NeonNumber {

    public static void main(String[] args) {

        // Get input from user
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number to check if it is a neon number: ");
        int number = scanner.nextInt();
        
        // Calculate square of number
        int square = number * number;

        // Calculate sum of digits of square
        int sum = 0;
        while (square != 0) {
            int digit = square % 10;
            sum += digit;
            square /= 10;
        }

        // Check if sum of digits of square is equal to number
        if (sum == number) {
            System.out.println(number + " is a neon number");
        } else {
            System.out.println(number + " is not a neon number");
        }
    }
}
Output :
Enter a number to check if it is a neon number: 9
9 is a neon number
Enter a number to check if it is a neon number: 13
13 is not a neon number

In this program, we first get input from the user using a Scanner object. We then calculate the square of the number and the sum of its digits using a while loop.

Finally, we check if the sum of digits of the square is equal to the original number and print the result.