Mastering Control Statements in C Programming: A Hands-on Guide with Basic Exercises |
Introduction
Control
statements are the backbone of programming, allowing developers to dictate the
flow of execution based on specific conditions. In this blog post, we’ll
explore a variety of beginner and intermediate exercises focused on conditional control statements in C
programming. Each exercise is accompanied by a code solution and a short
explanation to help you understand and implement these concepts effectively.
Prerequisite
To practice this project, ensure that you have Code::Blocks installed on your system. If not, you can download it from the official Code::Blocks Download Page.
Exercise 1: Check If a Number is Even or Odd
Question:
Write a
program to check weather a number input through the keyboard is even or odd.
Code:
#include <stdio.h>
int main()
{
int number;
printf("---------------------------------------------------------");
printf("\nThis program will test if the input number is even or odd");
printf("\n---------------------------------------------------------");
printf("\nInput an integer number: ");
scanf("%d", &number);
if(number % 2 == 0)
{
printf("\nThe number is Even.");
}
else
{
printf("\nThe number is Odd.");
}
}
Explanation:
The code
utilizes the modulo operator (%
) to check for evenness. If the remainder after
dividing the number by 2 is 0, the number is even; otherwise, it’s odd.
Sample Input/Output:
Exercise 1 Output |
- Input: 15
- Output: The number is Odd.
- Input: 24 (Try This)
- Output: The number is Even.
Exercise 2: Maximum Value of Two Numbers
Question:
Write a
program to print the maximum out of two numbers.
Code:
#include <stdio.h>
int main()
{
printf("\n-----------------------------------------------------------");
printf("\nThis program will test the maximum out of two input numbers");
printf("\n-----------------------------------------------------------");
int number1, number2;
printf("\nEnter two different integer number (space the number): ");
scanf("%d%d", &number1, &number2);
if(number1 >= number2)
{
printf("\nThe First number is the Maximum number");
}
else
{
printf("\nThe Second number is the maximum number");
}
}
Explanation:
The code
uses an if-else statement to compare the two numbers. If number1 is greater
than or equal to number2, number1 is the maximum; otherwise, number2 is the
maximum.
Sample Input/Output:
- Input: 10 5
- Output: The first number is the Maximum number.
- Input: 3 8 (Try This)
- Output: The second number is the maximum number
Exercise 3: Maximum Value of Three numbers
Question:
Write a
program to print the maximum out of three numbers.
Code:
#include <stdio.h>
int main()
{
printf("\n--------------------------------------------------------------");
printf("\nThis program test the maximum number among 3 different numbers");
printf("\n--------------------------------------------------------------");
int number1, number2, number3;
printf("\n\nInput 3 different integer numbers (space the numbers): ");
scanf("%d%d%d", &number1, &number2, &number3);
if(number1 >= number2 && number1 >= number3)
{
printf("\nThe First number is the maximum number.");
}
else if(number2 >= number1 && number2 >= number3)
{
printf("\nThe Second number is the maximum number.");
}
else
{
printf("\nThe third number is the maximum number.");
}
}
Explanation:
The code
employs nested if-else statements to compare the three numbers and determine
the maximum.
Sample Input/Output:
- Input: 15 7 20
- Output: The third number is the maximum number.
- Input: 25 30 18
- Output: The second number is the maximum number.
Exercise 4: Maximum Value of Four Numbers
Question:
Write a
program to print the maximum out of four numbers, using Nested-if-else and short-logic.
Code (Nested if-else):
#include <stdio.h>
int main()
{
printf("\n----------------------------------------------------------------------");
printf("\nThis program will test the maximum number among four different numbers");
printf("\n----------------------------------------------------------------------");
int number1, number2, number3, number4;
printf("\n\nInput 4 different integer numbers (space the inputs): ");
scanf("%d%d%d%d", &number1, &number2, &number3, &number4);
if(number1 >= number2 && number1 >= number3 && number1 >= number4)
{
printf("\nThe first number is the maximum number\n");
}
else if(number2 >= number1 && number2 >= number3 && number2 >= number4)
{
printf("\nThe second number is the maximum number\n");
}
else if(number3 >= number1 && number3 >= number2 && number3 >= number4)
{
printf("\nThe third number is the maximum number\n");
}
else
{
printf("\nThe fourth number is the maximum number\n");
}
}
Code (Short-logic):
#include <stdio.h>
int main()
{
printf("\n----------------------------------------------------------------");
printf("\nThis program will test four different number to know the maximum");
printf("\n----------------------------------------------------------------");
int num1, num2, num3, num4, maxi;
printf("\n\nInput 4 different integer numbers (space the input): ");
scanf("%d%d%d%d", &num1, &num2, &num3, &num4);
maxi = num1;
if(num2 > maxi)
{
maxi = num2;
}
if(num3 > maxi)
{
maxi = num3;
}
if(num4 > maxi)
{
maxi = num4;
}
printf("\nThe maximum number is %d\n", maxi);
}
Explanation:
- Nested if-else: Uses nested if-else statements to compare all four numbers.
- Short-logic: Uses a temporary variable (maxi) to store the maximum value found so far.
Sample Input/Output:
- Input: 10 25 8 15
- Output: The second number is the maximum number
Exercise 4 Output |
Exercise 5: Second maximum of Three Numbers
Question:
Write a
program to print the second maximum out of three numbers.
Code:
#include <stdio.h>
int main()
{
printf("\n---------------------------------------------------------------");
printf("\nThe program print out the second maximum number from the 3 inputs");
printf("\n---------------------------------------------------------------");
int num1, num2, num3, sec;
printf("\n\nEnter 3 different integer numbers (space the inputs): ");
scanf("%d%d%d", &num1, &num2, &num3);
if((num1 > num2 && num1 < num3) || (num1 > num3 && num1 < num2))
{
sec = num1;
}
else if((num2 > num1 && num2 < num3) || (num2 > num3 && num2 < num3))
{
sec = num2;
}
else
{
sec = num3;
}
printf("\nThe second maximum number is %d\n", sec);
}
Explanation:
The code
determines the second-largest number by checking if a number is greater than
one number and less than another.
Sample Input/Output:
- Input: 10 20 15
- Output: The second maximum number is 15
Exercise 5 Output |
Exercise 6: Check for Leap Year
Question:
Write a program to check whether a year is leap year or not.
Code:
#include <stdio.h>
int main()
{
printf("\n---------------------------------------------------------");
printf("\nThis program will tell weather a year is leap year or not");
printf("\n---------------------------------------------------------");
int year;
printf("\nInput the year: ");
scanf("%d", &year);
if((year % 4 == 0 && year % 100 != 0)|| (year % 400 == 0))
{
printf("\nThe year %d is a leap year\n", year);
}
else
{
printf("\nThe year %d is not a leap year\n", year);
}
}
Explanation:
The code uses the following conditions to check for a leap year:
- Divisible by 4.
- Not divisible by 100.
- Or divisible by 400.
Sample Input/Output:
Exercise 6 Output |
- Input: 2024
- Output: The year 2024 is a leap year
- Input: 1900
- Output: The year 1900 is not leap year.
Exercise 7: Calculate Student Grade
Question:
Write a program to calculate the grade of a student after the input of marks of that student, given the conditions that:
- 90 or above: Grade A
- 70-89: Grade B
- 50-69: Grade C
- Below 50: Grade F
Code:
#include <stdio.h>
int main()
{
printf("\n----------------------------------------------------------------");
printf("\nThis program will award the appropriate Grade to the test score.");
printf("\n----------------------------------------------------------------");
int score;
printf("\n\nEnter the Test Score: ");
scanf("%d", &score);
if(score >= 90)
printf("\nGrade: A");
else if(score >= 70 && score <= 90)
printf("\nGrade: B");
else if(score >= 50 && score <= 70)
printf("\nGrade: C");
else
printf("\nGrade: F");
}
Sample Input/output:
Exercise 7 Output |
- Input: 85
- Output: Grade: B
- Input: 92 (Try This)
- Output: Grade: A
Exercise 8: Calculate Telephone Bill
Question:
Write a program to compute the telephone bill of a customer. Monthly Rental is 100 NGN. Rates are as follows:
- No of calls 1 to 100 rate = 0
- No of calls 101 to 200 rate = 0.80 NGN per call
- No of calls 201 to 500 rate = 1.00 NGN per call
- No of calls 501 above rate = 1.20 NGN per call
Code:
#include <stdio.h>
int main()
{
printf("\n-----------------------------------------------------------------------");
printf("\nThis Program will compute the telephone bill of a customer in a month.");
printf("\n-----------------------------------------------------------------------");
int calls;
printf("\nEnter the number of calls made in a month: ");
scanf("%d", &calls);
// Monthly rental is 100.0
float bill = 0.00;
if(calls >= 1 && calls <= 100)
{
bill = (0.00 * 100.00);
printf("\nYour Telephone bill for the month is %.2f NGN\n", bill);
}
else if(calls >= 101 && calls <= 200)
{
bill = (0.80 * 100.00);
printf("\nYour Telephone bill for the month is %.2f NGN\n", bill);
}
else if (calls >= 201 && calls <= 500)
{
bill = (1.00 * 100.00);
printf("\nYour Telephone bill for the month is %.2f NGN\n", bill);
}
else
{
bill = (1.20 * 100.00);
printf("\nYour Telephone bill for the month is %.2f NGN\n", bill);
}
}
Sample Input/output:
- Input: 150
- Output: Your Telephone bill for the month is 80.00 NGN
Exercise 8 Output |
Exercise 9: Computes Employee Pension
Question:
Write a program to compute the pension of an employees.
If the person is male:
- Age >= 90 pension is 4000
- Age >= 60 pension is 6000
- Age < 60 pension is 0
If the person is female:
- Age >= 90 pension is 3000
- Age >= 60 pension is 5000
- Age < 60 pension is 0
Code:
#include <stdio.h>
int main()
{
printf("\n------------------------------------------------------------------");
printf("\nThis program will compute the pension of male and female employee");
printf("\n------------------------------------------------------------------");
int sex;
int age;
printf("\nEnter the Employees' age: ");
scanf("%d", &age);
printf("\nWhat is the sex of the Employee?\n1 Male\n2 Female\n");
scanf("%d", &sex);
switch(sex)
{
case 1:
{
if(age >= 90)
printf("\nEmployee Pension is 4000.00 NGN\n");
else if(age >=60 && age <= 90)
printf("\nEmployee Pension is 6000.00 NGN\n");
else
printf("\nEmployee Pension is 0.00 NGN\n");
break;
}
case 2:
{
if(age >= 90)
printf("\nEmployee Pension is 3000.00 NGN\n");
else if(age >=60 && age <= 90)
printf("\nEmployee Pension is 5000.00 NGN\n");
else
printf("\nEmployee Pension is 0.00 NGN\n");
break;
}
}
return (0);
}
Explanation:
- The code uses a switch-case statement to handle different genders.
- Nested if-else statements within each case determine the pension based on age.
Sample Input/Output:
Exercise 9 Output |
- Input: Age 65, Sex: 1 (Male)
- Output: Employee pension is 6000.00 NGN
Exercise 10: Magic Number
Question:
Write a program to check weather a 3 digit number is a magic number or
not. (Palindrome) A number is a magic number if its reverse is same as the
original number.
Code:
#include <stdio.h>
int main()
{
printf("\n-------------------------------");
printf("\nCHECK IF YOUR NUMBER IS MAGIC");
printf("\n-------------------------------");
int number;
printf("\nEnter a 3 digit number: ");
scanf("%d", &number);
if(number >=100 && number <= 999)
{
int magic = number;
int rev_number = 0;
int last_digit;
last_digit = magic%10;
rev_number = rev_number*10+last_digit;
magic = magic / 10;
last_digit = magic%10;
rev_number = rev_number*10+last_digit;
magic = magic/10;
last_digit = magic%10;
rev_number = rev_number*10+last_digit;
magic = magic/10;
if(rev_number == number)
{
printf("\nYour number %d is a MAGIC number\n", number);
} else
{
printf("\nYour number %d is not a MAGIC number\n", number);
}
}
else
{
printf("\nYou did not Enter a 3 digit number\n");
}
}
Explanation:
The code extracts each digit of the number and reconstructs the reverse
of the number. It then compares the original number with its reverse.
Sample Input/Output:
- Input: 121
- Output: Your number 121 is a MAGIC number.
Exercise 10 Output |
Exercise 11: Check the Character Type
Question:
Any character is entered through the keyboard, write a program to
determine whether the character entered is a capital letter, a small case
letter, a digit or a special symbol.
Code:
#include <stdio.h>
int main()
{
printf("\n----------------------------------------------------------");
printf("\nThis program tells you the character type you've inputted");
printf("\n----------------------------------------------------------");
char ch;
printf("\nEnter any character: ");
scanf("%c", &ch);
if(ch >= 'A' && ch <= 'Z')
{
printf("\nThe %c Character is a Capital Letter\n", ch);
}
else if(ch >= 'a' && ch <= 'z')
{
printf("\nThe %c Character is a Small letter\n", ch);
}
else if(ch >= '0' && ch <= '9')
{
printf("\nThe %c Character is a Digit\n", ch);
}
else
{
printf("\nThe %c Character is a Special Character\n", ch);
}
}
Explanation:
The code uses if-ese statements and character comparisons to determine the character type:
- ch >= ‘A’ && ch <= ‘Z’ : Checks for uppercase letters.
- ch >= ‘a’ && ch <= ‘z’ : Checks for lowercase letters.
- ch >= ‘0’ && ch <= ‘9’ : Checks for digits.
- If none of the above conditions are met, the character is considered a special symbol.
Sample Input/Output:
Exercise 11 Output |
- Input: A
- Output: The A Character is a Capital Letter.
- Input: * (Try This)
- Output: The * Character is a Special Character.
Exercise 12: Leap Year (With Logical Operators)
Question:
Write a program using conditional operators to determine whether a year entered
through the keyboard is a leap year or not.
Code:
#include <stdio.h>
int main()
{
printf("\n-----------------------------------------------------------------");
printf("\nThis program will check for Leap year using logical operator");
printf("\n-----------------------------------------------------------------");
int year;
printf("\nEnter the year: ");
scanf("%d", &year);
((year%4 == 0 && year%100 != 0) || (year%400 == 0))?
printf("\nThe year %d is a leap year\n", year):printf("\nThe year %d is not a leap year\n", year);
}
Explanation:
The code uses the ternary operator (? :) and logical operators (&&, ||) to concisely check the leap year conditions:
- Year % 4 == 0 && year % 100 != 0 : The year is divisible by 4 and not divisible by 100
- Year % 400 == 0 : The year is divisible by 400.
Sample Input/Output:
- Input: 2024 (Try This)
- Output: The year 2024 is a leap year
- Input: 1900
- Output: The year 1900 is not leap year.
I hope this
post has been helpful! Feel free to experiment with these exercises and modify then
to explore different approaches and enhance your understanding of C
programming.
Don't forget to check our C programming page for more programming projects.
Follow us on our social media handles to stay updated.