×

C++ Programs

C++ Most popular & Searched Programs

C++ Basic I/O Programs

C++ Constructor & Destructor Programs

C++ Manipulators Programs

C++ Inheritance Programs

C++ Operator Overloading Programs

C++ File Handling Programs

C++ Bit Manipulation Programs

C++ Classes & Object Programs

C++ program to find the power of a number using loop

Finding power of a number in C++: Here, we are going to learn how to find the power of a number using loop in C++?
Submitted by Anuj Singh, on June 04, 2019

Here, we are going to calculate the value of Nth power of a number without using pow function.

The idea is using loop. We will be multiplying a number (initially with value 1) by the number input by user (of which we have to find the value of Nth power) for N times. For multiplying it by N times, we need to run our loop N times. Since we know the number of times loop will execute, so we are using for loop.

Example:

    Input:
    base: 5, power: 4

    Output:
    625

C++ code to find power of a number using loop

#include <iostream>
using namespace std;

int main()
{
    int num;
    int a = 1;
    int pw;

    cout << "Enter a number: ";
    cin >> num;
    cout << "\n";

    cout << "Enter a power : ";
    cin >> pw;
    cout << "\n";

    for (int i = 1; i <= pw; i++) {
        a = a * num;
    }

    cout << a;

    return 0;
}

Output

Enter a number: 5

Enter a power : 4

625
Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.