write a c program to find the power of any number
Answers
Explanation:
#include <math.h>
#include <stdio.h>
int main() {
double base, exp, result;
printf("Enter a base number: ");
scanf("%lf", &base);
printf("Enter an exponent: ");
scanf("%lf", &exp);
// calculates the power
result = pow(base, exp);
printf("%.1lf^%.1lf = %.2lf", base, exp, result);
return 0;
}
PROGRAM
#include <stdio.h>
void main()
{
int i,num,power,product;
product = 1;
printf("Enter the number at the base:\n");
scanf("%d",&num);
printf("Enter the number at the exponent:\n");
scanf("%d",&power);
i = 1;
while(i <= power)
{product = product*num;
i++;}
printf(" %d raised to power %d is %d.",num,power,product);}
Understanding the SYNTAX :-
- While loop has been used to get the required product.This loop is iterated power times.
Each time while traversing through the while loop, i is increased by 1. I have used post increment operator to increase the value of i.