bsc

Comprehensive codebase and coursework for the 4-year B.Sc. Computer Science Honours degree (WBSU)

git clone https://github.com/notamitgamer/bsc.git
· rebuilt hourlyOpen on GitHub
root / semester_1 / letusc / lucproblem012.c

lucproblem012.c (589B)


      1 /* Write a Function power(a, b), to calculate the value of a raised to b */
      2 /* Let Us C, Chap - 8, Page - 141, Problem 8.2 */
      3 
      4 #include <stdio.h>
      5 
      6 double power(double, int);
      7 
      8 double power(double a, int b)
      9 {
     10     if (b == 0)
     11         return 1;
     12     double res = 1;
     13     int i;
     14     if (b > 0)
     15         for (i = 1; i <= b; i++)
     16             res *= a;
     17     return res;
     18 }
     19 
     20 int main()
     21 {
     22     double a, result;
     23     int b;
     24     printf("Enter the value and the power (Format A^B) : ");
     25     scanf("%lf^%d", &a, &b);
     26     result = power(a, b);
     27     printf("Result of %g^%d = %g", a, b, result);
     28     return 0;
     29 }