power function in c without pow code example

Example 1: Write a Program to find exponential without using pow() method

#include <stdio.h>
int main() {
    int base, exp;
    long long result = 1;
    printf("Enter a base number: ");
    scanf("%d", &base);
    printf("Enter an exponent: ");
    scanf("%d", &exp);

    while (exp != 0) {
        result *= base;
        --exp;
    }
    printf("Answer = %lld", result);
    return 0;
}

Example 2: power func in c

The function pow() is used to calculate the power raised 
to the base value. It takes two arguments. It returns the
power raised to the base value. It is declared in 
“math.h” header file.

Example 3: pow not working c

gcc ... -lm ...
#include <math.h>

Tags:

C Example