Java Program to Check Neon Number

In this post, we will learn to code the Java Program to Check Neon Number. Let’s understand the Neon number and How to Find Neon Number in Java Programming Language.

What is Neon Number?

A number is said to be a Neon Number if the sum of the digits of the square of the number is equal to the same number. For example, 9 is a neon number because 9 * 9 = 81, and the sum of 81 is 8 + 1 = 9 which is equal to the original number.

We will code the Java Program to Check Neon Number using two approaches, for loop and while loop.

Let’s see the Java Program to Check Neon Number Using For Loop.

Java Program to Check Neon Number

import java.util.*;

public class Main{
    
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number : ");
        int num = sc.nextInt();
        
        // Finding the square root
        int sqr = num * num;
        
        int sum;
        
        // Finding the sum
        for (sum = 0; sqr > 0; sqr /= 10){
            int rem = sqr % 10;
            sum = sum + rem;
        }
        
        // Checking whether sum is equals to original number
        if (sum == num){
            System.out.println(""+num+" is a neon number.");
        }
        else{
             System.out.println(""+num+" is a NOT neon number.");
        }
    }
    
}

Output 1

Enter a number: 9
9 is a neon number.

Output 2

Enter the number : 45
45 is a NOT neon number.

How Does This Program Work ?

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number : ");
        int num = sc.nextInt();

In this Program, first, we take the input of the number from the user using the Scanner Class in Java.

        // Finding the square root
        int sqr = num * num;

Then, we calculate the square of the number and we store it in a variable sqr of int data type.

        int sum;
        
        // Finding the sum
        for (sum = 0; sqr > 0; sqr /= 10){
            int rem = sqr % 10;
            sum = sum + rem;
        }

Now, we calculate the sum of the digits of the square number using for loop. Suppose, the entered number is 9, then it’s square will be 81. Therefore, 

1st For Loop Iteration: for (sum = 0;  81 > 0; 81 /= 10)
rem = sqr % 10 = 81 % 10 = 1
sum = sum + rem = 0 + 1 = 1

2nd For Loop Iteration: for (sum = 1; 8 > 0; 8 /= 10)
rem = sqr % 10 = 8 % 10 = 8
sum = sum + rem = 1 + 8 = 9

We get sum = 9.

        // Checking whether sum is equals to original number
        if (sum == num){
            System.out.println(""+num+" is a neon number.");
        }
        else{
             System.out.println(""+num+" is a NOT neon number.");
        }

Then, we check whether the sum is equal to the original number. If the condition is True, then we print the number as a neon number otherwise it’s not a neon number.

This is the Java Program to Check Neon Number Using For Loop.

Java Program to Check Neon Number Using While Loop

import java.util.*;

public class Main{
    
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number : ");
        int num = sc.nextInt();
        
        // Finding the square root
        int sqr = num * num;
        
        int sum=0;
        
        // Finding the sum
        while (sqr != 0){
            int rem = sqr % 10;
            sum = sum + rem;
            sqr = sqr / 10;
        }
        
        // Checking whether sum is equals to original number
        if (sum == num){
            System.out.println(""+num+" is a neon number.");
        }
        else{
             System.out.println(""+num+" is a NOT neon number.");
        }
    }
    
}

Output 1

Enter a number: 9
9 is a neon number.

Output 2

Enter the number : 45
45 is a NOT neon number.

This is the Java Program to Check Neon Number Using While Loop.

Conclusion

I hope after going through this post, you understand Java Program to Check Neon Number.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *