Write C++ program to calculate power using while & for loop

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.

Reverse a Number using while loop

#include <iostream>
#include <math.h>
 
using namespace std;
 
int main()
{
    int base, exponent,power, i;
 
    //Reading base & exponent
    cout<<"Enter base: ";
    cin>>base;
    cout<<"Enter exponent: ";
    cin>>exponent;
 
    power = 1;
    i = 1;
    //caculatinh power of given number
    while(i <= exponent)
    {
        power = power * base;
        i++;
    }
    cout<<"Power of "<<base<<" is: " <<power;
 
    return 0;
}

Reverse a Number using for loop

#include <iostream>
#include <math.h>
 
using namespace std;
 
int main()
{
    int base, exponent, i, power;
 
    //Reading base & exponent
    cout<<"Enter base: ";
    cin>>base;
    cout<<"Enter exponent: ";
    cin>>exponent;
 
   power = 1;
 
    //caculatinh power of given number using for loop
    for(i=1; i<=exponent; i++)
        power = power * base;
    cout<<"Power of "<<base<<" is: " <<power;
 
    return 0;
}

Result

Write C++ program to calculate power using while & for loop
Write C++ program to calculate power using while & for loop

Leave a Comment