Write a C program to print all strong numbers between 1 to N.
Wap in C to find strong numbers between 1 to 100.
Required Knowledge
- C printf and scanf functions
- For loop in C
- Factorial program in C
A number is a strong number if the sum of factorial of digits is equal to number itself.
For Example: 145 is a strong number. !1 + !4 + !5 = 145
C program to print all strong numbers between 1 to N
#include <stdio.h> #include <conio.h> int main(){ int N, num, temp, digit, nFactorial, counter, factSum; printf("To find all strong numbers between 1 to N"); printf("Enter value of N"); scanf("%d",&N); printf("List of strong numbers between 1 to %d\n", N); for(num = 1; num <= N; num++){ /* Calculate sum of factorial of digits of num */ temp = num; factSum = 0; while(temp){ digit = temp%10; /* Calculate factorial of every digit * N! = N*(N-1)*(N-2)*(N-3)*.....*3*2*1 */ for(counter=1, factorial=1; counter<=digit; counter++){ factorial = factorial * counter; } factSum += factorial; temp = temp/10; } if(factSum == num) printf("%d ", num); } getch(); return 0; }
Output
To find all strong numbers between 1 to N Enter value of N 1000 List of strong numbers between 1 to 1000 1 2 145