neon number
☰Fullscreen
Table of Content:
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.
// 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 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.