➤ Hello World! in C++
➤ Print Number in C++
➤ Add 2 Numbers C++
➤ Arithmetic Operation
➤ Sum Avg of 3 Number
➤ Area Program in C++
➤ Simple Interest in C++
➤ Find ASCII value in C++
➤ Swap 2 Number in C++
Flow Control Programs
➤ Even-Odd in C++
➤ +ve, -ve, 0 in C++
➤ Vowel-Consonant
➤ Greatest of 3 no.
➤ Check Leap Year
➤ Calculator Program
➤ Reverse a Number
➤ Sum of Natural Number
➤ GCD of 2 Number
➤ LCM of 2 Number
➤ Find Power in C++
➤ Fibonacci Series in C++
➤ Palindrome Number
➤ Find Factorial in C++
➤ Factorial Using Recursion
➤ Prime Number in C++
➤ Prime Number b/w 1-N
Array
➤ Linear Search in C++
➤ Binary Search in C++
Others
➤ Introduction to C++
➤ Data Types in C++
➤ Range of Data Types
➤ Void main, main vs int main
C++ program to find power | In this post, we will develop a C++ program to calculate the power of a number. First, we will develop a program using for loop then we will use the recursion technique, and finally we will use function overloading.
Power of any number b*n given as b*b*…..*b (n-times). Here b is called base and n is called the exponent. Here we will write the C program to find the power of a number.
Example:-
22 = 2*2 = 4
33 = 3*3*3 = 27
53 = 5*5*5 = 125
C++ Program to Find Power of a Number Using For Loop
// C++ program to calculate power of a Number
#include<iostream>
using namespace std;
int main()
{
// declare variables
int base, exponent;
long result=1;
// take input
cout << "Enter base and exponent: ";
cin >> base >> exponent;
// calculate power value
for (int i = exponent; i >= 1; i--)
{
result *= base;
}
// display result
cout << "Result = " << result << endl;
return 0;
}
Output:-
Enter base and exponent: 5 4
Result = 625
Using pow() Function
We can also use pow() function to calculate the power of a number in C++.
// C++ program to calculate power of a Number
#include<iostream>
#include
using namespace std;
int main()
{
// declare variables
int base, exponent;
long result;
// take input
cout << "Enter base and exponent: ";
cin >> base >> exponent;
// calculate power value
result = pow(base, exponent);
// display result
cout << "Result = " << result << endl;
return 0;
}
C++ Program to Find Power of a Number Using Recursion
We can also use recursion technique to calculate the power of a number. In recursion technique, we are defining a recursive function.
A technique of defining the method/function that contains a call to itself is called the recursion. The recursive function/method allows us to divide the complex problem into identical single simple cases that can be handled easily. This is also a well-known computer programming technique: divide and conquer.
#include<iostream>
#include
using namespace std;
// function declaration
long power(int, int);
// main function
int main()
{
// declare variables
int base, exponent;
long result;
// take input
cout << "Enter base and exponent: ";
cin >> base >> exponent;
// calculate power value
result = power(base, exponent);
// display result
cout << "Result = " << result << endl;
return 0;
}
// function to calculate power
long power(int a, int b)
{
if(b==0)
return 1; //base case
else
return a * power(a, --b); //general case
}
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!
Learn More C++ Programming Examples,
- Introduction to C++ Programming
- Data Types in C++ Programming
- Find Range of Data Types in C++
- Void main(), main() vs int main() in C++
- Hello World! Program in C++
- Addition of Two Numbers in C++
- Add Subtract Multiply Divide 2 Numbers
- Sum and Average of three numbers in C++
- Area of Circle Triangle Rectangle in C++
- Calculate Simple Interest in C++
- C++ program to find ASCII value
- C++ program to swap two Number
- Check Even or Odd Number in C++
- Find Positive Negative Zero in C++
- Check Vowel or Consonant in C++
- Greatest of Three Numbers in C++
- Program to Check Leap Year in C++
- Simple Calculator Program in C++
- Program to Reverse a Number in C++
- Sum of Natural Numbers in C++
- C++ Program for GCD of Two Numbers
- C++ Program for LCM of Two Numbers
- Find Power of a Number in C++
- Fibonacci Series Program in C++
- Check Palindrome Number in C++
- Factorial of a Number Program in C++
- Factorial Using Recursion in C++
- Prime Number Program in C++
- Prime Number b/w 1 to N in C++