Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C++ Exercises: Find power of any number using for loop

C++ For Loop: Exercise-62 with Solution

Write a program in C++ to find power of any number using for loop.

Pictorial Presentation:

C++ Exercises: Find power of any number using for loop

Sample Solution:-

C++ Code :

#include <iostream>
using namespace std;
int main()
{
    int bs, ex, num=1,i;
    cout << "\n\n Find power of any number using for loop:\n";
    cout << "---------------------------------------------\n";
    cout << " Input the base: ";
    cin >> bs;
    cout << " Input the exponent: ";
	cin>>ex;
	
    for (i = 1; i <=ex; i++) 
    {
       num=num*bs;
    }
	cout <<bs<<" ^ "<<ex<<" = "<<num<<endl ;
}

Sample Output:

 Find power of any number using for loop:                              
---------------------------------------------                          
 Input the base: 2                                                     
 Input the exponent: 5                                                 
2 ^ 5 = 32 

Flowchart:

Flowchart: Find power of any number using for loop

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C++ to print all ASCII character with their values.
Next: Write a program in C++ to enter any number and print all factors of the number.

What is the difficulty level of this exercise?