C Decision Making
C decision making exercises with if, else and switch: a calculator, leap year, positive or negative, even or odd, vowel check, voting eligibility and an electricity bill.
Write a C program to check whether a number is even or odd
Description: Take a number as input. Print if it is even or odd.
Hint:
- A number is even when the remainder of the division by 2 is zero.
C program to check even or odd
#include <stdio.h>
int main(void) {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n % 2 == 0) {
printf("%d is even\n", n);
} else {
printf("%d is odd\n", n);
}
return 0;
}
Output:
Enter a number: 27
27 is odd
How it works
27 % 2gives 1, so the number is odd.- For negative numbers it also works, because
-4 % 2is 0.
Write a C program to check whether a user is eligible to vote or not
Description: Take the age of a person as input. If the age is 18 or more, print that the person can vote.
Hint:
- Use the
>=operator withif else.
C program to check the voting eligibility
#include <stdio.h>
int main(void) {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18) {
printf("You are eligible to vote\n");
} else {
printf("You are not eligible to vote\n");
}
return 0;
}
Output:
Enter your age: 17
You are not eligible to vote
How it works
- The condition
age >= 18is false for 17, so theelseblock runs.
Read C If Else Statement.
Write a C program to check whether an alphabet is vowel or consonant
Description: Take an alphabet as input. Print if it is a vowel or a consonant.
Hint:
- The vowels are a, e, i, o, u (in small and capital letters). Convert the letter to lower case with
tolower()to keep the code short.
C program to check vowel or consonant
#include <stdio.h>
#include <ctype.h>
int main(void) {
char ch;
printf("Enter an alphabet: ");
scanf(" %c", &ch);
switch (tolower(ch)) {
case 'a': case 'e': case 'i': case 'o': case 'u':
printf("%c is a vowel\n", ch);
break;
default:
if (isalpha(ch)) {
printf("%c is a consonant\n", ch);
} else {
printf("%c is not an alphabet\n", ch);
}
}
return 0;
}
Output:
Enter an alphabet: E
E is a vowel
How it works
tolower('E')gives'e', which matches a vowel case.- The space before
%cinscanf(" %c")skips the white spaces, like the Enter key. isalpha()checks that the input is a letter.
Read C Switch Statement.
Write a C program to find the maximum number between three numbers
Description: Take three numbers as input and print the largest one.
Hint:
- Compare the numbers using the
>=and&&operators.
C program to find the largest of three numbers
#include <stdio.h>
int main(void) {
int a, b, c, max;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a >= b && a >= c) {
max = a;
} else if (b >= a && b >= c) {
max = b;
} else {
max = c;
}
printf("Largest = %d\n", max);
return 0;
}
Output:
Enter three numbers: 12 45 30
Largest = 45
How it works
- We check if
ais greater than or equal to both others. If not, we checkb, and otherwisecmust be the largest. - Using
>=also works when two numbers are equal.
Write a C program to check whether a number is positive, negative or zero
Description: Take a number as input. Print if it is positive, negative or zero.
Hint:
- Compare the number with 0 using
>and<.
C program to check positive, negative or zero
#include <stdio.h>
int main(void) {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n > 0) {
printf("Positive number\n");
} else if (n < 0) {
printf("Negative number\n");
} else {
printf("Zero\n");
}
return 0;
}
Output:
Enter a number: -8
Negative number
How it works
- The first condition
n > 0is false for -8, the secondn < 0is true, so “Negative number” is printed.
Read Else If Ladder.
Write a C program to calculate an electricity bill
Description: Take the units of electricity used as input and calculate the bill with these rates:
- First 100 units: Rs 5 per unit
- Next 100 units (101 to 200): Rs 7 per unit
- Above 200 units: Rs 10 per unit
C program to calculate the electricity bill
#include <stdio.h>
int main(void) {
int units;
float bill;
printf("Enter units used: ");
scanf("%d", &units);
if (units <= 100) {
bill = units * 5;
} else if (units <= 200) {
bill = 100 * 5 + (units - 100) * 7;
} else {
bill = 100 * 5 + 100 * 7 + (units - 200) * 10;
}
printf("Electricity bill = Rs %.2f\n", bill);
return 0;
}
Output:
Enter units used: 250
Electricity bill = Rs 1700.00
How it works
- For 250 units: first 100 units cost 500, the next 100 units cost 700, and the last 50 units cost 500.
- Total is 500 + 700 + 500 = 1700.
Read Else If Ladder.
Write a C program to check whether two integers are equal or not
Description: Take two integers as input. Print if they are equal or not.
Hint:
- Use the
==operator inside anifstatement.
C program to check if two integers are equal
#include <stdio.h>
int main(void) {
int a, b;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
if (a == b) {
printf("The numbers are equal\n");
} else {
printf("The numbers are not equal\n");
}
return 0;
}
Output:
Enter two integers: 15 15
The numbers are equal
How it works
a == bis true when both values are the same.- Do not write
a = bin the condition. It assigns a value and is always true.
Read C If Else Statement.
Write a C program to check whether a year is a leap year or not
Description: Take a year as input. Print if it is a leap year or not.
Hint:
- A year is a leap year if it is divisible by 400, or it is divisible by 4 but not by 100.
C program to check for a leap year
#include <stdio.h>
int main(void) {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
printf("%d is a leap year\n", year);
} else {
printf("%d is not a leap year\n", year);
}
return 0;
}
Output:
Enter a year: 2024
2024 is a leap year
How it works
- 2024 is divisible by 4 and not by 100, so it is a leap year.
- 1900 is divisible by 100 but not by 400, so it is not. The year 2000 is divisible by 400, so it is.
Read C Operators for the logical operators.
Write a C program to calculate the profit or loss on a product
Description: Take the cost price and selling price of a product. Print the profit or the loss amount. If both are the same, print “No profit, no loss”.
Hint:
- Profit = selling price – cost price (when selling price is more). Loss = cost price – selling price.
C program to calculate profit or loss
#include <stdio.h>
int main(void) {
float cost, sell;
printf("Enter cost price and selling price: ");
scanf("%f %f", &cost, &sell);
if (sell > cost) {
printf("Profit = %.2f\n", sell - cost);
} else if (cost > sell) {
printf("Loss = %.2f\n", cost - sell);
} else {
printf("No profit, no loss\n");
}
return 0;
}
Output:
Enter cost price and selling price: 500 650
Profit = 150.00
How it works
- If the selling price is more than the cost price, the difference is profit.
- If it is less, the difference is loss. Otherwise there is neither.
Read Else If Ladder.
Write a C program to find in which quadrant a coordinate point lies
Description: Take the x and y coordinates of a point. Print the quadrant in which it lies.
Hint:
- Quadrant 1: x > 0 and y > 0. Quadrant 2: x < 0 and y > 0. Quadrant 3: x < 0 and y < 0. Quadrant 4: x > 0 and y < 0.
C program to find the quadrant of a point
#include <stdio.h>
int main(void) {
int x, y;
printf("Enter x and y: ");
scanf("%d %d", &x, &y);
if (x > 0 && y > 0) {
printf("First quadrant\n");
} else if (x < 0 && y > 0) {
printf("Second quadrant\n");
} else if (x < 0 && y < 0) {
printf("Third quadrant\n");
} else if (x > 0 && y < 0) {
printf("Fourth quadrant\n");
} else {
printf("The point is on an axis or at the origin\n");
}
return 0;
}
Output:
Enter x and y: -3 -7
Third quadrant
How it works
- Both values are negative, so the point is in the third quadrant.
- If x or y is zero, the point is on an axis, so the last
elseruns.
Write a C program to make a simple calculator using switch
Description: Take two numbers and an operator (+, -, *, /) as input. Print the result.
Hint:
- Use a
switchon the operator character. Check that the divisor is not zero before dividing.
C program for a simple calculator
#include <stdio.h>
int main(void) {
float a, b;
char op;
printf("Enter number, operator, number: ");
scanf("%f %c %f", &a, &op, &b);
switch (op) {
case '+': printf("Result = %.2f\n", a + b); break;
case '-': printf("Result = %.2f\n", a - b); break;
case '*': printf("Result = %.2f\n", a * b); break;
case '/':
if (b != 0) {
printf("Result = %.2f\n", a / b);
} else {
printf("Division by zero is not allowed\n");
}
break;
default:
printf("Invalid operator\n");
}
return 0;
}
Output:
Enter number, operator, number: 12 / 5
Result = 2.40
How it works
- The input
12 / 5is read intoa,opandb. - The
case '/'runs and prints 2.40. - The
defaultcase handles any other character.
Read C Switch Statement.