For Loops

This program first asks the user how many numbers, then uses a for loop to repeat the same instruction that many times.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Read in numbers: find total, using a for loop
#include <iostream>
using namespace std;
void main() { 
  double num, total=0.0;
  int numbers;
  int n; //variable to 'drive' the loop, usually a single letter 
  cout<<"How many numbers are there to be added:";
  cin>>numbers;
  for(n=0; n<numbers; n++) { 
    cout<<"Enter a number:";
    cin>>num;
    total=total+num;
  } //end of for loop
  cout<<"Total="<<total<<"\n";
  system("pause");
} //main
CODE

NEXT: For loop counts from 10 down to 1