Java Program to Check whether the input number is a Neon Number

JavaObject Oriented ProgrammingProgramming

In this article, we will understand how to check whether the input number is a neon number. A neon number is such a number whose sum of digits of square of the number is equal to the number.

Below is a demonstration of the same −

Input

Suppose our input is −

9

Output

The desired output would be the following because 92 = 81 i.e. 8 + 1 = 9

The given input is a neon number

Algorithm

Step1- Start
Step 2- Declare an integers my_input
Step 3- Prompt the user to enter an integer value/ Hardcode the integer
Step 4- Read the values
Step 5- Compute the square of the input and store it in a variable input_square
Step 6- Compute the sum of the of the digits of input_square and compare the result with my_input
Step 6- If the input matches, then the input number is a neon number
Step 7- If not, the input is not a neon number.
Step 8- Display the result
Step 9- Stop

Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool run button.

import java.util.Scanner;
public class NeonNumbers{
   public static void main(String[] args){
      int my_input, input_square, sum=0;
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("Required packages have been imported");
      System.out.println("A scanner object has been defined ");
      System.out.println("Enter the number: ");
      my_input=my_scanner.nextInt();
      input_square=my_input*my_input;
      while(input_square>0){
         sum=sum+input_square%10;
         input_square=input_square/10;
      }
      if(sum!=my_input)
         System.out.println("The given input is not a Neon number.");
      else
         System.out.println("The given input is Neon number.");
   }
}

Output

Required packages have been imported
A scanner object has been defined
Enter the number:
9
The given input is Neon number.

Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class NeonNumbers{
   public static void main(String[] args){
      int my_input, input_square, sum=0;
      my_input= 9;
      System.out.printf("The number is %d ", my_input);
      input_square=my_input*my_input;
      while(input_square<0){
         sum=sum+input_square%10;
         input_square=input_square/10;
      }
      if(sum!=my_input)
         System.out.println("\nThe given input is not a Neon number.");
      else
         System.out.println("\nThe given input is Neon number.");
   }
}

Output

The number is 9
The given input is Neon number.
raja
Updated on 21-Feb-2022 10:07:05

Advertisements