简体   繁体   English

创建程序,使用C ++中的“ for”命令计算指数

[英]Creation program which calculate exponets with the use of “for” command in C++

I tried to write a program to do the exponential calculation with the use of for command. 我试图编写一个程序,使用for命令进行指数计算。 I wrote the code which appears in the lines below but it's not working. 我写了下面几行中出现的代码,但是没有用。

include <iostream>
using namespace std;
int main()
{
    int base=2;
    int exp=2;
    int result;
    for (int i=1; i<e ;i++) {
        result=base*base;
    }
    cout << result <<endl;
    return 0;
}
  1. Initialize result to 1 . result初始化为1
  2. In each iteration, multiply result by base . 在每次迭代中,将result乘以base
  3. Make sure result is the running result. 确保result是运行结果。
int result = 1;
// for (int i=1; i <= exp ; i++) // This will work too.
for (int i=0; i < exp ; i++)
{
   result *= base;
}

You never really explained what you wanted to happen or for the matter why, but hopefully this solves your problem. 您从未真正解释过您想发生的事情或原因,但是希望这可以解决您的问题。

#include<iostream>

using namespace std;

int main()
{

int base = 2;
int exp = 2;
int result = base*base;

for(int i = 0; i <= exp; i++) //Unsure why you use the variable exp in the condition
{
   cout << result << endl;
   base++ 
}

system("PAUSE"); //Non portable code; used just for an example

return 0;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM