if-else
Example 1: Basic If-Else
#include <stdio.h>
int main() {
int num = 10;
Example 4: Multiple Conditions
if (num > 0) { #include <stdio.h>
printf("The number is positive.\n"); int main() {
} else { int a = 5, b = 10, c = 15;
printf("The number is not positive.\n"); if (a > b && a > c) {
} printf("a is the largest number.\n");
return 0; } else if (b > a && b > c) {
} printf("b is the largest number.\n");
} else {
printf("c is the largest number.\n");
Output: The number is positive. }
return 0;
Example 2: If-Else Ladder }
#include <stdio.h>
int main() { Output: c is the largest number.
int num = -5;
if (num > 0) {
printf("The number is positive.\n"); Example 5: Using Logical
} else if (num == 0) { Operators
printf("The number is zero.\n"); #include <stdio.h>
} else { int main() {
printf("The number is negative.\n"); int age = 20;
} char gender = 'M';
if (age >= 18 && gender == 'M') {
return 0; printf("Adult Male.\n");
} } else if (age >= 18 && gender == 'F') {
printf("Adult Female.\n");
Output: The number is negative. } else if (age < 18 && gender == 'M') {
printf("Minor Male.\n");
} else {
Example 3: Nested If-Else printf("Minor Female.\n");
#include <stdio.h> }
int main() { return 0;
int num = 8; }
if (num % 2 == 0) {
if (num > 0) {
Output: Adult Male.
printf("The number is positive and even.\
n"); Example 6: Checking Multiple
} else { Conditions
printf("The number is zero.\n");
#include <stdio.h>
}
int main() {
} else {
int marks = 85;
if (num > 0) {
if (marks >= 90) {
printf("The number is positive and odd.\n");
printf("Grade: A\n");
} else {
} else if (marks >= 80) {
printf("The number is negative and odd.\
printf("Grade: B\n");
n");
} else if (marks >= 70) {
}
printf("Grade: C\n");
}
} else if (marks >= 60) {
return 0;
printf("Grade: D\n");
}
} else {
printf("Grade: F\n");
Output: The number is positive and even. }
return 0;
}
1|Page
Output: Grade: B
Assignment Questions
1. Positive, Negative, or Zero
o Problem: Write a C program that takes an integer as input and determines whether
it is positive, negative, or zero.
o Hint: Use if-else statements to check if the number is greater than, less than, or
equal to zero.
2. Even or Odd
o Problem: Write a C program that takes an integer as input and determines whether
it is even or odd.
o Hint: Use the modulus operator (%) to check if the number is divisible by 2.
3. Leap Year Checker
o Problem: Write a C program to check whether a given year is a leap year. A year is a
leap year if it is divisible by 4 but not by 100, or it is divisible by 400.
o Hint: Use nested if-else statements to apply the leap year conditions.
4. Grading System
o Problem: Write a C program that takes marks as input and prints the corresponding
grade according to the following criteria:
Marks >= 90: Grade A
Marks >= 80: Grade B
Marks >= 70: Grade C
Marks >= 60: Grade D
Marks < 60: Grade F
o Hint: Use an if-else ladder to check each range of marks.
5. Maximum of Three Numbers
o Problem: Write a C program that takes three integers as input and prints the largest
of the three.
o Hint: Use nested if-else statements to compare the three numbers.
6. Calculator
o Problem: Write a C program that takes two integers and a character representing an
arithmetic operator (+, -, *, /) as input, and performs the corresponding arithmetic
operation.
o Hint: Use if-else statements to check the operator and perform the appropriate
operation.
7. Vowel or Consonant
o Problem: Write a C program that takes a single character as input and determines
whether it is a vowel or a consonant.
o Hint: Use if-else statements to check if the character is one of a, e, i, o, u,
A, E, I, O, U.
8. Age Group
o Problem: Write a C program that takes an integer representing age as input and
categorizes it into one of the following age groups:
0-12: Child
13-19: Teenager
20-64: Adult
65 and above: Senior
o Hint: Use an if-else ladder to check each age range.
9. Triangle Type
o Problem: Write a C program that takes the lengths of three sides of a triangle as
input and determines whether it is equilateral, isosceles, or scalene.
o Hint: Use if-else statements to compare the lengths of the sides.
10. Temperature Converter
2|Page
o Problem: Write a C program that takes a temperature value and a character
representing the scale (C for Celsius, F for Fahrenheit) as input, and converts the
temperature to the other scale.
o Hint: Use if-else statements to check the scale and perform the appropriate
conversion.
Example of a Detailed Assignment
Positive, Negative, or Zero
Problem Statement: Write a C program that prompts the user to enter an integer and then
determines whether the number is positive, negative, or zero.
Requirements:
1. Prompt the user to enter an integer.
2. Use if-else statements to check if the number is greater than zero, less than zero, or equal to
zero.
3. Print a message indicating whether the number is positive, negative, or zero.
Sample Code:
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num > 0) {
printf("The number is positive.\n");
} else if (num < 0) {
printf("The number is negative.\n");
} else {
printf("The number is zero.\n");
}
return 0;
}
Expected Output:
If the user enters 5:
Enter an integer: 5
The number is positive.
If the user enters -3:
Enter an integer: -3
The number is negative.
If the user enters 0:
Enter an integer: 0
The number is zero.
This format can be used for each assignment question, with specific instructions, hints, and
example code as needed.
3|Page