Introduction to If Else in C Programming
The if else statement is one of the most important decision-making concepts in C programming. It allows a program to execute different blocks of code depending on whether a condition is true or false. Beginners should practice real-world problems with if else because it builds the foundation for logical thinking and problem-solving.
Below are practical if else questions in C programming with step-by-step explanations and complete solutions. Try writing each program yourself before checking the solution.
Q1: Check Whether a Number Is Positive, Negative, or Zero
Question: Write a C program to accept a number and check whether it is positive, negative, or zero.
Step 1: Take an integer as input from the user.
Step 2: If the number is greater than 0, it is positive.
Step 3: If the number is less than 0, it is negative.
Step 4: Otherwise, the number is zero.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("Positive number");
} else if (num < 0) {
printf("Negative number");
} else {
printf("Zero");
}
return 0;
}
Q2: Check Whether a Number Is Even or Odd
Question: Write a C program to check whether a given number is even or odd.
Step 1: Take a number as input.
Step 2: Divide the number by 2 using the modulus operator.
Step 3: If the remainder is 0, the number is even.
Step 4: Otherwise, the number is odd.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("Even number");
} else {
printf("Odd number");
}
return 0;
}
Q3: Find the Largest of Two Numbers
Question: Write a C program to accept two numbers and find the larger number.
Step 1: Take two numbers as input.
Step 2: Compare the first number with the second number.
Step 3: If the first number is greater, display it as the largest.
Step 4: Otherwise, compare whether the second number is greater or whether both numbers are equal.
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if (a > b) {
printf("%d is the largest", a);
} else if (b > a) {
printf("%d is the largest", b);
} else {
printf("Both numbers are equal");
}
return 0;
}
Q4: Check Student Grade Using Marks
Question: Write a C program to accept marks and display the grade. Use A for 80 or above, B for 60 to 79, C for 40 to 59, and F for below 40.
Step 1: Accept the student's marks.
Step 2: Check whether marks are 80 or above.
Step 3: If not, check the next range using else if.
Step 4: Display the appropriate grade.
#include <stdio.h>
int main() {
int marks;
printf("Enter marks: ");
scanf("%d", &marks);
if (marks >= 80) {
printf("Grade A");
} else if (marks >= 60) {
printf("Grade B");
} else if (marks >= 40) {
printf("Grade C");
} else {
printf("Grade F");
}
return 0;
}
Q5: Check Driving License Eligibility
Question: Write a C program that accepts a person's age and checks whether the person is eligible to apply for a driving license. Assume the minimum age is 18.
Step 1: Take the person's age as input.
Step 2: Check whether the age is greater than or equal to 18.
Step 3: If the condition is true, display that the person is eligible.
Step 4: Otherwise, display that the person is not eligible.
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18) {
printf("Eligible to apply");
} else {
printf("Not eligible to apply");
}
return 0;
}
Q6: Calculate Electricity Bill Based on Units
Question: Write a C program to calculate an electricity bill using different rates. For up to 100 units, charge ₹5 per unit. For 101 to 200 units, charge ₹7 per unit. Above 200 units, charge ₹10 per unit.
Step 1: Take the number of units as input.
Step 2: Check which unit range the customer falls into.
Step 3: Apply the corresponding rate.
Step 4: Display the calculated bill.
#include <stdio.h>
int main() {
int units;
float bill;
printf("Enter electricity units: ");
scanf("%d", &units);
if (units <= 100) {
bill = units * 5;
} else if (units <= 200) {
bill = units * 7;
} else {
bill = units * 10;
}
printf("Electricity Bill = %.2f", bill);
return 0;
}
Q7: Check Whether a Year Is a Leap Year
Question: Write a C program to check whether a given year is a leap year.
Step 1: A year divisible by 400 is a leap year.
Step 2: A year divisible by 100 but not 400 is not a leap year.
Step 3: A year divisible by 4 but not 100 is a leap year.
Step 4: Use if, else if, and else to implement these conditions.
#include <stdio.h>
int main() {
int year;
printf("Enter year: ");
scanf("%d", &year);
if (year % 400 == 0) {
printf("Leap year");
} else if (year % 100 == 0) {
printf("Not a leap year");
} else if (year % 4 == 0) {
printf("Leap year");
} else {
printf("Not a leap year");
}
return 0;
}
Practice Challenges for Students
After completing the solved programs, try these exercises yourself: check whether a person is eligible to vote, find the largest among three numbers using if else, calculate a discount based on shopping amount, check whether a character is a vowel or consonant, determine whether a number is divisible by 5 and 11, calculate profit or loss from cost price and selling price, and create a simple calculator using if else.
Why Practice If Else Questions?
If else exercises help beginners understand conditions, comparison operators, logical operators, decision-making, and program flow. These concepts are used throughout C programming and provide a strong foundation for loops, functions, arrays, pointers, and more advanced programming problems.
Conclusion
Learning if else in C becomes much easier when students solve practical problems instead of only studying syntax. Start with simple conditions such as even or odd, then progress to grading systems, eligibility checks, billing calculations, and multiple-condition problems. Write each program yourself, test different inputs, and gradually create your own variations of these exercises.