1// power function in c without pow
2#include <stdio.h>
3double mypow(double base, double power);
4int main()
5{
6
7 int base, exponent;
8 printf("Enter Base: ");
9 scanf("%d", &base);
10 printf("Enter Exponent: ");
11 scanf("%d", &exponent);
12 printf("%d raised to the power %d = %0.2lf\n", base, exponent, mypow(base, exponent));
13 return 0;
14}
15double mypow(double base, double power)
16{
17 double result = 1;
18 if (power >= 0)
19 {
20
21 while (power--)
22 {
23 result = result * base;
24 }
25
26 return result;
27 }
28 else if (power < 0)
29 {
30 while (power++)
31 {
32 result = result * base;
33 }
34 return 1 / result;
35 }
36}
1#include <stdio.h>
2int main() {
3 int base, exp;
4 long long result = 1;
5 printf("Enter a base number: ");
6 scanf("%d", &base);
7 printf("Enter an exponent: ");
8 scanf("%d", &exp);
9
10 while (exp != 0) {
11 result *= base;
12 --exp;
13 }
14 printf("Answer = %lld", result);
15 return 0;
16}
1The function pow() is used to calculate the power raised
2to the base value. It takes two arguments. It returns the
3power raised to the base value. It is declared in
4“math.h” header file.