0
Help me in C++
Vous devez créer une application qui calcule la somme des nombres de 1 à N, où N est pris en entrée. Par exemple, pour le nombre 5, la sortie devrait être 15, car 1+2+3+4+5=15. On utilisant boucle for Solution c++
11 Respuestas
+ 1
What have you tried so far?
+ 1
It is not necessary to repeat what you have written in the description.
Show as your code attempt so we can make suggestions.
Hint:
You need integer input.
You can use a loop.
+ 1
MattMad
For beginners, yes, first give hints and motivate the OP to come up with and present their own attempt. It is totally okay when a first attempt doesn't work or is incomplete.
Once an attempt is presented, we can get an idea of their approach and identify potential knowledge gaps and misconceptions. Then we can give further hints and explanations so the OP eventually finds out how to fix their code themselves.
+ 1
Let's first fix some syntax issues:
– You can add something to a variable using += (not =+).
– Do not delete the "return 0;" at the end of the main function.
Then look at your for-loop:
I think you confused "n" and "i" here.
– "i" is the counter variable – the value that you want to add to "sum".
‐ "n" is the input; the upper limit to which we count up.
So the head of your for-loop would look something like:
for (i = 0; i <= n; i++)
Note that we only modify "i", but not "n".
Finally, after the loop, we need to print out the value stored in "sum".
0
I want to create a application c++ , ask user to enter number and after calcule the sum while to arrive to the number entered.
For example, you entere 6, the output equal 16, because 1+2+3+4+5+6=16.
NB: use the boucle for
@Liza #Liza
0
Please don't give ready-made codes. Rather help the OP to develop their own approach.
0
Lisa
So you're saying... Essentially, hints are better than a full code?
0
OK, I know that, I try to write the code but it's not Ok
0
SHOW
THE
CODE
0
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int i, sum = 0;
for(n= 1; n<=i ; ++n){
sum =+i;
}
}
0
That'd be it then 🙃