C++ Exercises. Part 1.

C++ Exercises. Part 1.

Exercises for practicing if statements and loops.

Problem 1. Find the smaller of two integers. (Easy)

Write a program smaller.cpp that asks the user to input two integer numbers and prints out the smaller of the two.

Example

$ ./smaller
Enter the first number: 15
Enter the second number: -24

The smaller of the two is -24

Problem 2. Find the smaller of three integers. (Medium)

Write a program smaller3.cpp that asks the user to input three integer numbers, and prints out the smallest of the three.

(Hint: There are many possible solutions here. One possible strategy: Given number x, y, and z, you can first compare x and y, take whichever is smaller and compare it with z.)

Example

$ ./smaller3
Enter the first number: 23
Enter the second number: 76
Enter the third number: 37

The smaller of the three is 23

Problem 3. A leap year or a common year? (Medium)

Introduction about the modulo operator (%):

In C++, the operator % lets us compute the remainder of the division of x by y. In particular, 37 % 10 would return 7, since this is the remainder of 37 when divided by 10.

The task:

Write a program leap.cpp that asks the user to input an integer representing a year number (1999, 2016, etc.). If the input year is a leap year according to the modern Gregorian calendar, it should print Leap year, otherwise, print Common year.

In the modern Gregorian calendar, a year is a leap year if it is divisible by 4, but century years are not leap years unless they are divisible by 400.

This means that 2010, 2016, 2020, and 2040 are all leap years.
However, the century years 1800, 1900, 2100, 2200, 2300 and 2500 are NOT.
Yet, 2000, 2400, 2800 are still leap years.

Example 1

$ ./leap
Enter the year: 2016

Leap year

Example 2

$ ./leap
Enter the year: 2017

Common year

Problem 4. Possible or impossible date? (Hard)

Write a program possible.cpp that asks the user to input a date: Month, Day, and Year, and tells the user whether the input date is Possible or Impossible (according to the modern Gregorian calendar).

Example 1

$ ./possible
Enter the month: 9
Enter the day: 13
Enter the year: 2016

Possible

Example 2

$ ./possible
Enter the month: 777
Enter the day: 0
Enter the year: 2017

Impossible

You may assume that all input Month, Day, and Year are non-negative integers (≥ 0).

Possible dates are those that could happen in the past or in the future, so for instance month 14 is impossible, or June 31 is impossible as well. The caveat is that on a leap year, February has 29 days, while there are only 28 on a normal year.

Examples of possible dates: 9/15/2017, 9/30/1500, 5/31/2500, etc.

Examples of impossible dates: 27/99/1989, 6/31/2016, 0/32/2000, etc.

To help you save one internet search, here’s how many days is in each of the months: jan 31, feb 28/29, mar 31, apr 30, may 31, jun 30, jul 31, aug 31, sep 30, oct 31, nov 30, dec 31.

Problem 5. Divisible by 14. (Easy)

Write a program div14.cpp that prints out all integers between 1 and 100 that are divisible by 14.

(Hint: Will need to use a for loop and the modulo operator %)

Example

$ ./div14
14
28
42
56
70
84
98

Problem 6. A simple primality test. (Medium)

A natural number greater than 1 is called a prime if it is divisible only by 1 and by itself. So, for exmaple, the first few primes are

2, 3, 5, 7, 11, 13, 17, 19, 23, …

There is a simple way to test if a given number x is a prime: it should be greater than 1 and should not divide evenly by any of the numbers between 2 and x-1.

Write a program prime.cpp that decides whether or not the input number is a prime.

Example 1

$ ./prime
Enter an integer: 19

It is a prime.

Example 2

$ ./prime
Enter an integer: 25

It is NOT a prime.

Problem 7. All primes between 1 and 100. (Medium)

Write a program that prints all primes between 1 and 100.

Problem 8. ASCII table. (Medium)

In this task, you learn about the char type and the standard computer character encoding called ASCII table.

In C and C++, char is the datatype representing characters, for example one can write:

int main() {
  char c1 = 'A';
  char c2 = 'a';
  char c3 = '$';
  cout << c1 << c2 << c3 << endl;
}

Output:

Aa$

For historical reasons char is actually an integer type one byte long. Depending on your computer architecture the range of its values can be either from -127 to 127 or from 0 to 255.

The exact mapping between the code and the character is called the ASCII table.

All characters in the range from 32 to 126 are printable characters.

Write a program that will print out all of these printable characters together with their codes. (Also note that you may need to typecast a variable of the char type to int if you want to print the code of the character instead of its sybmol.)

Example

$ ./a.out
32  
33 !
34 "
35 #
36 $
37 %
38 &

...

123 {
124 |
125 }
126 ~