Code |
Output |
/****************************************************************************************
4_51a.cpp
example 1, trivial case
****************************************************************************************/
#include <iostream>
#include <string>
using namespace std;
int main()
{
//prompt & inform the user
cout << "This program will add together the range of integers from 0 to 10" << endl;
cout << "Sum: " << 0+1+2+3+4+5+6+7+8+9+10 << endl;
return 0;
}
|
This program will add together the range of integers from 0 to 10
Sum: 55
|
/****************************************************************************************
4_51b.cpp
Series sum using WHILE
****************************************************************************************/
#include <iostream>
#include <string>
using namespace std;
int main()
{
//prompt & inform the user
cout << "This program will add together the range of integers from 0 ... 10" << endl;
int sum = 0, //placeholder for running sum
next= 0; //placeholder for which number to add
while ( next <= 10)
{
sum = sum + next;
next++;
}
cout << "Sum: " << sum << endl;
return 0;
}
|
This program will add together the range of integers from 0 to 10
Sum: 55
|
/****************************************************************************************
4_51c.cpp
Jim Millard
for CO311/Spring 2000
Series sum using FOR
****************************************************************************************/
#include <iostream>
#include <string>
using namespace std;
int main()
{
//prompt & inform the user
cout << "This program will add together the range of integers 0 ... 10" << endl;
int sum = 0; //placeholder for running sum
for (int next=0; next <= 10; next++)
sum += next;
cout << "Sum: " << sum << endl;
return 0;
}
|
This program will add together the range of integers from 0 to 10
Sum: 55
|
/****************************************************************************************
4_51d.cpp
Jim Millard
for CO311/Spring 2000
Series sum using FOR
****************************************************************************************/
#include <iostream>
#include <string>
using namespace std;
int main()
{
//useful constants
const int start = 0, finish = 10;
//prompt & inform the user
cout << "This program will add together the range of integers from "
<< start << " ... " << finish << endl;
int sum = 0; //placeholder for running sum
for (int next=start; next <= finish; next++)
sum += next;
cout << "Sum: " << sum << endl;
return 0;
}
|
This program will add together the range of integers from 0 to 10
Sum: 55
|