Repetition
Repetition
Repetition
Exercise #1: Upper Case Letters: Write a program that prompts the user to enter n characters, counts, and
prints the number of upper case letters.
#include<iostream>
int main()
{
int n, upper = 0, k = 0;
char letter;
cout << "There are " << upper << " upper case letters" << endl;
return 0;
}
1
Exercise #2 Data Validation Write a program that reads a positive integer L such that 1 < L < 25, and
prints whether the number is odd or even. The program should validate if the number is within range or
not. After processing the first integer, the program asks if the user wants to try again. If the user enters Y,
y, N, or n the program ask for the correct values of the number, while if the user enters a different letter
the program displays the message Invalid letter, enter Y, y, N, or n and reads the new character.
Sample inputs / outputs:
Enter a positive integer L, such that 1 < L < 25: -3
Enter a positive integer L, such that 1 < L < 25: 25
Enter a positive integer L, such that 1 < L < 25: 17
17 is an odd number
Do you want to try again?
Y or y for yes, N or n for no: R
Invalid letter, enter Y, y, N, or n: n
#include<iostream>
using namespace std;
int main()
{
int number;
char letter;
while(true)
{
while(true)
{
cout << "Enter a positive integer L, such that 1 < L < 25: ";
cin >> number;
if(number % 2 == 0)
cout << number << " is an even number\n";
else
cout << number << " is an odd number\n";
return 0;
}
3
Exercise #3 Inverse of a Number Write a program that reads a positive integer, which is not a multiple
of 10, finds, and prints the inverse of that integer.
#include <iostream>
int main()
{
int number, digit, sum = 0;
return 0;
}
4
Exercise #4 Number Location in an Interval Write a program that prompts the user to enter the lower
and upper limits of an interval [m, n]. The program also prompts the user to enter a number k within the
interval. The program uses a repetition statement to find, and print the location of the number k in that
interval. For example, if the limits of the interval are [17, 28] and the number is 21, then 21 is the 5th
element in the interval [17, 28].
#include <iostream>
int main()
{
int number, lower, upper, k, location = 1;
cout << "Enter the lower and upper limits of an interval: ";
cin >> lower >> upper;
cout<<"Enter a number within the interval [" << lower << ", " << upper << "]: ";
cin >> number;
k = lower;
cout << "The number " << number << " is the " << location << "th number" << endl;
return 0;
}
5
Exercise #5 Ascending Power Number Write a program that prompts the user to enter a positive integer
number (any number of digits) and checks whether the integer is an ascending power numbers or not. The
number 2646798 is an ascending power number since, 21 + 62 + 43 + 64 + 75 + 96 + 87 = 2 + 36 + 64 +
1296 + 16807 + 531441 + 2097152 = 2646798. Use the function pow(x, n) to find xn.
#include <iostream>
int main()
{
int number, n, d, k = 0, i=1, sum = 0;
n = number;
while(n > 0)
{
n = n / 10;
k++;
}
n = number;
while(n > 0)
{
d = n % 10;
n = n / 10;
sum = sum + pow(d, k);
k--;
}
if(number == sum)
cout << number << " is an ascending power number" << endl;
else
cout << number << " is not an ascending power number" << endl;
return 0;
}
6
Exercise #6 The Value of Write a program that prompts the user to enter how many terms to be used in
calculating the value of π. The program then calculates and prints an approximation to the value of π
using the following formula:
π = 4 (1 – 1/3 + 1/5 – 1/7 + 1/9 – 1/11 + ... )
Sample inputs / outputs:
Enter how many terms: 100
The value of pi to 100 terms is: 3.13159
#include <iostream>
int main()
{
int N, i;
float sum = 0, sign = -1;
cout << "The value of pi to " << N << " terms is: " << 4*sum << endl;
return 0;
}
7
Exercise #7 The Cats Calories A cat has energy in the form of C calories, when it walks for one hour it
burns B calories and when it eats for one hour it gains E calories. The cat walks for one hour and then eats
for another one hour. You are asked to write a program that prompts the user to enter the values of C, B,
and E. The program finds and prints the total number of hours needed for the cats energy to be zero. The
cat stops walking or eating when the energy is zero or less. For example: Let C = 9, B = 4, and E = 2.
After the first hour of walk the cats energy drops to 5, then after a second hour of eating the cats energy
increases to 7, after the third hour of walk the cats energy drops to 3, then after a fourth hour of eating the
cats energy increases to 5, after the fifth hour of walk the cats energy drops to 1, then after a sixth hour
of eating the cats energy increases to 3, finally after the seventh hour of walk the cats energy drops to -1.
#include<iostream>
using namespace std;
int main()
{
int C, B, E, hour = 0, temp;
while(C > 0)
{
C = C - B;
hour++;
if(C > 0)
{
C = C + E;
hour++;
}
}
cout << "For " << temp << " calories with walk " << B << " and eat " << E
<< " the cat needs " << hour << " hours to stop." << endl;
return 0;
}
8
Exercise #8 Find the nth Term The following relation is a recurrence relation:
s0 = 1, s1 = 2, sn+2 = 2 sn+1 – sn + 1, n 0
One can calculate the value of s2, s3, s4, … as 4, 7, 11, 16, 22, …
Write a program that prompts the user to enter the term number, finds, and prints the term value.
Sample inputs / outputs:
Enter which term: 4
The 4th term is 11
#include<iostream>
int main()
{
int n, s0=1, s1=2, s2, k = 1;
while(k < n)
{
s2 = 2*s1-s0+1;
s0 = s1;
s1 = s2;
k++;
}
cout << "The " << n << "th term is " << s2 << endl;
return 0;
}
9
Exercise #9 Sequence of Numbers Consider the following algorithm to generate a sequence of numbers.
Start with an integer n. If n is even, divide it by 2. If n is odd, multiply it by 3 and add 1. Repeat this
process with the new value of n, terminating when n = 1. For example, the following sequence of numbers
will be generated for n = 22:
22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
Write a program that prompts the user to enter a positive integer n, finds, and prints all the values in the
sequence.
Sample inputs / outputs:
Enter an integer: 10
The values of the sequence are: 10 5 16 8 4 2 1
The sequence length is 6
#include <iostream>
int main()
{
int n, k = 1;
k++;
cout << "\t" << n;
}
cout << endl;
return 0;
}
10
Exercise #10 Minimum and Maximum: Write a program that prompts the user to enter an even integer N
that represents how many numbers he/she wants to enter. The program then reads N decimal numbers and
finds the minimum number in the first half and the maximum number in the second half.
#include<iostream>
int main()
{
float num, minimum, maximum;
int N, k;
cout << "Enter an even integer: ";
cin >> N;
k = 1;
cout << "Enter a number " << N/2 << " decimal numbers: ";
cin >> minimum;
while(k < N/2)
{
cin >> num;
if(num < minimum)
minimum = num;
k++;
}
k = 1;
cout << "Enter a number " << N/2 << " decimal numbers: ";
cin >> maximum;
while(k < N/2)
{
cin >> num;
if(num > maximum)
maximum = num;
k++;
}
cout << "The minimum is " << minimum << " and the maximum is " << maximum << endl;
return 0;
}
11
Exercise #11 Rock, Paper, Scissors Write a program that simulates the game "rock, paper, scissors". The
program reads the input of both players (rock, paper or scissors), first parameter from first player, second
from second player. The function returns the result as such:
"Player 1 wins"
"Player 2 wins"
"TIE" (if both inputs are the same)
Sample input/output:
Enter the players parameter: rock paper
Player 2 wins
#include <iostream>
int main()
{
char op1, op2;
if(op1 == op2)
cout << "TIY" << endl;
else if(op1 == 'p' && op2 == 'r')
cout << "Player 1 wins" << endl;
else if(op1 == 'p' && op2 == 's')
cout << "Player 2 wins" << endl;
else if(op1 == 'r' && op2 == 's')
cout << "Player 1 wins" << endl;
else if(op1 == 'r' && op2 == 'p')
cout << "Player 2 wins" << endl;
else if(op1 == 's' && op2 == 'p')
cout << "Player 1 wins" << endl;
else if(op1 == 's' && op2 == 'r')
cout << "Player 2 wins" << endl;
return 0;
}
12
Exercise #12 Sum of Factors of Factors Write a program that reads a number and returns the sum of
factors of factors of the given number. Do not count factors that are equal to 1 or the number. For
example, the factors of 24 that you count are 2, 3, 4, 6, 8, and 12. The factors 2 and 3 have no factors that
can be counted. The factor 4 has only 2 that can be counted. The factor 6 has 2 and 3 that can be counted.
The factor 8 has 2 and 4 that can be counted. The factor 12 has 2, 3, 4, and 6 that can be counted. The
factor of factors that can be counted are: 2 + 2+3 + 2+4 + 2+3+4+6 = 28.
Sample input/output:
Enter an integer: 24 Enter an integer: 69
The sum of factors of factors of 24 is 28 The sum of factors of factors of 69 is 0
#include <iostream>
int main()
{
int n, i, j, sum = 0;
cout << "The sum of factors of factors of " << n << " is " << sum << endl;
return 0;
}
13
Exercise #13 Double Pay An employee working at a very bizzare company, earns one penny on their
first day. However, for every day that passes, their base amount doubles, so they earn two pennies on the
second day and four pennies on the third day (totaling 7 pennies). Write a program that given a number of
days, return how many pennies the employee accumulates.
#include <iostream>
int main()
{
int total = 0, pay = 1, day, i;
cout << "After " << day << " days, the total pay is " << total << endl;
return 0;
}
14
Exercise #14 Sum of Digits between Two Integers Write a program that sums the total number of digits
between two numbers, inclusive. For example, between the numbers 19 and 22 we have 1 + 9 + 2 + 0 + 2
+ 1 + 2 + 2 = 19
#include <iostream>
int main()
{
int total = 0, lower, upper, i, sum, number, digit;
cout << "The sum of all digits of the interval [" << lower << ", " << upper
<< "] is " << total << endl;
return 0;
}
15
Exercise #15 Is it a Prime Write a program that reads a positive integer, checks whether it is a prime
number or not. A prime number is an integer that is divisible by 1 and itself only. For example, 11, 13, 17,
19 are prime numbers.
#include <iostream>
int main()
{
int number, i;
return 0;
}
16
Form a Number Write a program that prompt the user to enter n digits and use them to form, and print a
number. The first entered digit is the unit digit; the second entered digit is the ten digit, and so on.
#include<iostream>
int main()
{
int n,number=0,x,finalAnswer=0,temp;
temp = number;
while(temp > 0)
{
finalAnswer = finalAnswer * 10 + temp % 10;
temp = temp / 10;
}
return 0;
}
17
Scrambled Word Write a program that reads letters, scramble them into a word, and prints the scrambled
word. Reading stops at the character 0. One way to scramble lower-case letters into a word is to replace
each letter by the kth letter in the alphabet. For example, the scrambled version of the letters shtqixew with
k = 4 is wlxumbia.
#include<iostream>
int main()
{
int k;
char c,x;
string word;
while(c != '0')
{
x = (int(c) - 97 + k) % 26 + 97;
word += x;
cout << "A scrambled version of the letters is " << word;
return 0;
}
18
Amicable Numbers Two numbers are amicable if the sum of the proper divisors of one is equal to the other.
The pair of numbers 220 and 284 are amicable because
the proper divisors of 220 are: 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110 (their sum is 282)
the proper divisors of 282 are: 1, 2, 4, 71, 142 (their sum is 220)
Write a program that prompts the user to enter two numbers, checks, and prints whether the two numbers are
related or not.
#include<iostream>
int main()
{
int x,y,sum1=0,sum2=0;
else
cout << "The two numbers " << x << " and " << y << " are not related";
return 0;
}
19
Digitize an Integer Write a program that reads a positive integer, finds the digit-sum, and how many
times each digit is repeated in the integer.
#include<iostream>
int main()
{
int x,sum=0,zero=0,one=0,two=0,three=0,four=0,five=0,six=0,seven=0,eight=0,nine=0,temp;
temp = x;
while(temp > 0)
{
sum = sum + temp % 10;
if(temp % 10 == 0)
zero++;
else if(temp % 10 == 1)
one++;
else if(temp % 10 == 2)
two++;
else if(temp % 10 == 3)
three++;
else if(temp % 10 == 4)
four++;
else if(temp % 10 == 5)
five++;
else if(temp % 10 == 6)
six++;
20
else if(temp % 10 == 7)
seven++;
else if(temp % 10 == 8)
eight++;
else if(temp % 10 == 9)
nine++;
cout << "The digit-sum of " << x << " is " << sum << endl;
return 0;
}
Customer Bill A shop sales 5 types of items, the price of item number 1 is 2.99, the price of item number
2 is 4.5, the price of item number 3 is 9.99, the price of item number 4 is 3.0, the price of item number 5 is
6.0.
Write a program that reads a series of pairs of numbers (item number and quantity sold). Your program
should calculate and display the total customer bill of all items sold. Use -1 as a sentinel value. If the item
number is not valid print an error message.
Sample inputs / outputs:
Enter the item number & Quantity: 2 7
Enter the item number & Quantity: 9 12
21
#include<iostream>
int main()
{
int item,q;
float cost = 0;
while(item != -1)
{
while(item < 0 || item > 5)
{
cout << "\nThe value of the item is out of range" << endl;
if(item == 1)
cost += 2.99 * q;
else if(item == 2)
cost += 4.5 * q;
else if(item == 3)
cost += 9.99 * q;
else if(item == 4)
cost += 3.0 * q;
else if(item == 5)
cost += 6.0 * q;
return 0;
}
22
Grade Point Average Write a program that reads the number of courses N taken by a student. The
program reads the grade and the credit hours for N courses. The program calculates and prints the student
grade-point average GPA. The grades are represented by 4 as the weight of A, by 3 as the weight of B, by
2 as the weight of C, by 1 as the weight of D, and by 0 as the weight of F.
GPA = sum(credit * weight) / sum(credit)
The GPA of the below input / output sample is:
GPA = (3 * 3 + 3 * 4 + 4 * 2 + 2 *1) / (3 + 3 + 4 + 2) = 2.58333
#include<iostream>
int main()
{
int n,credit;
float GPA,pointsSum = 0,creditSum = 0;
char grade;
creditSum += credit;
if(grade == 'A')
pointsSum += credit * 4.0;
return 0;
}
24