C Program to Calculate the Power of a Number

Example on how to calculate the power of a number if the exponent is an integer. Also, you will learn to compute the power using pow() function.



The program below takes two integers from the user (a base number and an exponent) and calculates the power.
For example: In case of 23
  • 2 is the base number
  • 3 is the exponent
  • And, the power is equal to 2*2*2

Example #2: Power Using pow() Function

#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
    int base, exponent, result;
    printf("Enter a base number: ");
    scanf("%d", &base);
    printf("Enter an exponent: ");
    scanf("%d", &exponent);
    // calculates the power
    result = pow(base, exponent);
    printf("%d^%d = %d", base, exponent, result);
printf("\nSubscribe My Channel");
    return 0;
getch();
}

Output

Enter the value of base = 2
Enter the value of Exponent= 2
2^2 = 4

Click on the download button to download file


download button

Post a Comment

Thanks For Your Comment. We will reply you as soon as possible ...

 
COOL PROGRAMMING 4 U © 2013. All Rights Reserved. Shared by Cp4U
Top