📜  霓虹灯编号

📅  最后修改于: 2021-04-26 09:14:51             🧑  作者: Mango

霓虹数字是一个数字,该数字的平方的总和等于该数字。任务是检查并打印范围内的霓虹灯编号。

例子:

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.

实现很简单,我们首先计算给定数字的平方,然后求出平方中的位数。

C++
// C/C++ program to check and print
// Neon Numbers upto 10000
#include 
using namespace std;
#include 
  
int checkNeon(int x)
{
    // storing the square of x
    int sq = x * x;
  
    // calculating the sum of sum of digits
    // of sq
    int sum_digits = 0;
    while (sq != 0) {
        sum_digits = sum_digits + sq % 10;
        sq = sq / 10;
    }
    return (sum_digits == x);
}
  
// Driver Code
int main(void)
{
    // Printing Neon Numbers upto 10000
    for (int i = 1; i <= 10000; i++) 
        if (checkNeon(i))
            cout << i << " ";    
}


Java
// Java program to check and print
// Neon Numbers upto 10000
import java.io.*;
  
class GFG {
    // function to check Neon Number
    static boolean checkNeon(int x)
    {
        // storing the square of x
        int sq = x * x;
  
        // calculating the sum of sum of digits
        // of sq
        int sum_digits = 0;
        while (sq != 0) {
            sum_digits = sum_digits + sq % 10;
            sq = sq / 10;
        }
        return (sum_digits == x);
    }
  
    // Driver Code
    public static void main(String args[])
                        throws IOException
    {
        // Printing Neon Numbers upto 10000
        for (int i = 1; i <= 10000; i++) 
            if (checkNeon(i))
                System.out.print(i + " ");        
    }
}
// This code is contributed by Nikita Tiwari.


Python
# Python program to check and print 
# Neon Numbers upto 10000
  
# function to check Neon Number
def checkNeon (x) :
    # storing the square of x
    sq = x * x
      
    # calculating the sum of sum of digits
    # of sq
    sum_digits = 0
    while (sq != 0) :
        sum_digits = sum_digits + sq % 10
        sq = sq / 10
      
    return (sum_digits == x)
  
# Driver Code
  
i = 1
# Printing Neon Numbers upto 10000
while i <= 10000 :
    if (checkNeon(i)) :
        print i,
    i = i + 1
  
# This code is contributed by Nikita Tiwari.


C#
// C# program to check and print
// Neon Numbers upto 10000
using System;
  
class GFG 
{
    // function to check Neon Number
    static bool checkNeon(int x)
    {
        // storing the square of x
        int sq = x * x;
  
        // calculating the sum of sum of digits
        // of sq
        int sum_digits = 0;
        while (sq != 0) 
        {
            sum_digits = sum_digits + sq % 10;
            sq = sq / 10;
        }
        return (sum_digits == x);
    }
  
    // Driver Code
    public static void Main()
                          
    {
        // Printing Neon Numbers upto 10000
        for (int i = 1; i <= 10000; i++) 
            if (checkNeon(i))
                Console.Write(i + " "); 
    }
}
  
// This code is contributed by vt_m.


PHP


输出:

1 9