Split a number and then add all factorial digit
3. Take 3 digit number from user. Split the digits of the number using a user defined function splitter(). Find the factorial of each digit using a user defined function find_factorial(). Finally, print the sum of the factorials using anther user defined function calculate_sum().
Sample Output
Input Number: 123 Output : 9 Process returned 0 (0x0) execution time : 1.911 s Press any key to continue.
Source Code
#include<stdio.h> int arr[100], factorialSum = 0; int calculate_sum(int n) { factorialSum = factorialSum + n; } int find_factorial(int f) { int fact = 1; for(int i = 1; i <= f; i++) { fact = fact * i; } calculate_sum(fact); } int splitter(int number) { int i = 0, digit = 0; while(number != 0) { arr[i++] = number % 10; number = number/10; digit++; } return digit; } int main() { int number, digit; printf("Input Number: "); scanf("%d", &number); digit = splitter(number); for(int i = 0; i < digit; i++) { find_factorial(arr[i]); } printf("Output : %d\n\n", factorialSum); return 0; }
Sample Output
Input Number: 125 Output : 123 Process returned 0 (0x0) execution time : 5.352 s Press any key to continue.
No comments:
Post a Comment