C++ Exercises. Part 1.

C++ Exercises. Part 1.

Exercises for practicing if statement.

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

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

Example

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

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)

In C++, the operator % (usage example (x % y)) let 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. Think, what would be the result of 1234567 % 200? (You may write a short program if you want to check your answer.)

The actual task: Write a program that asks the user to input an integer representing a year number (1999, 2016, etc.). If the input year is a leap year, 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

Enter the year: 2016

Leap year

Problem 4. Possible or impossible date? (Hard)

Ask the user to input three integers, representing Month, Day, and Year (ask for input in this exact order). The program should decide and report to the user whether the numbers correspond to a possible date (like 9/15/2016), or it is impossible (for example, 27/99/1989, 6/31/2016, 0/32/2000, etc.)

You may assume that all input numbers are non-negative integers (so assume they can be positive or equal to zero).

The caveat is that on a leap year, February has 29 days, while there are only 28 on a normal year.

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.

Example

Enter the month: 9
Enter the day: 13
Enter the year: 2016

Possible

Solution

possible-impossible (hopefully there are no mistakes there, but I did not test it very extensively)

Exercises for practicing loops.

Problem 5. Divisible by 14. (Easy)

Write a program 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

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 that decides whether or not the input number is a prime.

Example

Enter an integer: 19

It is a 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

32  
33 !
34 "
35 #
36 $
37 %
38 &

...

123 {
124 |
125 }
126 ~