Practical / Solution
if — Exercise 1
Problem
Write a C program to check whether a number is positive.
Program
#include <stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
// check whether the number is positive
if (num > 0)
printf("The number is positive.\n");
return 0;
}
Explanation
The if statement executes its statement only when
the condition num > 0 is true.
Expected Output
Enter a number: 15
The number is positive.
if — Exercise 2
Problem
Write a C program to check whether a number is negative.
Program
#include <stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
// check whether the number is negative
if (num < 0)
printf("The number is negative.\n");
return 0;
}
Explanation
The condition num < 0 becomes true when the
entered number is less than zero.
Expected Output
Enter a number: -8
The number is negative.
if — Exercise 3
Problem
Write a C program to check whether a number is even.
Program
#include <stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
// check whether the number is even
if (num % 2 == 0)
printf("The number is even.\n");
return 0;
}
Explanation
The modulus operator % returns the remainder.
If num % 2 == 0, the number is even.
Expected Output
Enter a number: 24
The number is even.
if — Exercise 4
Problem
Write a C program to check whether a number is divisible by 5.
Program
#include <stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
// check divisibility by 5
if (num % 5 == 0)
printf("The number is divisible by 5.\n");
return 0;
}
Explanation
If the remainder obtained by dividing the number by 5 is zero,
the number is divisible by 5.
Expected Output
Enter a number: 25
The number is divisible by 5.
if — Exercise 5
Problem
Write a C program to check whether a person is eligible to vote.
A person is eligible if the age is 18 or more.
Program
#include <stdio.h>
int main()
{
int age;
printf("Enter age: ");
scanf("%d", &age);
// check voting eligibility
if (age >= 18)
printf("Eligible to vote.\n");
return 0;
}
Explanation
The condition age >= 18 checks whether the person
has reached the required age.
Expected Output
Enter age: 21
Eligible to vote.
if — Exercise 6
Problem
Write a C program to check whether a student has scored 50 or more marks.
Program
#include <stdio.h>
int main()
{
int marks;
printf("Enter marks: ");
scanf("%d", &marks);
// check whether marks are 50 or more
if (marks >= 50)
printf("The student has passed.\n");
return 0;
}
Explanation
The if condition checks whether the marks are
greater than or equal to 50.
Expected Output
Enter marks: 72
The student has passed.
if — Exercise 7
Problem
Write a C program to check whether a number is greater than 100.
Program
#include <stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
// check whether the number is greater than 100
if (num > 100)
printf("The number is greater than 100.\n");
return 0;
}
Explanation
The condition num > 100 is true only when
the entered value is greater than 100.
Expected Output
Enter a number: 150
The number is greater than 100.
if — Exercise 8
Problem
Write a C program to check whether a character is an uppercase letter.
Program
#include <stdio.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf(" %c", &ch);
// check whether the character is uppercase
if (ch >= 'A' && ch <= 'Z')
printf("The character is uppercase.\n");
return 0;
}
Explanation
The condition checks whether the character lies between
'A' and 'Z'.
Expected Output
Enter a character: G
The character is uppercase.
if — Exercise 9
Problem
Write a C program to check whether a year is a leap year.
Consider a year divisible by 400, or divisible by 4 but not by 100.
Program
#include <stdio.h>
int main()
{
int year;
printf("Enter year: ");
scanf("%d", &year);
// check leap year condition
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
printf("The year is a leap year.\n");
return 0;
}
Explanation
The condition checks the standard leap-year rules using
arithmetic and logical operators.
Expected Output
Enter year: 2024
The year is a leap year.
if — Exercise 10
Problem
Write a C program to check whether a number is a three-digit number.
Program
#include <stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
// check whether the number has three digits
if ((num >= 100 && num <= 999) ||
(num <= -100 && num >= -999))
printf("The number is a three-digit number.\n");
return 0;
}
Explanation
The condition checks both positive and negative values having
exactly three digits.
Expected Output
Enter a number: 456
The number is a three-digit number.