c語言for循環++
Here, we are going to calculate the value of Nth power of a number without using pow function.
在這里,我們將不使用pow函數來計算數字的N 次冪的值 。
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.
這個想法是使用循環。 我們將通過輸入的號碼被用戶乘以一個數字(初始值為1)(其中我們必須找到的第 N功率值)為N次 。 為了將其乘以N倍,我們需要運行循環N次。 由于我們知道循環執行的次數,因此我們使用for循環。
Example:
例:
Input:
base: 5, power: 4
Output:
625
C ++代碼使用循環查找數字的冪 (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
翻譯自: https://www.includehelp.com/cpp-programs/find-the-power-of-a-number-using-loop.aspx
c語言for循環++