In this post, we will learn how to find neon numbers using the C Programming language. But before that, let’s learn about neon numbers.
A number is said to be a neon number if the sum of the digits of the square of the number is equal to the same number. For example, 9 is a neon number because 9 * 9 = 81 and the sum of 81 is 8 + 1 = 9 which is equal to the original number.
We will use following approaches to find neon number:
- Using For Loop
- Using While Loop
So, without further ado, let’s begin this tutorial.
C Program to Find Neon Number Using For Loop
// C Program to Find Neon Number Using For Loop #include <stdio.h> #include <math.h> int main(){ int num, sqr, rem, sum; // Asking for input printf("Enter a number: "); scanf("%d", &num); // Finding the square root sqr = pow(num, 2); // Finding the sum for (sum = 0; sqr > 0; sqr /= 10){ rem = sqr % 10; sum = sum + rem; } // Checking whether sum is equals to original number if (sum == num){ printf("%d is a neon number.", num); } else{ printf("%d is not a neon number.", num); } return 0; }
Output
Enter a number: 9
9 is a neon number.
How Does This Program Work ?
int num, sqr, rem, sum;
In this program, we have declared four integer data type variables named num, sqr, rem and sum.
// Asking for input printf("Enter a number: "); scanf("%d", &num);
Now, the user is asked to enter a number.
// Finding the square root sqr = pow(num, 2);
We find the square of the entered number using the pow() function.
// Finding the sum for (sum = 0; sqr > 0; sqr /= 10){ rem = sqr % 10; sum = sum + rem; }
Now, we calculate the sum of the digits of the square number using for loop. Suppose, the entered number is 9, then it’s square will be 81. Therefore,
1st For Loop Iteration: for (sum = 0; 81 > 0; 81 /= 10)
rem = sqr % 10 = 81 % 10 = 1
sum = sum + rem = 0 + 1 = 1
2nd For Loop Iteration: for (sum = 1; 8 > 0; 8 /= 10)
rem = sqr % 10 = 8 % 10 = 8
sum = sum + rem = 1 + 8 = 9
We get sum = 9.
// Checking whether sum is equals to original number if (sum == num){ printf("%d is a neon number.", num); } else{ printf("%d is not a neon number.", num); }
After that, we check whether the sum is equal to the original number. If the condition is True, then the entered number is a neon number otherwise it’s not a neon number.
C Program to Find Neon Number Using While Loop
// C program to Find Neon Number #include <stdio.h> int main(){ int num, rem, sqr, sum = 0; // Asking for input printf("Enter a number: "); scanf("%d", &num); // Calculating square sqr = num * num; while (sqr != 0){ rem = sqr % 10; sum = sum + rem; sqr = sqr / 10; } // Checking for Neon Number if (sum == num){ printf("%d is a neon number.", num); } else{ printf("%d is not a neon number.", num); } return 0; }
Output 1
Enter a number: 9
9 is a neon number.
Output 2
Enter a number: 7
7 is not a neon number.
Conclusion
I hope after going through this post, you understand how to find neon numbers using C Programming language.
If you have any doubt regarding the program, then contact us in the comment section. We will be delighted to assist you.
Also Read: