Neon Number Program In Java

In this tutorial we will learn what is neon number in java and how to check given number is neon number with  example . We will also write a java program to check given number is neon number or not .

what is neon number ?

A neon number is a number where the sum of digits of square of the number is equal to the number itself.

neon number example

Let’s discuss neon number examples  here .

Example 1 : if the input number is 9, its square is 9*9 = 81 and sum of the digits is (8+1) 9. i.e. 9 is a neon number.

Example 2 : if the input number is 45, its square is 45*45 = 2025 and sum of the digits is (2+0+2+5) 9 i.e. 45 is not a neon number

Algorithm to find Neon number

  1.  Take input from user an integer and initialize a number (n) to check.
  2.  Calculate the square of the given number (n) and store it in variable square.
  3.  Find the sum of the digits of the square (square) and store the sum in the variable (sum).
  4. Compare the given number n with sum ,  If both are equal, the given number is a neon number, else, not a neon number.
    Let’s implement the above steps in a Java program.

Neon Number Program In Java

import java.util.Scanner;

public class NeonNumber 
{
	public static void main(String[] args)
	{
		int n,square,sum=0;
	
		Scanner sc = new Scanner(System.in);

		System.out.print("enter a number: " );
		n=sc.nextInt();

		//calculate square.
		square=n*n;

		while(square>0)
		{
			sum=sum+square%10;
			square=square/10;
		}
		//condition for checking sum is equal or not.
		if(sum==n)
			System.out.println("Given number "+ n +" is a Neon number.");
		else
			System.out.println("Given number "+ n +" is not a Neon number.");
	}
}

Output :

enter a number: 9
Given number 9 is a Neon number.


enter a number: 45
Given number 45 is not a Neon number.

We have discussed what is neon number in java and we have write java program to find neon number . We have also see neon number example in this tutorial. You can learn more java coding program with easy example .

Leave a Reply

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

20 + = 21