Introduction
In this lab, you will learn how to compute powers and exponents in C programming. The lab covers two main steps: reading the base and exponent values from the user, and then calculating the power using either a manual loop or the built-in pow() function. By the end of this lab, you will have a solid understanding of how to perform power calculations in C, which is a fundamental operation in many mathematical and scientific applications.
Read Base and Exponent
In this step, you will learn how to read the base and exponent values for calculating powers in a C program. We'll create a simple program that prompts the user to input these values.
First, let's create a new C file in the ~/project directory:
cd ~/project
nano power_calculation.c
Now, enter the following code to read base and exponent:
#include <stdio.h>
int main() {
int base, exponent;
// Prompt user to enter base
printf("Enter the base number: ");
scanf("%d", &base);
// Prompt user to enter exponent
printf("Enter the exponent: ");
scanf("%d", &exponent);
// Print the entered values
printf("Base: %d\n", base);
printf("Exponent: %d\n", exponent);
return 0;
}
Let's compile and run the program:
gcc power_calculation.c -o power_calculation
./power_calculation
Example output:
Enter the base number: 5
Enter the exponent: 3
Base: 5
Exponent: 3
Code Explanation:
scanf()function is used to read integer input from the user%dis the format specifier for integers&baseand&exponentpass the memory addresses where input values will be stored- The program simply reads and displays the base and exponent values
This step sets up the foundation for calculating powers by first obtaining the necessary input from the user.
Use a Loop or pow() Function
In this step, you will learn two methods to calculate powers in C: using a manual loop and using the built-in pow() function from the math library.
First, let's modify the previous power_calculation.c file to implement power calculation:
cd ~/project
nano power_calculation.c
Method 1: Using a Loop
#include <stdio.h>
int calculate_power_loop(int base, int exponent) {
int result = 1;
for (int i = 0; i < exponent; i++) {
result *= base;
}
return result;
}
int main() {
int base, exponent;
printf("Enter the base number: ");
scanf("%d", &base);
printf("Enter the exponent: ");
scanf("%d", &exponent);
int power_result = calculate_power_loop(base, exponent);
printf("%d raised to the power of %d is: %d\n", base, exponent, power_result);
return 0;
}
Method 2: Using pow() Function
#include <stdio.h>
#include <math.h>
int main() {
int base, exponent;
printf("Enter the base number: ");
scanf("%d", &base);
printf("Enter the exponent: ");
scanf("%d", &exponent);
// Note: pow() returns a double, so we cast to int
int power_result = (int)pow(base, exponent);
printf("%d raised to the power of %d is: %d\n", base, exponent, power_result);
return 0;
}
Compile the program with the math library:
gcc power_calculation.c -o power_calculation -lm
Example output:
Enter the base number: 2
Enter the exponent: 3
2 raised to the power of 3 is: 8
Code Explanation:
- Loop method manually multiplies the base by itself
exponenttimes pow()function frommath.hprovides a built-in power calculation-lmflag is required to link the math library when compiling- We cast
pow()result tointto match our integer calculation
Print the Result
In this step, you will enhance the power calculation program to provide more detailed and formatted output, demonstrating different ways of presenting results in C.
Let's modify the power_calculation.c file:
cd ~/project
nano power_calculation.c
Add the following comprehensive code:
#include <stdio.h>
#include <math.h>
void print_power_result(int base, int exponent, int result) {
// Basic print format
printf("Basic Result: %d^%d = %d\n", base, exponent, result);
// Formatted print with alignment
printf("Formatted Result: %2d raised to the power of %2d equals %5d\n",
base, exponent, result);
// Scientific notation for larger numbers
printf("Scientific Notation: %d^%d = %.2e\n", base, exponent, pow(base, exponent));
}
int calculate_power_loop(int base, int exponent) {
int result = 1;
for (int i = 0; i < exponent; i++) {
result *= base;
}
return result;
}
int main() {
int base, exponent;
printf("Enter the base number: ");
scanf("%d", &base);
printf("Enter the exponent: ");
scanf("%d", &exponent);
int power_result = calculate_power_loop(base, exponent);
// Call function to print results
print_power_result(base, exponent, power_result);
return 0;
}
Compile the program:
gcc power_calculation.c -o power_calculation -lm
Run the program:
./power_calculation
Example output:
Enter the base number: 5
Enter the exponent: 3
Basic Result: 5^3 = 125
Formatted Result: 5 raised to the power of 3 equals 125
Scientific Notation: 5^3 = 1.25e+02
Code Explanation:
print_power_result()demonstrates multiple printing formats- Basic print shows simple calculation
- Formatted print uses width specifiers for alignment
- Scientific notation useful for large numbers
%.2edisplays number in scientific format with 2 decimal places
Summary
In this lab, you learned how to read the base and exponent values from the user, and then calculate the power using two different methods: a manual loop and the built-in pow() function. You implemented the loop-based power calculation function and the pow() function call, and then compared the results. This allowed you to understand the different approaches to computing powers and exponents in C programming.



