CPL Assignment Task 3
CPL Assignment Task 3
1. Write a C program to input a number and display the day of the week using a switch case
#include <stdio.h>
int main() {
int day;
scanf("%d", &day);
switch (day) {
case 1:
printf("Sunday\n");
break;
case 2:
printf("Monday\n");
break;
case 3:
printf("Tuesday\n");
break;
case 4:
printf("Wednesday\n");
break;
case 5:
printf("Thursday\n");
break;
case 6:
printf("Friday\n");
break;
case 7:
printf("Saturday\n");
break;
default:
return 0;
Tuesday
2. Implement a program that checks if a character is a vowel or consonant using a switch case.
#include <stdio.h>
int main() {
char ch;
scanf("%c", &ch);
switch(ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("%c is a vowel.\n", ch);
break;
default:
} else {
return 0;
e is a vowel.
3. Create a calculator using a switch case to perform addition, subtraction, multiplication, and
division.
#include <stdio.h>
int main() {
char operator;
scanf("%c", &operator);
switch (operator) {
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
if (num2 != 0) {
} else {
break;
default:
printf("Invalid operator!\n");
return 0;
#include <stdio.h>
int main() {
int month;
scanf("%d", &month);
switch (month) {
printf("31 days\n");
break;
printf("30 days\n");
break;
case 2:
break;
default:
return 0;
30 days
int main() {
scanf("%c", &ch);
getchar();
scanf("%c", &choice);
switch (choice) {
case 'U':
ch -= 32;
break;
case 'L':
ch += 32;
break;
default:
printf("Invalid choice!\n");
return 0;
Uppercase: A
6. Write a C program to check whether a year is a leap year using a switch case.
#include <stdio.h>
int main() {
int year;
scanf("%d", &year);
case 1:
break;
case 0:
break;
return 0;
#include <stdio.h>
int main() {
int num;
switch (num % 2) {
case 0:
break;
case 1:
break;
return 0;
4 is even.
8. Implement a switch case to find the grade based on a student's marks (A, B, C, D, F)
#include <stdio.h>
int main() {
int marks;
scanf("%d", &marks);
case 10:
case 9:
printf("Grade A\n");
break;
case 8:
printf("Grade B\n");
break;
case 7:
printf("Grade C\n");
break;
case 6:
printf("Grade D\n");
break;
default:
printf("Grade F\n");
return 0;
Grade A
9. Write a program to convert temperature from Celsius to Fahrenheit or vice versa using a
switch case.
#include <stdio.h>
int main() {
double temp;
char choice;
scanf("%lf", &temp);
getchar();
scanf("%c", &choice);
switch (choice) {
case 'F':
break;
case 'C':
break;
default:
printf("Invalid choice!\n");
return 0;
10. Create a program to perform unit conversion (km to miles, inches to cm, etc.) using switch
case.
#include <stdio.h>
int main() {
double value;
int choice;
scanf("%lf", &value);
printf("Choose conversion:\n1. km to miles\n2. inches to cm\n");
scanf("%d", &choice);
switch (choice) {
case 1:
break;
case 2:
break;
default:
printf("Invalid choice!\n");
return 0;
Choose conversion:
1. km to miles
2. inches to cm
11. Write a switch case to display a menu for food items with corresponding prices.
#include <stdio.h>
int main() {
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Pizza: Rs 250\n");
break;
case 2:
printf("Burger: Rs 150\n");
break;
case 3:
printf("Pasta: Rs 200\n");
break;
default:
printf("Invalid choice!\n");
return 0;
OUTPUT:- Menu:
1. Pizza - Rs 250
2. Burger - Rs 150
3. Pasta - Rs 200
Pizza: Rs 250
12. Implement a program to input a digit and print its word form (0 -> "Zero", 1 -> "One", etc.)
using switch case
#include <stdio.h>
int main() {
int digit;
scanf("%d", &digit);
switch (digit) {
return 0;
Three
13. Write a program to find the area of different shapes (circle, square, rectangle) based on
user input using switch case.
#include <stdio.h>
int main() {
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%lf", &radius);
break;
case 2:
scanf("%lf", &side);
break;
case 3:
break;
default:
printf("Invalid choice!\n");
return 0;
1. Circle
2. Square
3. Rectangle
14. Use a switch case to input a number and determine whether it is a positive, negative, or
zero
#include <stdio.h>
int main() {
int num;
scanf("%d", &num);
case 1:
printf("Positive\n");
break;
case -1:
printf("Negative\n");
break;
case 0:
printf("Zero\n");
break;
return 0;
}
OUTPUT:- Enter a number: 10
Positive
15. Create a program that outputs the corresponding planet name for a number input (1 ->
"Mercury", 2 -> "Venus", etc.) using switch case.
#include <stdio.h>
int main() {
int planet;
scanf("%d", &planet);
switch (planet) {
return 0;
Mercury
16. Implement a program that prints the type of a triangle (equilateral, isosceles, scalene) based
on user input using switch case
#include <stdio.h>
int main() {
int a, b, c;
case 3:
printf("Equilateral triangle\n");
break;
case 2:
printf("Isosceles triangle\n");
break;
case 1:
printf("Scalene triangle\n");
break;
default:
printf("Invalid triangle\n");
return 0;
Equilateral triangle
17. Write a program to check the validity of an operator (+, -, *, /) entered by the user using
switch case.
#include <stdio.h>
int main() {
char op;
switch (op) {
case '+':
case '-':
case '*':
case '/':
printf("Valid operator\n");
break;
default:
printf("Invalid operator\n");
return 0;
Valid operator
18. Use a switch case to determine if a given character is a digit, alphabet, or special symbol.
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
switch (1) {
case 1:
else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
else
break;
return 0;
5 is a digit
19. Write a program to calculate the total electricity bill based on unit consumption using switch
case for different rates
#include <stdio.h>
int main() {
int units;
float bill;
scanf("%d", &units);
switch (units / 100) {
case 0:
break;
case 1:
break;
case 2:
break;
default:
bill = 100 * 1.5 + 100 * 2.0 + 100 * 3.0 + (units - 300) * 4.0;
return 0;
20. Implement a switch case to simulate a simple banking application (deposit, withdraw,
balance inquiry).
#include <stdio.h>
int main() {
int choice;
switch (choice) {
case 1:
scanf("%f", &amount);
balance += amount;
break;
case 2:
scanf("%f", &amount);
balance -= amount;
} else {
printf("Insufficient balance!\n");
break;
case 3:
break;
default:
printf("Invalid choice!\n");
return 0;
OUTPUT:- 1. Deposit
2. Withdraw
3. Balance Inquiry
Balance: 1000.00
21. Create a program to simulate an ATM machine where the user can select from balance
check, deposit, or withdrawal using switch case.
#include <stdio.h>
int main() {
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
break;
case 2:
scanf("%f", &amount);
balance += amount;
break;
case 3:
scanf("%f", &amount);
balance -= amount;
printf("New Balance: %.2f\n", balance);
} else {
printf("Insufficient balance!\n");
break;
default:
printf("Invalid choice!\n");
return 0;
2. Deposit
3. Withdraw
Balance: 1000.00
22. Write a C program to assign colors to traffic lights (Red, Yellow, Green) based on user input
using switch case.
#include <stdio.h>
int main() {
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Red\n");
break;
case 2:
printf("Yellow\n");
break;
case 3:
printf("Green\n");
break;
default:
printf("Invalid choice!\n");
return 0;
Red
23. Use a switch case to display the season name based on a month number (1-12).
#include <stdio.h>
int main() {
int month;
scanf("%d", &month);
switch (month) {
case 1:
case 2:
case 12:
printf("Winter\n");
break;
case 3:
case 4:
case 5:
printf("Spring\n");
break;
case 6:
case 7:
case 8:
printf("Summer\n");
break;
case 9:
case 10:
case 11:
printf("Autumn\n");
break;
default:
printf("Invalid month!\n");
return 0;
Spring
24. Write a program to simulate a grading system where the user inputs marks and the grade is
outputted using switch case.
#include <stdio.h>
int main() {
int marks;
scanf("%d", &marks);
case 10:
case 9:
printf("Grade A\n");
break;
case 8:
printf("Grade B\n");
break;
case 7:
printf("Grade C\n");
break;
case 6:
printf("Grade D\n");
break;
default:
printf("Grade F\n");
return 0;
Grade B
25. Implement a switch case to find the square, cube, or square root of a given number based on
user choice.
#include <stdio.h>
#include <math.h>
int main() {
scanf("%d", &choice);
scanf("%d", &num);
switch (choice) {
case 1:
break;
case 2:
break;
case 3:
break;
default:
printf("Invalid choice!\n");
return 0;
1. Square
2. Cube
3. Square Root
Enter a number: 4
Square: 16
26. Create a program to simulate a railway ticket booking system with different classes (1st AC,
2nd AC, Sleeper) using switch case.
#include <stdio.h>
int main() {
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
break;
case 2:
break;
case 3:
break;
default:
printf("Invalid choice!\n");
return 0;
}
OUTRPUT:- Select class:
1. 1st AC
2. 2nd AC
3. Sleeper
Invalid choice!
27. Write a switch case program to select a geometric operation (perimeter or area) based on
user input
#include <stdio.h>
int main() {
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Calculating area...\n");
break;
case 2:
printf("Calculating perimeter...\n");
break;
default:
printf("Invalid choice!\n");
return 0;
}
OUTPUT:- Choose operation:
1. Area
2. Perimeter
Calculating area...
28. Implement a program to convert a given integer to binary, octal, or hexadecimal using switch
case.
#include <stdio.h>
int main() {
scanf("%d", &num);
scanf("%d", &choice);
switch (choice) {
case 1:
break;
case 2:
break;
case 3:
break;
default:
printf("Invalid choice!\n");
}
return 0;
Choose conversion:
1. Binary
2. Octal
3. Hexadecimal
Octal: 12
29. Write a C program to simulate a grocery billing system, where the user selects items using a
switch case.
#include <stdio.h>
int main() {
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Price: Rs 100\n");
break;
case 2:
printf("Price: Rs 50\n");
break;
case 3:
printf("Price: Rs 80\n");
break;
default:
printf("Invalid choice!\n");
return 0;
1. Apples
2. Bananas
3. Oranges
Price: Rs 50
30. Use a switch case to calculate the final cost of products after applying different discount
percentages based on user choice
#include <stdio.h>
int main() {
scanf("%d", &choice);
switch (choice) {
case 1:
discount = 10;
break;
case 2:
discount = 20;
break;
case 3:
discount = 30;
break;
default:
printf("Invalid choice!\n");
return 0;
return 0;
1. 10%
2. 20%
3. 30%
Invalid choice!