[英]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;
}
result
to 1
. 将result
初始化为1
。 result
by base
. 在每次迭代中,将result
乘以base
。 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.