C program to calculate power of a number

Power of a number tells how many times the number must multiply itself. for example: 2 power 3 tells that 2 should be multiplied 3 times: 2 * 2 * 2 = 8.

Output:


Enter the number: 5

Enter the power: 2
The result of 5 raised to power 2 is 25

Implementation:


/* Using In-Built Function pow() in math.h */


#include <stdio.h>

int main(void) {
 int n,p,res;
 printf("Enter the number:\n");
 scanf("%d",&n);
 printf("Enter the power:\n");
 scanf("%d",&p);
 
 /* calculating the power of the entered number
    i.e., finding (n)^p 
    For example:
      2^3 = 8
      5^2 = 25
      */
 /* using inbuilt function */
 res = pow(n,p);
 
 /* printing the result */
 printf("The result of %d raised to power %d is %d",n,p,res);
 return 0;
}


/* Without Using In-Built Function pow() */


#include <stdio.h>

int main(void) {
 int n,p,res,i;
 printf("Enter the number:\n");
 scanf("%d",&n);
 printf("Enter the power:\n");
 scanf("%d",&p);
 
 /* calculating the power of the entered number
    i.e., finding (n)^p 
    For example:
      2^3 = 8
      5^2 = 25
      */
 res = 1;
 for(i=1;i<=p;i++)
 {
  res *= n;
 }
 
 /* printing the result */
 printf("The result of %d raised to power %d is %d",n,p,res);
 return 0;
}