Java Program to Check if a Number is a Neon Number or not

by | Jan 10, 2023 | Java, Java Programs

What is a Neon Number?

If you are given a number and the summation of the square of the individual digits of the number equal to the number itself, then the number is called a neon number.

Example: For the number 9: 9*9 = 81 and 8 + 1 = 9

Illustration:

Case 1:

Input: 9
Output: Given number is a Neon Number

Explanation : square of 9 = 9 * 9 = 81;
sum of digits of square : 8 + 1 = 9 (same as the original number )

Case 2:

Input: 8
Output: Given number is not a Neon Number

Explanation: Square of 8 = 8 * 8 = 64
sum of digits of square : 6 + 4 = 10 (same as the original number)

Algorithm

  1. Start
  2. The first step is to find the square of the number.
  3. The next step is to implement a loop that will give the sum of the digits of the square of the number.
  4. Implement a condition such that when the condition checksum is true, True will be returned, which means that a number is a neon number. It will return false if the condition is false, which means that the number is not a neon number.

Pseudo Code

Square = n*n;
while (square > 0)
{
int r = square % 10;
sum + = r;
square = square / 10;
}

 

Sample Code

import java.io.*;
class Demo
{
//the function below will check if the inputted number is a neon number
public static boolean checkNeon (int n)
{
//in this step, we are finding the square of the original number
int square = n * n;
//using a flag variable called sum
int sum = 0;
//If the square of the number is > 0
while (square > 0)
{
//Finding the remainder
int r = square % 10;
//Summing the current value of the sum variable to the remainder
sum + = r;
//Extracting the last digit
square = square / 10;
}
//If the number obtained after the calculation is equal to the original number,
if (sum == n)
//returns true for a neon number
return true;
else
//returns false for a neon number
return false;
}
public static void main (String args [])
{
int n = 9;
//Invoking the above function created that will check for a neon number
if (checkNeon (n))
//if a neon number
System.out.println (“Given number ” + n + “is Neon Number”);
else
//if not, a neon number
System.out.println (“The given number” + n + “is not a neon number”);
}
}

 

OUTPUT

The given number is a Neon Number

Time Complexity

O (l) where the value of l depends on the number of digits that is contained in the square of the original number.

 

Author

0 Comments

Submit a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.