Pages

Monday

C Program to Find Power of a Number x^n | C Program

Here's a C program to find power of a number i.e. x^n or x to the power n using while loop.


# include <stdio.h>
# include <conio.h>
void main() 
{ 
 int x, n, count = 1, pow = 1 ; 
 clrscr() ; 
 printf("Enter the value of x : ") ; 
 scanf("%d", &x) ; 
 printf("\nEnter the value of n : ") ; 
 scanf("%d", &n) ; 
 while(count <= n) 
 { 
  pow = pow * x ; 
  count ++ ; 
 } 
 printf("\nThe power of %d^%d is : %d", x, n, pow) ; 
 getch() ; 
} 

Output of above program is

Enter the value of x : 2
Enter the value of n : 4
The power of 2^4 is : 16


Explanation of above program

Variable description -
  • x - is a number whose power we're calculating.
  • n - is the power or the number of times the number x is multiplied with itself i.e. x ^ n.
  • pow - is the final result i.e. x ^ n. It is initialized to 1.
  • count - is a temporary counter to keep track of how many times x is multiplied with itself.

First we ask the user to enter the values of x and n. Then using while loop we're calculating the power i.e. x ^ n. While loop will run as long as count <= n. Inside while loop we're multiplying value of x with the pow variable and again storing this value in pow variable.

Suppose, n = 4 and x = 2 i.e. we want to calculate 2 ^ 4 or 2 * 2 * 2 * 2. The process of while loop in this case -

count <= n i.e. 1 <= 4 - 
  • pow = pow * x = 1 * 2 = 2
  • count++ = count = 2.

count <= n i.e. 2 <= 4 - 
  • pow = pow * x = 2 * 2 = 4
  • count++ = count = 3.

count <= n i.e. 3 <= 4 - 
  • pow = pow * x = 4 * 2 = 8
  • count++ = count = 4.

count <= n i.e. 4 <= 4 - 
  • pow = pow * x = 8 * 2 = 16
  • count++ = count = 5.
 Now count > n i.e. 5 > 4. So the loop terminates. The final value is printed using the printf().
Suggestion: This is good way to understand how to calculate power of a number but there is a simple way to do this process. In C there is a built-in function pow() which does the same thing. Take a look at this program which is a variation of above program and uses C's pow() function to calculate the power of a number. C Program to Find Power of a Number using pow() Function

3 comments:

  1. We can use recursion to calculate power of a number because it follows recursive sub-problem structure. This approach reduces the problem of finding an to problem of finding an-1 till exponent becomes 0.
    Time complexity of this program is O(n).
    power(a, b) = a * power(a, b-1);
    power of number using recursion

    ReplyDelete
  2. Hello There,

    Grazie! Grazie! Grazie! Your blog is indeed quite interesting around ! C Program to Find Power of a Number x^n | C Program I agree with you on lot of points!

    Getting memory violation error when i use string function "strlwr", what wrong in step 3, please help, I am new to c language

    char *a="SAM";
    char *c;
    c=strlwr(a);
    printf("%s",c);

    Very useful article, if I run into challenges along the way, I will share them here.

    Kind Regards,
    Preethi

    ReplyDelete