Click on any exercise below to see the source code, program output and a brief explanation of the code. Exercises in this section make use of common control flow constructs such Statements and blocks, If-Else, Else-if, Switch, Loops (While, For and Do-While), Break and Continue, Goto and Labels etc.

Available Exercises

Exercise 2.1. Write a complete C program that does the following.
1. It asks the user to enter an integer that is at least 3.
2. The program reads a value x entered by the user. If the value is not legal, the program repeatedly makes the user type in another value until a legal value of x has been entered.
3. The program prints a picture with x columns. For example, if the user enters 3 for x the program should print the following picture.

  Image for exercise 2.1

Exercise 2.2. Write a program that works out the largest and smallest values from a set of 10 inputted numbers.

Exercise 2.3. Write a program which reads two integer values from the command line. If the first is less than the second, print the message up. If the second is less than the first, print the message down. If the numbers are equal, print the message equal. If there is an error reading the data, print a message containing the word Error and perform exit(0);

Exercise 2.4. Read an integer value. Assume it is the number of a month of the year; print out the name of that month.

Exercise 2.5. Write a program to input an integer, n, between 1 and 9, and print a line of output consisting of digits from 1 to n, followed by descending digits from (n-1) to 1. For example, if n = 6, then the output will be 12345654321

Exercise 2.6.Write C code to read an integer value for a student's mark (0-100) and print the correct letter grade based on the following:
Below 50 – F
50 to 59 – C
60 to 69 – B
70 to 79 – B
80 to 89 – A
90 to 100 - A+

Exercise 2.7. Write a program which, given three integers initial, final and increment, prints a table of temperature conversions from Celsius to Fahrenheit. The table ranges from initial to final in steps of increment.