Write a c program to check a number is a neon number or not using the for loop. If the number equals the sum of digits of the square of a number, it is a neon number.
- We use the math pow function to find the square of a number.
- Divide the output into individual digits and calculate the sum of it.
- The if else to check whether the sum of digits in a square equals the actual number. If true, it is a neon number.
#include <stdio.h> #include<math.h> int main() { int Number, squr, rem, Sum; printf("Enter Number to Check = "); scanf("%d", &Number); squr = pow(Number, 2); for (Sum = 0; squr > 0; squr = squr / 10) { rem = squr % 10; Sum = Sum + rem; } if (Number == Sum) printf("\n%d is a Neon Number.\n", Number); else printf("\n%d is not.\n", Number); }
This C program checks whether the given number is a neon number or not using a while loop.
#include <stdio.h> #include<math.h> int main() { int Number, squr, rem, Sum = 0; printf("Enter Number to Check = "); scanf("%d", &Number); squr = pow(Number, 2); printf("The Square of a Number %d = %d\n", Number, squr); while(squr > 0) { rem = squr % 10; Sum = Sum + rem; squr = squr / 10; } printf("The Sum of Digits in a Square = %d\n", Sum); if (Number == Sum) printf("\n%d is a Neon Number.\n", Number); else printf("\n%d is not.\n", Number); }
Enter Number to Check = 9
The Square of a Number 9 = 81
The Sum of Digits in a Square = 9
9 is a Neon Number.
In this check neon number example, the squareDigitsSum recursive function divide and find the sum by calling it recursively.
#include <stdio.h> #include<math.h> int squareDigitsSum(int num) { static int rem, sum = 0; if(num > 0) { rem = num % 10; sum = sum + rem; squareDigitsSum(num / 10); } return sum; } int main() { int Number; printf("Enter Number to Check = "); scanf("%d", &Number); int squr = pow(Number, 2); int Sum = squareDigitsSum(squr); printf("The Square of %d Number = %d\n", Number, squr); printf("The Sum of Digits in %d = %d\n", squr, Sum); if (Number == Sum) printf("\n%d is a Neon Number.\n", Number); else printf("\n%d is not.\n", Number); }
Enter Number to Check = 5
The Square of 5 Number = 25
The Sum of Digits in 25 = 7
5 is not.