The University of Jordan
King Abdullah II School of Information Technology
Department of Computer Science
Computer Skills for Scientific Faculties-(1931102)
Chapter 5: Programming Exercises -sample
ExProg1: Write a program that reads a set of integers and then finds
and prints the sum
of the even and odd integers.
Solution:
#include <iostream>
#include <iomanip>
using namespace std;
const int N = 20;
int main ()
{
//Declare variables
int counter; //loop control variable
int number; //variable to store the new number
//Declare and initialize (remaining) variables
int sumOdds = 0; //variable to store the sum of odd integers
int sumEvens = 0; //variable to store the sum of even integers
cout << "Please enter " << N << " integers" << endl;
cout << "The numbers you entered are: " << endl;
for (counter = 1; counter <= N; counter++)
{
cin >> number; //read number
cout << number << " "; // output number
if (number % 2 == 0)
sumEvens = sumEvens + number; //update even sum
else
sumOdds = sumOdds + number; //update odd sum
}
cout << endl;
cout << "Even sum: " << sumEvens << endl;
cout << "Odd sum: " << sumOdds << endl;
[Link]();
[Link]();
return 0;
}
Sample Run:
Please enter 20 integers
The numbers you entered are:
11 22 12 33 52 45 75 88 66
33
12 22 42 25 11 25 33 62 45
22
11 22 12 33 52 45 75 88 66 33 12 22 42 25 11 25 33 62 45 22
Even sum: 400
Odd sum: 336
ExProg2: Write a program that uses while loops to perform the following
steps:
a) Prompt the user to input two integers: firstNum and secondNum
(firstNum must be less than secondNum).
b) Output all odd numbers between firstNum and secondNum.
c) Output the sum of all even numbers between firstNum and
secondNum.
d) Output the numbers and their squares between 1 and 10.
e) Output the sum of the square of the odd numbers between firstNum
and secondNum.
f) Output all uppercase letters.
Solution:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int firstNum, secondNum;
int sumEven = 0;
int sumSquareOdd = 0;
char chCounter;
int counter;
//Part a
cout << "Enter two numbers." << endl;
cout << "First number must be less than "
<< "the second number you enter" << endl;
cout << "Enter numbers: " << flush;
cin >> firstNum >> secondNum;
cout << endl;
//Part b
if (firstNum % 2 == 0)
counter = firstNum + 1;
else
counter = firstNum;
cout << "Odd integers between " << firstNum << " and "
<< secondNum << " are: " << endl;
while (counter <= secondNum)
{
cout << counter << " ";
counter = counter + 2;
}
cout << endl;
//Part c
if (firstNum % 2 == 0)
counter = firstNum;
else
counter = firstNum + 1;
while (counter <= secondNum)
{
sumEven = sumEven + counter;
counter = counter + 2;
}
cout << "Sum of even integers between " << firstNum << " and "
<< secondNum << " = " << sumEven << endl;
//Part d
cout << "Number Square of Number" << endl;
counter = 1;
while (counter <= 10)
{
cout << setw(4) << counter << setw(18)
<< counter * counter << endl;
counter++;
}
cout << endl;
//Part e
if (firstNum % 2 == 0)
counter = firstNum + 1;
else
counter = firstNum;
while (counter <= secondNum)
{
sumSquareOdd = sumSquareOdd + counter * counter;
counter = counter + 2;
}
cout << "Sum of the squares of odd integers between "
<< firstNum << " and " << secondNum << " = "
<< sumSquareOdd << endl;
//Part f
cout << "Upper case letters are: ";
chCounter = 'A';
while (chCounter <= 'Z')
{
cout << chCounter << " ";
chCounter++;
}
cout << endl;
return 0;
}
Sample Run:
Enter two numbers.
First number must be less than the second number you enter
Enter numbers: 9
10
Odd integers between 9 and 10 are:
9
Sum of even integers between 9 and 10 = 10
Number Square of Number
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100
Sum of the squares of odd integers between 9 and 10 = 81
Upper case letters are: A B C D E F G H I J K L M N O P Q R S T U V W
X Y Z
ExProg2: The population of a town A is less than the population of town
B. However, the population of town A is growing faster than the
population of town B. Write a program that prompts the user to enter the
population and growth rate of each town. The program outputs after how
many years the population of town A will be greater than or equal to the
population of town B and the populations of both the towns at that time.
(A sample input is: Population of town A = 5000, growth rate of town A =
4%, population of town B = 8000, and growth rate of town B = 2%.)
Solution:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int townAPop;
int townBPop;
double growthRateTownA;
double growthRateTownB;
int numOfYears = 0;
cout << "Enter the current population of town A: ";
cin >> townAPop;
cout << endl;
cout << "Enter the current population of town B: ";
cin >> townBPop;
cout << endl;
cout << "Enter the growth rate of town A: ";
cin >> growthRateTownA;
cout << endl;
cout << "Enter the growth rate of town B: ";
cin >> growthRateTownB;
cout << endl;
while (townAPop < townBPop)
{
townAPop = static_cast<int>(townAPop * (1 + growthRateTownA /
100.0));
townBPop = static_cast<int>(townBPop * (1 + growthRateTownB /
100.0));
numOfYears++;
}
cout << "After " << numOfYears << " year(s) the population of town A "
<< "will be greater than or equal to the population of town B." <<
endl;
cout << "After " << numOfYears << " population of town A is "
<< townAPop << endl;
cout << "After " << numOfYears << " population of town B is "
<< townBPop << endl;
[Link]();
[Link]();
return 0;
}
Sample Run:
Enter the current population of town A: 200
Enter the current population of town B: 400
Enter the growth rate of town A: 15
Enter the growth rate of town B: 10
After 16 year(s) the population of town A will be greater than or
equal to the population of town B.
After 16 population of town A is 1841
After 16 population of town B is 1824