数字总和C ++ [英] Sum of Numbers C++
本文介绍了数字总和C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
的所有整数从1到输入的数字的总和。例如,如果用户输入50,循环将找到
1,2,3,4,... 50的总和。
但是对于有些原因,它不工作,我有我的循环麻烦,但这是我到目前为止。
#包括< iostream>
使用namespace std;
int main()
{
int positiveInteger;
int startingNumber = 1;
int i = 0;
cout<< 请输入一个高达100的整数 << ENDL;
cin>> positiveInteger;
for(int i = 0; i {
i = startingNumber + 1;
cout<<一世;
}
return 0;
$ b $ p
$ b我现在只是处于亏损状态,工作正常。
解决方案 #include< iostream>
使用namespace std;
int main()
{
int positiveInteger;
int startingNumber = 1;
cout<< 请输入一个高达100的整数。 << ENDL;
cin>> positiveInteger;
int result = 0
for(int i = startingNumber; i <= positiveInteger; i ++)
{
result + = i
cout< ;<结果;
}
cout<<结果;
返回0;
}
I am supposed to write a program that asks the user for a positive integer value. The program should use a loop to get the sum of
all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of
1, 2, 3, 4, ... 50.
But for some reason it is not working, i am having trouble with my for loops but this is what i have down so far.
#include <iostream>
using namespace std;
int main()
{
int positiveInteger;
int startingNumber = 1;
int i = 0;
cout << "Please input an integer up to 100." << endl;
cin >> positiveInteger;
for (int i=0; i < positiveInteger; i++)
{
i = startingNumber + 1;
cout << i;
}
return 0;
}
I am just at a loss right now why it isn't working properly.
解决方案 try
#include <iostream>
using namespace std;
int main()
{
int positiveInteger;
int startingNumber = 1;
cout << "Please input an integer upto 100." << endl;
cin >> positiveInteger;
int result = 0
for (int i=startingNumber; i <= positiveInteger; i++)
{
result += i
cout << result;
}
cout << result;
return 0;
}
这篇关于数字总和C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文