🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
In mathematics, raising a number to a power means multiplying the number by itself for a specified number of times. For instance, 3 raised to the power of 4 (3^4) is 3 x 3 x 3 x 3 = 81. In programming, one can compute powers in multiple ways, and recursion offers a clean and logical method for such tasks. This blog post will walk you through a C++ program that calculates the power of a number using recursion.
2. Program Overview
The program's mechanism is as follows:
1. Get base and exponent input from the user.
2. If the exponent is zero, return 1 (as any number raised to the power of 0 is 1).
3. Otherwise, recursively multiply the base by itself until the exponent becomes zero.
4. Display the computed result to the user.
3. Code Program
#include <iostream>
using namespace std;
// Function to calculate power using recursion
double power(double base, int exponent) {
if(exponent == 0) {
return 1;
} else if(exponent < 0) {
return 1 / power(base, -exponent); // Dealing with negative exponents
} else {
return base * power(base, exponent - 1);
}
}
int main() {
double base;
int exponent;
// Taking user input
cout << "Enter base: ";
cin >> base;
cout << "Enter exponent: ";
cin >> exponent;
cout << base << " raised to the power of " << exponent << " = " << power(base, exponent);
return 0; // Signal successful termination
}
Output:
For input values of base 3 and exponent 4, the output will be: "3 raised to the power of 4 = 81"
4. Step By Step Explanation
1. Headers and Namespace: The I/O stream library is included, and the standard namespace is set up for simplified input and output operations.
#include <iostream>
using namespace std;
2. Recursive Function: The power function is the heart of this program. When the exponent is zero, the function returns 1. For negative exponents, the function calls itself with the positive value of the exponent and divides 1 by the result, giving the reciprocal. For positive exponents, the base is multiplied recursively.
// Function to calculate power using recursion
double power(double base, int exponent) {
if(exponent == 0) {
return 1;
} else if(exponent < 0) {
return 1 / power(base, -exponent); // Dealing with negative exponents
} else {
return base * power(base, exponent - 1);
}
}
3. Main Function: Here, the program begins. The base and exponent variables are initialized, and user input is taken using cin. The program then calls the power function and prints the result using cout.
int main() {
double base;
int exponent;
// Taking user input
cout << "Enter base: ";
cin >> base;
cout << "Enter exponent: ";
cin >> exponent;
cout << base << " raised to the power of " << exponent << " = " << power(base, exponent);
return 0; // Signal successful termination
}
4. Program Termination: The program ends and returns a value of 0, indicating successful execution.
return 0; // Signal successful termination
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment