c language program to calculate the power of a number using a for loop and a while loop.

Examples
3^3 =27. Here, 3 is actually multiplied to itself 3 times. i.e. 3*3*3 = 27.
4^3 =64. Here,4 is being multiplied to itself 3 times. i.e. 4*4*4*4 = 256.

Problem Description
Here in this article we are going to Write a C Program which calculates the power of any number, by accepting the base and exponent numbers from the user.
Solution of the problem
1. Take the base and exponent of two numbers as input.
2. Run a loop starting from the index value 1 to the exponent value.
3. At each iteration of the loop, we multiply the base number by itself.
4. Print the final result and exit.


1. c program with source code to find the power of a number using a for loop. The program is fruitfully tested and compiled

#include<stdio.h>
/* * program in c language to find base to the power of any number
two number using for loop
*/
int main()
{
 int exponent,base;
 float power = 1;
 int i;
 /* Accept and store base and exponent as input from user*/
 printf("Enter base:\n ");
 scanf("%d",&base);
 printf("Enter exponent:\n");
 scanf("%d",&exponent);
 int exp1 = exponent;
 //calculate the power of negative exponents
 for(i=exp1;i<0;i++)
 {
  power = power/base;
 }
 //calculate the power of positive exponents
 for(i=exp1;i>0;i--)
 {
  power = power * base;
 }
 printf("%d to the power %d is : %f", base, exponent, power);
 return 0;
}

Output
Enter base.
2
Enter Exponent
3
2 to the power 3 is : 8
Explanation
1. To calculate the power of a number using a loop, we need to receive two inputs from the user the base and the power. We must set the power variable to 1.
2. The power variable stores the final power value.
3. If the exponent is negative, run a for loop of i = expo1 until i is less than 0. For each iteration, calculate the power by dividing the power by the base and increasing the value of i by 1.
4. If the exponent is positive, run a for loop from i = expo1 until i is greater than 0. For each iteration, calculate the power by multiplying the power by the base and decrementing i by 1.
5. Print the power value after exiting the loop.


2. C Program to find the power of a number using a while loop.

/*
* C program source code to find power of any given number using while loop
*/
#include<stdio.h>
int main()
{
  int base, exponent;
 float power = 1;
  /* Take base and exponent as input*/
 printf("Enter base: \n");
 scanf("%d", &base);
 printf("Enter exponent:\n ");
  scanf("%d", &exponent);
 int expo = exponent;
 //calculates the power of negative exponents number
 while(expo < 0)
 {
  power=power/base;
  expo++;
  }
 //calculates the power of positive exponents number
 while(expo>0)
 {
  power=power*base;
  expo--;
 }
 printf("%d^%d =%f", base,exponent,power);
 return 0;
}

Output
Enter base.
2
Enter Exponent
3
3 to the power 3 is : 27
Explanation
1. To calculate the power of a number using a loop, we need to receive two input values from the user the base and the power. We must initialize the power variable to 1.
2. The power variable stores the final power value.
3. If the exponent is negative, execute a while loop until the value of the exponent is less than 0.
For each iteration, calculate the power by dividing the power by the base and increment the exponent by 1.
4. If the exponent is positive, execute the while loop until the exponent value exceeds 0.
In each iteration, calculate the power by multiplying the power by the base and subtracting the power value by 1.
5. Print the power value after exiting the loop


Previous :-->> 6.Write a program in c language to check if a given number is a palindrome or not using a do while loop.
 -->> NEXT: 8.Write a program in c to check if a given number is an Armstrong number or not using a while loop.
-->>ALL Loops Assignments in c