0
How to do the Sum practice with the for loop
I am not understanding how to put this together. I have an idea on how it works but i cannot put the coding together with the knowledge level im at. It explains that it takes an input of 1 to 5 and you have to add them together and output the sum. But when you actually do the challenge, it puts the inputs as 100 and wants an outcome of 5050. I just don't understand how I suppose to put it together and i have looked at the comments and discussions but I cannot find the right answer for this. And I have to do this before it lets me go on to the next lesson.
7 Respuestas
+ 6
Bigbling135 ,
to give you hints we need to see your code.
> please put it in playground, save it and place a link to it here.
+ 3
Bigbling135 this place is not for getting ready made code ,it will be better if you link your code , cuz it will help to understand you questions and get better solution by the community
+ 1
int n;
cin >> n;
int p = 0;
for(int o=5;o>=0;o--) {
int sum = n + p;
}
cout << sum;
}
This is what I have and I know its wrong but I just cannot figure out on how to put it together where one variable would add 5 different numbers together and then display the total. I am only up to for loops because I have to finish this challenge/practice before I can move on.
EDIT:
I went looking for answers about it and found this one very simple one and I still don't understand how when 100 gets put into it and it becomes 5050.
This is the code I found.
int n;
cin >> n;
int sum=0;
for(;n>0;n--) {
sum+=n;
}
cout << sum;
}
+ 1
100 + 99 + 98 + 97..................+ 1 = 5050;
0
You can try this !
int i;
int sum=0;
For(i=1;i<=5;i++)
{
Sum=sum+1;
}
cout<<"the sum is"<<sum;
0
It took me a little bit to understand the code I found and thanks for the help. The main issue I am having issues with now is understanding how to get a variable to add itself multiple times. I can kinda see how sum+=n works but im just not understanding how this stores and adds itself together. Got a little bit of a learning disability so it just makes it harder for me.