I can't understand much about loop..

Write a program that calculates the sum of sequence 1 to 5.
(Using LOOP STATEMENT)

please help.. I've been try it for hours using for-statement.
All I could do was just to create the sequence of 1 to 5..

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
for(int a=1;a<=5;a++)
{
cout<<a;
}
getch();
return 0;
}
Instead of printing out the value of a, add a into a variable you declare to hold the sum.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 #include<iostream> // cin, get, cout

using namespace std;

int main()
{
  int total = 0;

  for( int a = 1; a <= 5; a++ )
    {
        total = total + a; // "Instead of printing out the value of a, add a into a variable you declare to hold the sum."
    }
    
    cout << total;
    
    cin.get(); // wait for key press
    
    return 0;
} 
Last edited on
you were exactly as me when i was learning the loop until now lol.

you just need to understand the syntax of that loop not the formation
Topic archived. No new replies allowed.