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

28th Jan 2025, 10:47 AM
Omar Zaafouri
Omar Zaafouri - avatar
11 Respuestas
+ 1
What have you tried so far?
28th Jan 2025, 11:44 AM
Lisa
Lisa - avatar
+ 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.
29th Jan 2025, 9:22 AM
Lisa
Lisa - avatar
+ 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.
30th Jan 2025, 11:11 AM
Lisa
Lisa - avatar
+ 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".
30th Jan 2025, 1:57 PM
Lisa
Lisa - avatar
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
29th Jan 2025, 9:19 AM
Omar Zaafouri
Omar Zaafouri - avatar
0
Please don't give ready-made codes. Rather help the OP to develop their own approach.
30th Jan 2025, 10:26 AM
Lisa
Lisa - avatar
0
Lisa So you're saying... Essentially, hints are better than a full code?
30th Jan 2025, 10:41 AM
MattMad
MattMad - avatar
0
OK, I know that, I try to write the code but it's not Ok
30th Jan 2025, 12:34 PM
Omar Zaafouri
Omar Zaafouri - avatar
0
SHOW THE CODE
30th Jan 2025, 12:40 PM
Lisa
Lisa - avatar
0
#include <iostream> using namespace std; int main() { int n; cin >> n; int i, sum = 0; for(n= 1; n<=i ; ++n){ sum =+i; } }
30th Jan 2025, 12:52 PM
Omar Zaafouri
Omar Zaafouri - avatar
0
That'd be it then 🙃
30th Jan 2025, 3:23 PM
MattMad
MattMad - avatar