Conditional statements help a program make decisions based on different conditions. In C programming, decision-making is performed using statements like if, if-else, else-if, and switch. These statements allow programs to execute different blocks of code depending on whether a condition is true or false.
Conditional statements are widely used in real-world applications such as login systems, grading software, banking applications, billing systems, and eligibility checks. In this chapter, you’ll practice beginner-friendly C conditional statement programs with complete solutions, sample outputs, explanations, and concepts covered. C Conditional Statements Practice questions with solutions help to understand the concepts.
1. C Program to Check Whether a Number is Positive or Negative
Problem Statement
Write a C program to check whether a given number is positive or negative.
C Solution
#include <stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
if(number >= 0)
{
printf("Positive Number");
}
else
{
printf("Negative Number");
}
return 0;
}
Sample Output
Enter a number: 25
Positive Number
Explanation
The program compares the entered number with zero.
- If the number is greater than or equal to zero, it is positive.
- Otherwise, it is negative.
Concepts Covered
- if-else Statement
- Relational Operator
- Decision Making
2. C Program to Check Whether a Number is Even or Odd
Problem Statement
Write a C program to determine whether a number is even or odd.
C Solution
#include <stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
if(number % 2 == 0)
{
printf("Even Number");
}
else
{
printf("Odd Number");
}
return 0;
}
Sample Output
Enter a number: 18
Even Number
Explanation
The modulus operator (%) returns the remainder after division.
- If the remainder is 0, the number is even.
- Otherwise, it is odd.
Concepts Covered
- if-else Statement
- Modulus Operator
- Even and Odd Numbers
3. C Program to Find the Largest of Two Numbers
Problem Statement
Write a C program to find the larger of two numbers entered by the user.
C Solution
#include <stdio.h>
int main()
{
int first, second;
printf("Enter first number: ");
scanf("%d", &first);
printf("Enter second number: ");
scanf("%d", &second);
if(first > second)
{
printf("%d is Greater", first);
}
else
{
printf("%d is Greater", second);
}
return 0;
}
Sample Output
Enter first number: 35
Enter second number: 20
35 is Greater
Explanation
The greater-than (>) operator compares the two numbers. The program prints the larger value based on the comparison.
Concepts Covered
- if-else Statement
- Relational Operators
- Comparison
4. C Program to Find the Largest of Three Numbers
Problem Statement
Write a C program to find the largest among three numbers entered by the user.
C Solution
#include <stdio.h>
int main()
{
int first, second, third;
printf("Enter first number: ");
scanf("%d", &first);
printf("Enter second number: ");
scanf("%d", &second);
printf("Enter third number: ");
scanf("%d", &third);
if(first >= second && first >= third)
{
printf("%d is the Largest Number", first);
}
else if(second >= first && second >= third)
{
printf("%d is the Largest Number", second);
}
else
{
printf("%d is the Largest Number", third);
}
return 0;
}
Sample Output
Enter first number: 15
Enter second number: 40
Enter third number: 28
40 is the Largest Number
Explanation
The program compares all three numbers using if, else if, and else.
- If the first number is greater than or equal to the other two, it is the largest.
- Otherwise, the second number is checked.
- If neither the first nor the second is the largest, the third number is displayed.
Concepts Covered
- if-else Ladder
- Logical AND (
&&) - Relational Operators
- Decision Making
5. C Program to Check Whether a Year is a Leap Year
Problem Statement
Write a C program to check whether a given year is a leap year.
C Solution
#include <stdio.h>
int main()
{
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.", year);
}
else
{
printf("%d is Not a Leap Year.", year);
}
return 0;
}
Sample Output
Enter a year: 2024
2024 is a Leap Year.
Explanation
A year is considered a Leap Year if:
- It is divisible by 400, OR
- It is divisible by 4 but not divisible by 100.
This is the standard leap year rule used in the Gregorian calendar.
Concepts Covered
- if-else Statement
- Logical Operators
- Modulus Operator (
%) - Leap Year Logic
6. C Program to Check Whether a Number is Divisible by 5 and 11
Problem Statement
Write a C program to check whether a number is divisible by both 5 and 11.
C Solution
#include <stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
if(number % 5 == 0 && number % 11 == 0)
{
printf("%d is divisible by both 5 and 11.", number);
}
else
{
printf("%d is not divisible by both 5 and 11.", number);
}
return 0;
}
Sample Output
Enter a number: 55
55 is divisible by both 5 and 11.
Explanation
The program uses the modulus operator (%) to check divisibility.
- If the remainder is 0 for both 5 and 11, the number is divisible by both.
Concepts Covered
- if-else Statement
- Logical AND (
&&) - Modulus Operator
7. C Program to Check Whether a Character is a Vowel or Consonant
Problem Statement
Write a C program to check whether a given alphabet is a vowel or a consonant.
C Solution
#include <stdio.h>
int main()
{
char ch;
printf("Enter an alphabet: ");
scanf(" %c", &ch);
if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U' ||
ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
{
printf("%c is a Vowel.", ch);
}
else
{
printf("%c is a Consonant.", ch);
}
return 0;
}
Sample Output
Enter an alphabet: A
A is a Vowel.
Explanation
The program compares the entered character with all vowel characters.
If it matches any vowel, it prints Vowel; otherwise, it prints Consonant.
Concepts Covered
- Character Comparison
- Logical OR (
||) - if-else Statement
8. C Program to Check Whether a Character is an Alphabet
Problem Statement
Write a C program to determine whether a given character is an alphabet.
C Solution
#include <stdio.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf(" %c", &ch);
if((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
{
printf("%c is an Alphabet.", ch);
}
else
{
printf("%c is not an Alphabet.", ch);
}
return 0;
}
Sample Output
Enter a character: G
G is an Alphabet.
Explanation
The program checks whether the entered character falls within the ASCII ranges:
AtoZatoz
If yes, it is an alphabet.
Concepts Covered
- ASCII Values
- Logical Operators
- Character Comparison
9. C Program to Check Whether an Alphabet is Uppercase or Lowercase
Problem Statement
Write a C program to determine whether an entered alphabet is uppercase or lowercase.
C Solution
#include <stdio.h>
int main()
{
char ch;
printf("Enter an alphabet: ");
scanf(" %c", &ch);
if(ch >= 'A' && ch <= 'Z')
{
printf("%c is an Uppercase Alphabet.", ch);
}
else
{
printf("%c is a Lowercase Alphabet.", ch);
}
return 0;
}
Sample Output
Enter an alphabet: M
M is an Uppercase Alphabet.
Explanation
Uppercase English letters have ASCII values from 65 to 90, while lowercase letters range from 97 to 122.
Concepts Covered
- ASCII Values
- Character Comparison
- Conditional Statements
10. C Program to Assign Grades Based on Marks
Problem Statement
Write a C program to assign grades based on student marks.
Grade Criteria
- 90–100 → Grade A
- 80–89 → Grade B
- 70–79 → Grade C
- 60–69 → Grade D
- Below 60 → Grade F
C Solution
#include <stdio.h>
int main()
{
int marks;
printf("Enter your marks: ");
scanf("%d", &marks);
if(marks >= 90)
{
printf("Grade A");
}
else if(marks >= 80)
{
printf("Grade B");
}
else if(marks >= 70)
{
printf("Grade C");
}
else if(marks >= 60)
{
printf("Grade D");
}
else
{
printf("Grade F");
}
return 0;
}
Sample Output
Enter your marks: 87
Grade B
Explanation
The program uses an if-else-if ladder to check the student’s marks against predefined grade ranges and prints the corresponding grade.
Concepts Covered
- if-else-if Ladder
- Relational Operators
- Decision Making
- Multiple Conditions
11. C Program to Find the Absolute Value of a Number
Problem Statement
Write a C program to find the absolute value of a given number.
C Solution
#include <stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
if(number < 0)
{
number = -number;
}
printf("Absolute Value = %d", number);
return 0;
}
Sample Output
Enter a number: -35
Absolute Value = 35
Explanation
If the number is negative, multiplying it by -1 converts it into a positive number.
Concepts Covered
- if Statement
- Negative Numbers
- Arithmetic Operators
12. C Program to Check Whether a Number is a Three-Digit Number
Problem Statement
Write a C program to check whether the entered number is a three-digit number.
C Solution
#include <stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
if(number >= 100 && number <= 999)
{
printf("It is a Three-Digit Number.");
}
else
{
printf("It is not a Three-Digit Number.");
}
return 0;
}
Sample Output
Enter a number: 456
It is a Three-Digit Number.
Explanation
The program checks whether the number lies between 100 and 999, inclusive.
Concepts Covered
- Logical AND (
&&) - Relational Operators
- Decision Making
13. C Program to Check Whether a Person is Eligible to Vote
Problem Statement
Write a C program to determine whether a person is eligible to vote.
C Solution
#include <stdio.h>
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
if(age >= 18)
{
printf("Eligible to Vote");
}
else
{
printf("Not Eligible to Vote");
}
return 0;
}
Sample Output
Enter your age: 22
Eligible to Vote
Explanation
According to the voting rules, a person must be 18 years or older to vote.
Concepts Covered
- if-else Statement
- Relational Operators
- Eligibility Check
14. C Program to Check Whether a Person is Eligible for Driving License
Problem Statement
Write a C program to determine whether a person is eligible to apply for a driving license.
C Solution
#include <stdio.h>
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
if(age >= 18)
{
printf("Eligible for Driving License");
}
else
{
printf("Not Eligible for Driving License");
}
return 0;
}
Sample Output
Enter your age: 20
Eligible for Driving License
Explanation
The program checks whether the entered age is 18 or above.
Concepts Covered
- Conditional Statements
- Decision Making
- Relational Operators
15. C Program to Find the Smallest of Two Numbers
Problem Statement
Write a C program to find the smaller of two numbers.
C Solution
#include <stdio.h>
int main()
{
int first, second;
printf("Enter first number: ");
scanf("%d", &first);
printf("Enter second number: ");
scanf("%d", &second);
if(first < second)
{
printf("%d is the Smallest Number.", first);
}
else
{
printf("%d is the Smallest Number.", second);
}
return 0;
}
Sample Output
Enter first number: 18
Enter second number: 42
18 is the Smallest Number.
Explanation
The less-than (<) operator compares both numbers and displays the smaller one.
Concepts Covered
- Relational Operators
- if-else Statement
- Comparison
Chapter Summary
In this chapter, you learned how conditional statements help a C program make decisions. You practiced using if, if-else, and if-else-if statements to solve real-world problems such as checking positive or negative numbers, even or odd numbers, leap years, grades, voting eligibility, driving license eligibility, and character classification. These concepts are the foundation of decision-making logic and are widely used in software development.
Key Takeaways
- Conditional statements control the flow of program execution.
ifexecutes a block only when a condition is true.if-elsechooses between two possible outcomes.if-else-ifis used for multiple conditions.- Relational operators compare values.
- Logical operators combine multiple conditions.
- Decision-making is essential in real-world applications.
- ASCII values help classify characters.
- Conditional statements are used in loops, functions, and algorithms.
- Strong knowledge of conditions is necessary before learning loops.
Frequently Asked Questions (FAQs)
1. What are conditional statements in C?
Conditional statements allow a program to make decisions based on whether a condition is true or false.
2. What is the difference between if and if-else?
ifexecutes code only when the condition is true.if-elseexecutes one block if the condition is true and another block if it is false.
3. When should we use an if-else-if ladder?
Use an if-else-if ladder when there are multiple conditions to evaluate, such as assigning grades or checking age ranges.
4. What are relational operators?
Relational operators compare two values.
Examples include:
><>=<===!=
5. What are logical operators?
Logical operators combine or modify conditions.
Examples include:
&&(AND)||(OR)!(NOT)
6. What is the purpose of a leap year program?
It demonstrates how multiple conditions can be combined using logical operators to solve a practical problem.
7. Why are ASCII values used in conditional statements?
ASCII values help determine whether a character is uppercase, lowercase, or an alphabet.
8. Why are conditional statements important in C programming?
Conditional statements enable programs to make intelligent decisions, validate user input, implement business rules, and solve real-world problems such as authentication systems, grading systems, banking software, and eligibility checks.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
