This article is to check for neon numbers in C programming. We will two different examples in C for the Neon number.
- Check if the entered number is Neon number or not in C.
- Display all the Neon Number within a given range in C.
Neon Numbers
A number is said to be a neon number if the sum of the digits of a square of the number is equal t the original number.
Example: 9 is a Neon number (see explanation in the diagram).
C Program to Check the Number is a Neon Number or Not
The following program takes the user input for a number that the user wants to check for. Then square the number sum the digits using while loop but you can also sum the digits using for the loop, using the following code: for(i = square; i > 0; i = i/10) { sum = sum + i % 10; }
instead of a while loop. And lastly, print the result accordingly to print the neon number.
Source Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include <stdio.h> int main() { int num, square, i, sum = 0; printf("Enter the number you want to check for Neon No.: "); scanf("%d", &num); //Calculating the square of the No. square = num * num; i = square; //Adding the digits of square while (i > 0) { sum += i % 10; i = i / 10; } //Check for original Number and display if (sum == num) printf("The entered number %d is a neon number.", num); else printf("The entered number %d is NOT a neon number.", num); return 0; } |
The output of neon number in c programming.
C Program to print all the Neon Number within a Range
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #include <stdio.h> int isNeon(int num) { int sum = 0; int square = num * num; while (square != 0) { sum += square % 10; square /= 10; } return (sum == num); } int main() { int lrRange = 0, upRange = 0, i; //user inputs for max and min value printf("Enter lower range: "); scanf("%d", &lrRange); printf("Enter the upper range: "); scanf("%d", &upRange); // check number printf("\nAll the Neon numbers between %d to %d are: ", lrRange, upRange); for (i = lrRange; i <= upRange; i++) { //calling function by passing i if (isNeon(i)) printf("%d ", i); } return 0; } |
Output:
Enter lower range: 0
Enter the upper range: 10000
All the Neon numbers between 0 to 10000 are:
0 1 9