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?



Share this Tutorial / Exercise on : Facebook and Twitter