lucproblem015.c (1290B)
1 /* Write a program that defines a function that calculates power of one 2 number reaised to another and factorial value of a number in one cell. */ 3 /* Let Us C, Chap - 9, Page 159, Problem 9.2 */ 4 5 #include <stdio.h> 6 7 void bothPowerFactorial(double, int, int, double *, long long *); 8 9 int main() 10 { 11 double a, resultPower; 12 int b, factN; 13 long long resultFactorial; 14 printf("Enter a and b for calculating a raised to b: "); 15 scanf("%lf %d", &a, &b); 16 printf("Enter number to calculate the factorial: "); 17 scanf("%d", &factN); 18 if (b < 0 || factN < 0) 19 { 20 printf("\nOnly non-negative integer is allowed as input of b and factorial."); 21 return 1; 22 } 23 bothPowerFactorial(a, b, factN, &resultPower, &resultFactorial); 24 printf("\n%g Raised to %d: %g" 25 "\nFactorial of %d: %lld", 26 a, b, resultPower, factN, resultFactorial); 27 return 0; 28 } 29 30 void bothPowerFactorial(double a, int b, int n, double *resultPower, long long *resultFactorial) 31 { 32 double tempResultPower = 1; 33 long long tempResultFactorial = 1; 34 int i; 35 36 for (i = 1; i <= b; i++) 37 tempResultPower *= a; 38 39 for (i = 1; i <= n; i++) 40 tempResultFactorial *= i; 41 42 *resultPower = tempResultPower; 43 *resultFactorial = tempResultFactorial; 44 }