C program to find power of a number using loop
Learn how to write a C program to find the power of a number. This article provides a detailed explanation and sample code for calculating both positive and negative exponents.
Calculating the power of a number is a fundamental concept in mathematics and programming. This article will guide you through writing a C program to compute the power of a given base raised to a given exponent, providing a detailed explanation and sample code.
Understanding the Problem
The power of a number can be expressed as base^exponent, which means multiplying the base by itself exponent times. For example, 2^3 is 2 * 2 * 2 which equals 8.
Steps to Calculate Power of a Number
- Input the Base and Exponent: Read the base and exponent from the user.
- Initialize the Result: Start with a result of 1.
- Multiply the Base by Itself Exponent Times: Use a loop to perform the multiplication.
- Print the Result: Output the final result.
Write a C program to find power of a number using loop
Here's a C program to find the power of a number:
#include <stdio.h>
int main() {
int base, exponent;
long long result = 1;
// Input the base and exponent
printf("Enter base: ");
scanf("%d", &base);
printf("Enter exponent: ");
scanf("%d", &exponent);
// Calculate the power
for (int i = 0; i < exponent; i++) {
result *= base;
}
// Print the result
printf("%d^%d = %lld", base, exponent, result);
return 0;
}
Output
Enter base: 2
Enter exponent: 4
2^4 = 16
Explanation
- Input the Base and Exponent: The program prompts the user to enter the base and exponent. The
scanffunction reads these values and stores them in the variablesbaseandexponent. - Initialize the Result: The result is initialized to 1, as any number raised to the power of 0 is 1.
- Calculate the Power:
- A
forloop runs from 0 toexponent - 1. - In each iteration,
resultis multiplied bybase. This effectively multiplies the base by itself exponent times.
- A
- Print the Result: The program uses the
printffunction to display the result in the formatbase^exponent = result.
Detailed Steps
- Step 1: Input the Base and Exponent
- Use
printfto prompt the user for input. - Use
scanfto read the base and exponent from the user.
- Use
- Step 2: Initialize the Result
- Declare a variable
resultand initialize it to 1.
- Declare a variable
- Step 3: Calculate the Power
- Use a
forloop to iterate from 0 toexponent - 1. - In each iteration, multiply
resultbybase.
- Use a
- Step 4: Print the Result
- Use
printfto display the result in the formatbase^exponent = result.
- Use
Example
For an input of base 2 and exponent 3, the program will:
- Initialize
resultto1. - Iterate three times (exponent is 3):
- First iteration:
result = 1 * 2(result becomes 2) - Second iteration:
result = 2 * 2(result becomes 4) - Third iteration:
result = 4 * 2(result becomes 8)
- First iteration:
- Print
2^3 = 8.
For an input of base 5 and exponent 0, the program will:
- Initialize
resultto1. - Since the exponent is 0, the loop does not execute.
- Print
5^0 = 1.
Handling Negative Exponents
The basic program provided above does not handle negative exponents. In mathematics, a negative exponent represents the reciprocal of the base raised to the positive exponent. For example, 2^-3 is 1 / (2^3) which equals 1/8.
To handle negative exponents, we need to modify the program:
#include <stdio.h>
int main() {
int base, exponent;
double result = 1.0;
// Input the base and exponent
printf("Enter base: ");
scanf("%d", &base);
printf("Enter exponent: ");
scanf("%d", &exponent);
// Calculate the power
int positiveExponent = exponent < 0 ? -exponent : exponent;
for (int i = 0; i < positiveExponent; i++) {
result *= base;
}
// If the exponent is negative, take the reciprocal of the result
if (exponent < 0) {
result = 1.0 / result;
}
// Print the result
printf("%d^%d = %lf", base, exponent, result);
return 0;
}
Output
Enter base: 2
Enter exponent: -2
2^-2 = 0.250000
Explanation for Handling Negative Exponents
- Change Result Type: The result variable is changed to
doubleto handle fractional results. - Calculate Absolute Exponent: Use the absolute value of the exponent for the loop.
- Reciprocal for Negative Exponent: If the original exponent is negative, take the reciprocal of the result after the loop.
We can also find power a number using built-in function pow
Find power a number using pow