数字总和C ++

编程入门 行业动态 更新时间:2024-10-27 04:33:02
本文介绍了数字总和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

解决方案

#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 ++

本文发布于:2023-11-30 12:21:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649930.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:总和   数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!