Page 282 - Computer_Science_F5
P. 282
The following C++ program example 4.26 computes the sum of 10 positive integers
using for loop. The output is shown in Figure 4.51.
Program Example 4.26: Chapter Four: Object oriented programming with C++
Finding the sum of numbers using for loop
FOR ONLINE READING ONLY
#include<iostream>
using namespace std;
int main()
{
int i, sum=0;
for(i=1; i<=10; i++)
{
sum=sum+i;
}
cout<<”The sum of 10 numbers is: “<<sum<<endl;
return 0;
}
Output:
Figure 4.51: Output of the program
Activity 4.6:
displays total marks and average
Difference between the ‘while loop, marks for each student, using:
do….while loop and for…loop’ (i) while…..loop
(a) Write three C++ programs to (ii) do-while…loop
display students’ results. Each (iii) for.... loop
program prompts a user to enter (b) Compare the results generated from
the number of students in the class each loop
and the marks of five (5) subjects (c) Comment on the results obtained
for each student. Then, the program from each loop.
273
Student’s Book Form Five
Computer Science Form 5.indd 273 23/07/2024 12:34

