Repetition

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 24

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.

Sample inputs / outputs:


Enter a positive integer: 7
Enter 7 characters: t 8 & D Q + s Y 3 J
There are 2 upper case letters

#include<iostream>

using namespace std;

int main()
{
int n, upper = 0, k = 0;
char letter;

cout << "Enter a positive integer: ";


cin >> n;

cout << "Enter " << n << " characters: ";


while(k < n)
{
cin >> letter;
if('A' <= letter && letter <= 'Z')
upper++;
k++;
}

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 <= 1 || number >= 25)


continue;
else
break;
}

if(number % 2 == 0)
cout << number << " is an even number\n";
else
cout << number << " is an odd number\n";

cout << "Do you want to try again?\n";


cout << "Y or y for yes, N or n for no: ";
while(true) {
cin >> letter;
if(letter == 'Y' || letter == 'y' || letter == 'N' || letter == 'n')
break;
else
cout << "Invalid letter, enter Y, y, N, or n: ";
}
if(letter == 'N' || letter == 'n')
break;
2
}

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.

Sample inputs / outputs:


Enter a positive integer: 20816
The inverse of 20816 is 61802

#include <iostream>

using namespace std;

int main()
{
int number, digit, sum = 0;

cout << "Enter a positive integer: ";


cin >> number;

cout << "The inverse of " << number;


while(number > 0)
{
digit = number % 10;
number = number / 10;
sum = 10*sum + digit;
}

cout << " is " << sum << endl;

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].

Sample inputs / outputs:


Enter the lower and upper limits of an interval: 17 28
Enter a number within the interval [17. 28]: 21
The number 21 is the 5th number

#include <iostream>

using namespace std;

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;

while(k <= upper)


{
if(k == number)
break;
k++;
location++;
}

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.

Sample inputs / outputs:

Enter a positive integer: 2646798


2646798 is an ascending power number

#include <iostream>

using namespace std;

int main()
{
int number, n, d, k = 0, i=1, sum = 0;

cout << "Enter a positive integer: ";


cin >> number;

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>

using namespace std;

int main()
{
int N, i;
float sum = 0, sign = -1;

cout << "Enter how many terms: ";


cin >> N;

for(i=0; i<N; i++)


{
sign = -sign;
sum = sum + sign / (2*i+1);
}

cout << "The value of pi to " << N << " terms is: " << 4*sum << endl;

return 0;
}

7
Exercise #7 The Cats 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 cats 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 cats energy drops to 5, then after a second hour of eating the cats energy
increases to 7, after the third hour of walk the cats energy drops to 3, then after a fourth hour of eating the
cats energy increases to 5, after the fifth hour of walk the cats energy drops to 1, then after a sixth hour
of eating the cats energy increases to 3, finally after the seventh hour of walk the cats energy drops to -1.

Sample inputs / outputs:


Enter C, B, and E: 9 4 2
For 9 calories with walk 4 and eat 2 the cat needs 7 hours to stop.

#include<iostream>
using namespace std;

int main()
{
int C, B, E, hour = 0, temp;

cout<<"Enter C, B, and E: ";


cin >> C >> B >> E;
temp = C;

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>

using namespace std;

int main()
{
int n, s0=1, s1=2, s2, k = 1;

cout << "Enter which term: ";


cin >> n;

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

There are 15 new generated numbers, so the sequence length is 15.

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>

using namespace std;

int main()
{
int n, k = 1;

cout << "Enter an integer: ";


cin >> n;

cout << "The values of the sequence are: " << n;


while(n != 1)
{
if(n%2 == 0)
n = n/2;
else
n = 3*n+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.

Sample inputs / outputs:


Enter an even integer: 12
Enter a number 6 decimal numbers: 7.1 9.3 12.3 29.7 0.4 8.9
Enter a number 6 decimal numbers: 31.8 102.3 8.2 12.5 33.1 87.2
The minimum is 0.4 and the maximum is 102.3

#include<iostream>

using namespace std;

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)

The rules of rock, paper, and scissors are:


 Both players have to say either "rock", "paper" or "scissors" at the same time.
 Rock beats scissors, paper beats rock, scissors beat paper.

Sample input/output:
Enter the players parameter: rock paper
Player 2 wins

#include <iostream>

using namespace std;

int main()
{
char op1, op2;

cout << "Enter the players parameter: ";


cin >> 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>

using namespace std;

int main()
{
int n, i, j, sum = 0;

cout << "Enter an integer: ";


cin >> n;

for(i=2; i<n; i++)


{
if(n%i == 0)
{
for(j=2; j<i; j++)
if(i%j == 0)
sum = sum + j;
}
}

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.

Sample input / output:


Enter the day: 7
After 7 days, the total pay is 127

#include <iostream>

using namespace std;

int main()
{
int total = 0, pay = 1, day, i;

cout << "Enter the day: ";


cin >> day;

for(i=0; i<day; i++)


{
total = total + pay;
pay = 2 * pay;
}

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

Sample input / output:


Enter the lower and upper integers: 17 341
The sum of all digits of the interval [17, 341] is 3302

#include <iostream>

using namespace std;

int main()
{
int total = 0, lower, upper, i, sum, number, digit;

cout << "Enter the lower and upper integers: ";


cin >> lower >> upper;

for(i=lower; i<=upper; i++)


{
number = i;
sum = 0;
while(number > 0)
{
digit = number % 10;
number = number / 10;
sum = sum + digit;
}
total = total + sum;
}

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.

Sample input / output:


Enter a positive integer: 137 Enter a positive integer: 357
137 is a prime number 357 is not a prime number

#include <iostream>

using namespace std;

int main()
{
int number, i;

cout << "Enter a positive integer: ";


cin >> number;

for(i=2; i<number; i++)


{
if(number % i == 0)
break;
}

if(i < number)


cout << number << " is not a prime number" << endl;
else
cout << number << " is a prime number" << endl;

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.

Sample inputs / outputs:


Enter how many digits: 5
Enter 5 digits: 7 3 5 2 1
The new number is 12537

#include<iostream>

using namespace std;

int main()
{
int n,number=0,x,finalAnswer=0,temp;

cout << "Enter how many digits: ";


cin >> n;

cout << "Enter " << n << " digits: ";


for(int i=0; i<n; i++)
{
cin >> x;
number = number * 10 + x;

temp = number;

while(temp > 0)
{
finalAnswer = finalAnswer * 10 + temp % 10;
temp = temp / 10;
}

cout << "The new number is " << finalAnswer;

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.

Sample inputs / outputs:


Enter the k number: 4
Enter a lower-case letter, 0 to stop: s
Enter a lower-case letter, 0 to stop: h
Enter a lower-case letter, 0 to stop: t
Enter a lower-case letter, 0 to stop: q
Enter a lower-case letter, 0 to stop: i
Enter a lower-case letter, 0 to stop: x
Enter a lower-case letter, 0 to stop: e
Enter a lower-case letter, 0 to stop: w
Enter a lower-case letter, 0 to stop: 0
A scrambled version of the letters is wlxumbia

#include<iostream>

using namespace std;

int main()
{
int k;
char c,x;
string word;

cout << "Enter the k number: ";


cin >> k;

cout << "Enter a lower-case letter, 0 to stop: ";


cin >> c;

while(c != '0')
{
x = (int(c) - 97 + k) % 26 + 97;

word += x;

cout << "Enter a lower-case letter, 0 to stop: ";


cin >> c;

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.

Sample inputs / outputs:


Enter two numbers: 220 284
The two numbers 220 and 284 are related

#include<iostream>

using namespace std;

int main()
{
int x,y,sum1=0,sum2=0;

cout << "Enter two numbers: ";


cin >> x >> y;

for(int i = 1; i < x; i++)


{
if(x % i == 0)
sum1 += i;
}

for(int i=1; i < y ; i++)


{
if(y % i == 0)
sum2 += i;
}

if(sum1 == y && sum2 == x)


cout << "The two numbers " << x << " and " << y << " are related";

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.

Sample inputs / outputs:

Please enter a positive integer: 1538525


The digit-sum of 1538525 is 29
There are:
0 zeros, 1 ones, 1 tows, 1 threes, 0 fours
3 fives, 0 sixes, 0 sevens, 1 eights, 0 nine

#include<iostream>

using namespace std;

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;

cout << "Please enter a positive integer: ";


cin >> x;

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++;

temp = temp / 10;

cout << "The digit-sum of " << x << " is " << sum << endl;

cout << "There are:" << endl;


cout << zero << " zeros, " << one << " ones, " << two << " twos, " << three << " threes, " << four << "
fours" << endl;
cout << five << " fives, " << six << " sixes, " << seven << " sevens, " << eight << " eights, " << nine
<< " nines";

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

The value of the item is out of range

Enter the item number & Quantity: 1 5


Enter the item number & Quantity: -1 0
Total bill is 46.45

21
#include<iostream>

using namespace std;

int main()
{
int item,q;
float cost = 0;

cout << "Enter the item number & Quantity: ";


cin >> item >> q;

while(item != -1)
{
while(item < 0 || item > 5)
{
cout << "\nThe value of the item is out of range" << endl;

cout << "\nEnter the item number & Quantity: ";


cin >> item >> q;
}

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;

cout << "Enter the item number & Quantity: ";


cin >> item >> q;

cout << "Total bill is " << cost;

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

Sample inputs / outputs:

Enter the number of courses: 4


Enter the grade and credit of course number 1: B 3
Enter the grade and credit of course number 2: A 3
Enter the grade and credit of course number 3: C 4
Enter the grade and credit of course number 4: D 2
The student GPA is 2.58333

#include<iostream>

using namespace std;

int main()
{
int n,credit;
float GPA,pointsSum = 0,creditSum = 0;
char grade;

cout << "Enter the number of courses: ";


cin >> n;

for(int i=0; i<n; i++)


{
cout << "Enter the grade and credit of course number " << i+1 << ": ";
cin >> grade >> credit;

creditSum += credit;

if(grade == 'A')
pointsSum += credit * 4.0;

else if (grade == 'B')


pointsSum += credit * 3.0;

else if(grade == 'C')


pointsSum += credit * 2.0;

else if(grade == 'D')


pointsSum += credit * 1.0;
23
else if(grade == 'F')
pointsSum += credit * 0.0;

GPA = pointsSum / creditSum;

cout << "The student GPA is " << GPA;

return 0;
}

24

You might also like