1. If Statement
Problem Statement:
Write a program that checks whether a given number is divisible by 5.
- Input: An integer.
- Output: Print “Divisible by 5” if the number is divisible by 5.
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num % 5 == 0) {
printf("Divisible by 5\n");
}
return 0;
}
Sample Input:
25
Sample Output:
Divisible by 5
2. If-Else Statement
Problem Statement:
Write a program to check if a number is positive or negative.
- Input: An integer.
- Output: Print “Positive” if the number is positive, otherwise print “Negative”.
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num >= 0) {
printf("Positive\n");
} else {
printf("Negative\n");
}
return 0;
}
Sample Input:
-3
Sample Output:
Negative
3. Else-If Ladder
Problem Statement:
Write a program that reads the percentage of a student and prints the grade based on the following criteria:
- Grade A: 90% and above
- Grade B: 80% – 89%
- Grade C: 70% – 79%
- Grade D: 60% – 69%
- Grade F: Below 60%
- Input: Percentage (a float value).
- Output: Print the grade.
#include <stdio.h>
int main() {
float percentage;
printf("Enter the percentage: ");
scanf("%f", &percentage);
if (percentage >= 90) {
printf("Grade A\n");
} else if (percentage >= 80) {
printf("Grade B\n");
} else if (percentage >= 70) {
printf("Grade C\n");
} else if (percentage >= 60) {
printf("Grade D\n");
} else {
printf("Grade F\n");
}
return 0;
}
Sample Input:
85.5
Sample Output:
Grade B
4. If-Else with Multiple Conditions
Problem Statement:
Write a program to determine if a given year is a leap year. A year is a leap year if it is divisible by 4, but if it is divisible by 100, it must also be divisible by 400.
- Input: A year (an integer).
- Output: Print “Leap year” if the year is a leap year, otherwise print “Not a leap year”.
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
printf("Leap year\n");
} else {
printf("Not a leap year\n");
}
} else {
printf("Leap year\n");
}
} else {
printf("Not a leap year\n");
}
return 0;
}
Sample Input:
2000
Sample Output:
Leap year
5. Switch Statement
Problem Statement:
Write a program that takes a day number (1 for Monday, 2 for Tuesday, …, 7 for Sunday) and prints the name of the day using a switch statement.
- Input: An integer (1-7).
- Output: Print the day of the week.
#include <stdio.h>
int main() {
int day;
printf("Enter day number (1-7): ");
scanf("%d", &day);
switch (day) {
case 1: printf("Monday\n"); break;
case 2: printf("Tuesday\n"); break;
case 3: printf("Wednesday\n"); break;
case 4: printf("Thursday\n"); break;
case 5: printf("Friday\n"); break;
case 6: printf("Saturday\n"); break;
case 7: printf("Sunday\n"); break;
default: printf("Invalid day\n");
}
return 0;
}
Sample Input:
3
Sample Output:
Wednesday
6. Switch Statement with Arithmetic Operations
Problem Statement:
Write a program that asks the user to enter two numbers and a character representing an operation (‘+’, ‘-‘, ‘*’, ‘/’). Use a switch statement to perform the corresponding arithmetic operation and display the result.
- Input: Two integers and a character (+, -, *, /).
- Output: Print the result of the arithmetic operation.
#include <stdio.h>
int main() {
int num1, num2;
char operation;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
printf("Enter operation (+, -, *, /): ");
scanf(" %c", &operation);
switch (operation) {
case '+':
printf("Result: %d\n", num1 + num2);
break;
case '-':
printf("Result: %d\n", num1 - num2);
break;
case '*':
printf("Result: %d\n", num1 * num2);
break;
case '/':
if (num2 != 0) {
printf("Result: %d\n", num1 / num2);
} else {
printf("Error: Division by zero\n");
}
break;
default:
printf("Invalid operation\n");
}
return 0;
}
Sample Input:
Enter first number: 10 Enter second number: 5 Enter operation (+, -, *, /): *
Sample Output:
Result: 50
7. If-Else with Nested Conditions
Problem Statement:
Write a program to check if a student is eligible for a scholarship. The student is eligible if:
- The student has secured more than 85% in the exam and the family income is less than 5,00,000.
- Otherwise, the student is not eligible.
- Input: Percentage and family income.
- Output: Print “Eligible for scholarship” or “Not eligible for scholarship”.
#include <stdio.h>
int main() {
float percentage;
long income;
printf("Enter the percentage: ");
scanf("%f", &percentage);
printf("Enter family income: ");
scanf("%ld", &income);
if (percentage > 85) {
if (income < 500000) {
printf("Eligible for scholarship\n");
} else {
printf("Not eligible for scholarship\n");
}
} else {
printf("Not eligible for scholarship\n");
}
return 0;
}
Sample Input:
Percentage: 90 Income: 450000
Sample Output:
Eligible for scholarship
8. Conditional (Ternary) Operator
Problem Statement: Write a C program that takes two integers as input and uses the conditional (ternary) operator to find and print the larger of the two numbers.
Input: Two integers.
Output: Print the larger number using a single line of code with the conditional (ternary) operator.
#include <stdio.h>
int main() {
int num1, num2, larger;
// Input: Two integers
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
// Using conditional (ternary) operator to find the larger number
larger = (num1 > num2) ? num1 : num2;
// Output: The larger number
printf("The larger number is: %d\n", larger);
return 0;
}
Sample Input:
Enter first number: 8 Enter second number: 12
Sample Output:
The larger number is: 12
Hint:
You should use the syntax:
condition ? true_expression : false_expression;