To check strong number in c program
Strong number
The sum of the individual digit factorial is equal to the number itself is known as a strong number.For example, 145 is a strong number.
Here is the code of the strong number.
code:
#include<stdio.h>
int factorial(int); // declaration of factorial function
int factorial(int i)
{
int factorial= 1;
int fact = 1;
for (fact = 1; fact <= i; fact++)
factorial = factorial*fact;
return(factorial);
}
int main()
{
int num = 0; //to store user input
int digit1 = 0;
int temp = 0;
int digit2 = 0;
int digit3 = 0;
int strong = 0;
char choice ; // variable to continue or break
//This loop will continue till the user pressing y and othewise it will break
while (1){
printf("Enter the number, to check whether it is strong number or not : ");
scanf("%d", &num);
digit1 = num / 100;
temp = num % 100;
digit2 = temp / 10;
digit3 = temp % 10;
strong = factorial(digit1) + factorial(digit2) + factorial(digit3);
if (strong == num)
printf("\nAnd this (%d) is strong number.", num);
else if (strong != num)
printf("\nAnd this (%d) is not strong number.", num);
printf("\nIf you want to continue, press y : ");
scanf(" %c", &choice);
if (choice == 'y' || choice == 'Y')
continue;
else
break;
}
getch();
return 0;
}
Output:
Enter the number, to check whether it is strong number or not : 145
And this (145) is strong number.
If you want to continue, press y : y
Enter the number, to check whether it is strong number or not : 131
And this (131) is not strong number.
Comments
Post a Comment