Neon Number
Last Updated :
07 Jan, 2024
A neon number is a number where the sum of digits of square of the number is equal to the number. The task is to check and print neon numbers in a range.
Examples:
Input : 9
Output : Neon Number
Explanation: square is 9*9 = 81 and
sum of the digits of the square is 9.
Input :12
Output : Not a Neon Number
Explanation: square is 12*12 = 144 and
sum of the digits of the square is 9 (1
+ 4 + 4) which is not equal to 12.
The implementation is simple, we first compute square of given number, the find sum of digits in the square.
C++
#include <iostream>
using namespace std;
#include <math.h>
int checkNeon( int x)
{
int sq = x * x;
int sum_digits = 0;
while (sq != 0) {
sum_digits = sum_digits + sq % 10;
sq = sq / 10;
}
return (sum_digits == x);
}
int main( void )
{
for ( int i = 1; i <= 10000; i++)
if (checkNeon(i))
cout << i << " " ;
}
|
Java
import java.io.*;
class GFG {
static boolean checkNeon( int x)
{
int sq = x * x;
int sum_digits = 0 ;
while (sq != 0 ) {
sum_digits = sum_digits + sq % 10 ;
sq = sq / 10 ;
}
return (sum_digits == x);
}
public static void main(String args[])
throws IOException
{
for ( int i = 1 ; i <= 10000 ; i++)
if (checkNeon(i))
System.out.print(i + " " );
}
}
|
Python
def checkNeon (x) :
sq = x * x
sum_digits = 0
while (sq ! = 0 ) :
sum_digits = sum_digits + sq % 10
sq = sq / 10
return (sum_digits = = x)
i = 1
while i < = 10000 :
if (checkNeon(i)) :
print i,
i = i + 1
|
C#
using System;
class GFG
{
static bool checkNeon( int x)
{
int sq = x * x;
int sum_digits = 0;
while (sq != 0)
{
sum_digits = sum_digits + sq % 10;
sq = sq / 10;
}
return (sum_digits == x);
}
public static void Main()
{
for ( int i = 1; i <= 10000; i++)
if (checkNeon(i))
Console.Write(i + " " );
}
}
|
Javascript
<script>
function checkNeon(x)
{
let sq = x * x;
let sum_digits = 0;
while (sq != 0) {
sum_digits = sum_digits + sq % 10;
sq = Math.floor(sq / 10);
}
return (sum_digits == x);
}
for (let i = 1; i <= 10000; i++)
if (checkNeon(i))
document.write(i + " " );
</script>
|
PHP
<?php
function checkNeon( $x )
{
$sq = $x * $x ;
$sum_digits = 0;
while ( $sq != 0)
{
$sum_digits = $sum_digits +
$sq % 10;
$sq = $sq / 10;
}
return ( $sum_digits == $x );
}
for ( $i = 1; $i <= 10000; $i ++)
if (checkNeon( $i ))
echo $i . " " ;
?>
|
Output:
1 9
Share your thoughts in the comments
Please Login to comment...