Neon number program in java

neon number program in java

Neon number program in java


Write a program in java to accept a number from the user and check if it is a neon number or not.

The neon number is nothing but the sum of digits of square of the number is equal to the number.
Example :
Input number 9
Square the number 9 * 9 and it comes to 81
Sum the digits of the square number 8 + 1 = 9
If the number is equal to the sum of the square number then it is a neon the number and if it is not then it is not a neon number.


Java program of Neon Number Example



import java.util.*;
// Neon Number Class
public class NeonNumberCheck
{
    // main method begins execution of this class
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int n;      // n for number to check neon or not
        int m;      // m for square of the n 
        int a;      // a for taking reminder 
        int s=0;    // for summation
        System.out.println("Enter a number: ");
        n=sc.nextInt();
        m=n*n;      // Squaring of the number n
        while(m!=0)
        {
            a=m%10; // Calculating reminder
            s=s+a;  // adding the reminder to s
            m=m/10; // Integer division to truncate the last digit
        }
        if(s == n)  // Checking the original number to the summation
        {
            System.out.println("Neon number");
        }
        else
        {
            System.out.println("Not a Neon Number");
        }
    }
}

Sample output
Enter a number:
8
Not a Neon Number
Enter a number:
9
Neon number
Enter a number:
10
Not a Neon Number

In the above program, the entered number is calculated as m = n*n and then 'm' is mode division by 10 and reminder added to s as s=s+a and then integer division is don till number become Zero.
After check it as
  if(s == n)  // Checking the original number to the summation
        {
            System.out.println("Neon number");
        }
        else
        {
            System.out.println("Not a Neon Number");
        }
This will give the required result.

Variable Description
Sr. No
Variable / Function
Types
Description
1
sc
Object
Object of Scanner class
2
main()
void
main() method
3
n
int
No to be check as NEON no.
4
m
int
Store n
5
a
int
For reminder
6
s
int
Summation calculation


SHARE THIS
Previous Post
Next Post