Introduction
Calculating the power of a number involves raising a base number to an exponent. For example, ( 2^3 = 2 \times 2 \times 2 = 8 ). This guide will show you how to write a C program to calculate the power of a number using recursion.
Example:
- Input: base = 2, exponent = 3
- Output: 8 (since ( 2^3 = 8 ))
Problem Statement
Create a C program that:
- Takes a base number and an exponent as input from the user.
- Uses a recursive function to calculate the power of the base raised to the exponent.
- Displays the result.
Solution Steps
- Include the Standard Input-Output Library: Use
#include <stdio.h>
for standard input-output functions. - Write the Recursive Function: Define a recursive function that calculates the power of a number.
- Write the Main Function: Define the
main
function to take user input, call the recursive function, and display the result. - Input the Base and Exponent: Use
scanf
to take input from the user for the base and exponent. - Call the Recursive Function: Pass the base and exponent to the recursive function to calculate the power.
- Display the Result: Use
printf
to display the calculated power.
C Program to Calculate Power of a Number Using Recursion
#include <stdio.h> // Step 2: Define the recursive function to calculate power int power(int base, int exponent) { if (exponent == 0) { return 1; // Base case: Any number raised to the power of 0 is 1 } else { return base * power(base, exponent - 1); // Recursive case } } int main() { // Step 3: Declare variables to hold the base and exponent int base, exponent; // Step 4: Prompt the user to enter the base and exponent printf("Enter base: "); scanf("%d", &base); printf("Enter exponent: "); scanf("%d", &exponent); // Step 5: Call the recursive function to calculate the power int result = power(base, exponent); // Step 6: Display the result printf("%d^%d = %d\n", base, exponent, result); return 0; // Return 0 to indicate successful execution }
Explanation
Step 2: Define the Recursive Function
- The
power
function takes two integersbase
andexponent
as input. - Base Case: If the
exponent
is 0, the function returns 1 (since any number raised to the power of 0 is 1). - Recursive Case: If the
exponent
is greater than 0, the function returnsbase * power(base, exponent - 1)
, recursively calling itself with theexponent
decremented by 1.
Step 3: Declare Variables
- The variables
base
andexponent
store the input values for the base and exponent.
Step 4: Input the Base and Exponent
- The program prompts the user to enter the base and exponent using
scanf
.
Step 5: Call the Recursive Function
- The program calls the
power
function withbase
andexponent
as arguments and stores the result in the variableresult
.
Step 6: Display the Result
- The program uses
printf
to display the calculated power.
Return 0
- The
return 0;
statement indicates that the program executed successfully.
Output Example
Example 1:
Enter base: 2 Enter exponent: 3 2^3 = 8
Example 2:
Enter base: 5 Enter exponent: 4 5^4 = 625
Example 3:
Enter base: 7 Enter exponent: 0 7^0 = 1
Conclusion
This C program demonstrates how to calculate the power of a number using a recursive function. It covers basic concepts such as recursion, base cases, and user input, making it a useful example for beginners learning C programming.