Posted on by Kalkicode
Code Number

# Check whether given number is neon number or not

A neon number is a specific type of number in which the sum of the digits of the square of the number is equal to the original number itself. In other words, if you take a number, square it, and then sum its digits, if the sum is the same as the original number, then that number is called a neon number. For example, let's consider the number 9.

• Square of 9 = 9 * 9 = 81
• Sum of the digits of 81 = 8 + 1 = 9

As you can see, the sum of the digits (9) is equal to the original number (9). Hence, 9 is a neon number.

## Explanation with Examples

Let's take some more examples to understand the concept better:

1. Consider the number 1:
• Square of 1 = 1 * 1 = 1
• Sum of the digits of 1 = 1

Since the sum of the digits (1) is equal to the original number (1), 1 is a neon number.

1. Consider the number 5:
• Square of 5 = 5 * 5 = 25
• Sum of the digits of 25 = 2 + 5 = 7

Since the sum of the digits (7) is not equal to the original number (5), 5 is not a neon number.

## Pseudocode and Algorithm

``````Function isNeon(num):
square = num * num
sum = 0
while square != 0:
sum = sum + (square % 10)
square = square / 10

Print "Number: num"
if sum == num:
Print "Yes"
else:
Print "No"``````

## Explanation

• The `isNeon` function takes an integer `num` as input.
• It calculates the square of the input number and stores it in the variable `square`.
• It then initializes a variable `sum` to store the sum of the digits of the square.
• The `while` loop is executed until the square becomes zero. In each iteration, the last digit of the square is extracted using `(square % 10)` and added to the `sum`. The last digit is then removed from the square using `(square / 10)`.
• After the loop, the function checks whether `sum` is equal to the original number `num` or not.
• The result is then printed as "Yes" if `sum == num`, meaning the number is a neon number. Otherwise, it prints "No."

## Code Solution

Here given code implementation process.

``````//  C program
//  Check whether given number is neon number or not
#include <stdio.h>

// Check number is neon or not
void isNeon(int num)
{

// Calculate square of given number
int square =  num * num;

// Use to calculate digit sum element
int sum = 0;

// Execute loop until square of given number is not zero
while(square != 0)
{
sum    +=  square % 10;

// Remove last digit
square /= 10;
}

// Display given number
printf("\n Number : %d",num);

if(sum == num)
{
// When number is neon
printf("\n Yes ");
}
else
{
// When number is not neon
printf("\n No");
}

}
int main(int argc, char const *argv[])
{

// Test case
isNeon(1);
isNeon(5);
isNeon(9);

return 0;
}``````

#### Output

`````` Number : 1
Yes
Number : 5
No
Number : 9
Yes``````
``````/*
Java program
Compare two numbers without using comparison operator
*/
public class NeonNumber
{
// Check number is neon or not
public void isNeon(int num)
{
// Calculate square of given number
int square = num * num;
// Use to calculate digit sum element
int sum = 0;
// Execute loop until square of given number is not zero
while (square != 0)
{
sum += square % 10;
// Remove last digit
square /= 10;
}
// Display given number
System.out.print("\n Number : " + num);
if (sum == num)
{
// When number is neon
System.out.print("\n Yes ");
}
else
{
// When number is not neon
System.out.print("\n No");
}
}
public static void main(String[] args)
{
// Test case
}
}``````

#### Output

`````` Number : 1
Yes
Number : 5
No
Number : 9
Yes``````
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ program
Compare two numbers without using comparison operator
*/
class NeonNumber
{
public:
// Check number is neon or not
void isNeon(int num)
{
// Calculate square of given number
int square = num *num;
// Use to calculate digit sum element
int sum = 0;
// Execute loop until square of given number is not zero
while (square != 0)
{
sum += square % 10;
// Remove last digit
square /= 10;
}
// Display given number
cout << "\n Number : " << num;
if (sum == num)
{
// When number is neon
cout << "\n Yes ";
}
else
{
// When number is not neon
cout << "\n No";
}
}
};
int main()
{
// Test case
return 0;
}``````

#### Output

`````` Number : 1
Yes
Number : 5
No
Number : 9
Yes``````
``````// Include namespace system
using System;
/*
C# program
Compare two numbers without using comparison operator
*/
public class NeonNumber
{
// Check number is neon or not
public void isNeon(int num)
{
// Calculate square of given number
int square = num * num;
// Use to calculate digit sum element
int sum = 0;
// Execute loop until square of given number is not zero
while (square != 0)
{
sum += square % 10;
// Remove last digit
square /= 10;
}
// Display given number
Console.Write("\n Number : " + num);
if (sum == num)
{
// When number is neon
Console.Write("\n Yes ");
}
else
{
// When number is not neon
Console.Write("\n No");
}
}
public static void Main(String[] args)
{
// Test case
}
}``````

#### Output

`````` Number : 1
Yes
Number : 5
No
Number : 9
Yes``````
``````<?php
/*
Php program
Compare two numbers without using comparison operator
*/
class NeonNumber
{
// Check number is neon or not
public	function isNeon(\$num)
{
// Calculate square of given number
\$square = \$num * \$num;
// Use to calculate digit sum element
\$sum = 0;
// Execute loop until square of given number is not zero
while (\$square != 0)
{
\$sum += \$square % 10;
// Remove last digit
\$square = intval(\$square / 10);
}
// Display given number
echo "\n Number : ". \$num;
if (\$sum == \$num)
{
// When number is neon
echo "\n Yes ";
}
else
{
// When number is not neon
echo "\n No";
}
}
}

function main()
{
// Test case
}
main();``````

#### Output

`````` Number : 1
Yes
Number : 5
No
Number : 9
Yes``````
``````/*
Node Js program
Compare two numbers without using comparison operator
*/
class NeonNumber
{
// Check number is neon or not
isNeon(num)
{
// Calculate square of given number
var square = num * num;
// Use to calculate digit sum element
var sum = 0;
// Execute loop until square of given number is not zero
while (square != 0)
{
sum += square % 10;
// Remove last digit
square = parseInt(square / 10);
}
// Display given number
process.stdout.write("\n Number : " + num);
if (sum == num)
{
// When number is neon
process.stdout.write("\n Yes ");
}
else
{
// When number is not neon
process.stdout.write("\n No");
}
}
}

function main()
{
// Test case
}
main();``````

#### Output

`````` Number : 1
Yes
Number : 5
No
Number : 9
Yes``````
``````#   Ruby program
#   Compare two numbers without using comparison operator

class NeonNumber
#  Check number is neon or not
def isNeon(num)
#  Calculate square of given number
square = num * num
#  Use to calculate digit sum element
sum = 0
#  Execute loop until square of given number is not zero
while (square != 0)
sum += square % 10
#  Remove last digit
square /= 10
end

#  Display given number
print("\n Number : ", num)
if (sum == num)
#  When number is neon
print("\n Yes ")
else
#  When number is not neon
print("\n No")
end

end

end

def main()
#  Test case
end

main()``````

#### Output

`````` Number : 1
Yes
Number : 5
No
Number : 9
Yes ``````
``````#   Python 3 program
#   Compare two numbers without using comparison operator

class NeonNumber :
#  Check number is neon or not
def isNeon(self, num) :
#  Calculate square of given number
square = num * num
#  Use to calculate digit sum element
sum = 0
#  Execute loop until square of given number is not zero
while (square != 0) :
sum += square % 10
square = int(square /
#  Remove last digit
10)

#  Display given number
print("\n Number : ", num, end = "")
if (sum == num) :
#  When number is neon
print("\n Yes ", end = "")
else :
#  When number is not neon
print("\n No", end = "")

def main() :
#  Test case

if __name__ == "__main__": main()``````

#### Output

`````` Number :  1
Yes
Number :  5
No
Number :  9
Yes``````
``````/*
Scala program
Compare two numbers without using comparison operator
*/
class NeonNumber
{
// Check number is neon or not
def isNeon(num: Int): Unit = {
// Calculate square of given number
var square: Int = num * num;
// Use to calculate digit sum element
var sum: Int = 0;
// Execute loop until square of given number is not zero
while (square != 0)
{
sum += square % 10;
// Remove last digit
square = (square / 10).toInt;
}
// Display given number
print("\n Number : " + num);
if (sum == num)
{
// When number is neon
print("\n Yes ");
}
else
{
// When number is not neon
print("\n No");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: NeonNumber = new NeonNumber();
// Test case
}
}``````

#### Output

`````` Number : 1
Yes
Number : 5
No
Number : 9
Yes``````
``````/*
Swift 4 program
Compare two numbers without using comparison operator
*/
class NeonNumber
{
// Check number is neon or not
func isNeon(_ num: Int)
{
// Calculate square of given number
var square: Int = num * num;
// Use to calculate digit sum element
var sum: Int = 0;
// Execute loop until square of given number is not zero
while (square  != 0)
{
sum += square % 10;
// Remove last digit
square /= 10;
}
// Display given number
print("\n Number : ", num, terminator: "");
if (sum == num)
{
// When number is neon
print("\n Yes ", terminator: "");
}
else
{
// When number is not neon
print("\n No", terminator: "");
}
}
}
func main()
{
// Test case
}
main();``````

#### Output

`````` Number :  1
Yes
Number :  5
No
Number :  9
Yes``````
``````/*
Kotlin program
Compare two numbers without using comparison operator
*/
class NeonNumber
{
// Check number is neon or not
fun isNeon(num: Int): Unit
{
// Calculate square of given number
var square: Int = num * num;
// Use to calculate digit sum element
var sum: Int = 0;
// Execute loop until square of given number is not zero
while (square != 0)
{
sum += square % 10;
// Remove last digit
square /= 10;
}
// Display given number
print("\n Number : " + num);
if (sum == num)
{
// When number is neon
print("\n Yes ");
}
else
{
// When number is not neon
print("\n No");
}
}
}
fun main(args: Array < String > ): Unit
{
// Test case
}``````

#### Output

`````` Number : 1
Yes
Number : 5
No
Number : 9
Yes``````

## Time Complexity

Let's analyze the time complexity of the algorithm. The `while` loop runs for each digit of the square of the given number, which is proportional to the number of digits in the square. Since the number of digits in the square is at most twice the number of digits in the original number (considering the worst case where the square has one extra digit), the time complexity can be approximated as O(d), where 'd' is the number of digits in the original number.

## Resultant Output

The given C program tests the `isNeon` function with three test cases: 1, 5, and 9. Here's the output for each case:

``````Number: 1
Yes

Number: 5
No

Number: 9
Yes
``````

As we can see, the output confirms that 1 and 9 are neon numbers, while 5 is not.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post