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