Check Whether a Number is a Neon Number in Java



In this article, we will understand how to check whether the given 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 itself.

Example Scenario:

Input: num = 9;
Output: The given input is a neon number

How to Check Neon Number in Java?

To check a given input is a neon number in Java, use the below approaches ?

  • Using for loop
  • Using while loop
  • Using recursion

Using for Loop

In this approach, the for loop iterates as long as the square of given input is not equal to 0. During each iteration, extract the last digit and add it to get the sum of digits of square. Then, using an if-else block check whether the given input is a neon number.

Example

In this example, we are checking neon number using for loop.

public class NeonNumbers{
   public static void main(String[] args){
      int my_input, input_square, sum = 0;
      my_input = 9;
      System.out.print("The number is: " + my_input);
      input_square = my_input*my_input;
      for (; input_square != 0; input_square /= 10) {
         sum += 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.");
   }
}

On executing, this code will produce the following result ?

The number is: 9
The given input is Neon number.

Using while Loop

This is another way to check the neon number in Java. It uses the same logic as discussed above but the implementation is different. Instead of for loop, we are using while loop.

Example

In the following example, we are using a while loop to check whether the input number is a neon number.

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.");
   }
}

When you run this code, it will display the below output ?

The number is 9
The given input is Neon number.

Using Recursion

In the recursive approach, we create a function that will call itself to check if given input number is neon or not.

Example

The following example demonstrates how to check whether the given input is a neon number with the help of recursion.

public class NeonNumbers {
   public static int checkNeonFunc(int input_square, int sum) {
      if (input_square == 0) {
         return sum;
      } else {
         return checkNeonFunc(input_square / 10, sum + (input_square % 10));
      }
   }
   public static void main(String[] args) {
      int my_input, input_square, sum = 0;
      my_input = 9;
      System.out.println("The number is: " + my_input);
      input_square = my_input*my_input;
      if (checkNeonFunc(input_square, 0) == my_input)
         System.out.println(my_input + " is a Neon Number.");
      else
         System.out.println(my_input + " is not a Neon Number.");
   }
}

On executing the above code, it will produce the following result ?

The number is: 9
9 is a Neon Number.
Updated on: 2024-08-01T11:54:45+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements