c语言for循环++_C ++程序使用循环查找数字的幂
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: 4Output:625C ++代碼使用循環查找數字的冪 (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: 5Enter a power : 4625翻譯自: https://www.includehelp.com/cpp-programs/find-the-power-of-a-number-using-loop.aspx
c語言for循環++
總結
以上是生活随笔為你收集整理的c语言for循环++_C ++程序使用循环查找数字的幂的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 操作系统 非连续分配_操作系统中的连续和
- 下一篇: long类型20位示例_Java Lon