View All C Programs

 Previews Example  View All C Programs Next Example 

Program to find power of number in C

This program to find power of number. This is one of the basic c program.As power operator i.e "^" cannot be used directly as C language does not support this function, So we can calculate power of two numbers in a very easy way. Another way to calculate power is to call built in function called pow(double,double), stored in math.h header file, and it will return a double value.
#include <stdio.h>
#include <conio.h>

int  power(int ,int);

int main()
{
     int result,b,c;
     printf("Enter the number\n");
     scanf("%d",&b);
     printf("Enter the power\n");
     scanf("%d",&c);

     result=power(b,c);
     printf("Value of %d^%d = %d",b,c,result);
     getch();
}

int power(int c, int d)
{
     int pow=1;
     int i=1;
     while(i<=d)
     {
          pow=pow*c;
          i++;
     }
     return pow;
}
    
Result
Enter the number
5
Enter the power
2
Value of 5^3 = 25

Program to find power of two numbers using built in functions in C

#include <stdio.h>
#include <conio.h>
#include <math.h>

int main()
{
    double a;
    a=pow(2.0,3.0);
    printf("Value of 2^2 = %f",a);
    getch();
}
Result
Value of 2^2 = 4