0% found this document useful (0 votes)
21 views33 pages

CPL Assignment Task 3

Uploaded by

sonishivam.2905
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views33 pages

CPL Assignment Task 3

Uploaded by

sonishivam.2905
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

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;

printf("Enter a number (1-7) to display the day of the week: ");

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:

printf("Invalid input! Please enter a number between 1 and 7.\n");

return 0;

OUTPUT:- Enter a number (1-7) to display the day of the week: 3

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;

printf("Enter a character: ");

scanf("%c", &ch);

ch = (ch >= 'A' && ch <= 'Z') ? ch + 32 : ch;

switch(ch) {

case 'a':

case 'e':

case 'i':

case 'o':

case 'u':
printf("%c is a vowel.\n", ch);

break;

default:

if((ch >= 'a' && ch <= 'z')) {

printf("%c is a consonant.\n", ch);

} else {

printf("Invalid input! Please enter an alphabet character.\n");

return 0;

OUTPUT:- Enter a character: e

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;

double num1, num2, result;

printf("Enter an operator (+, -, *, /): ");

scanf("%c", &operator);

printf("Enter two numbers: ");

scanf("%lf %lf", &num1, &num2);

switch (operator) {
case '+':

result = num1 + num2;

printf("%.2lf + %.2lf = %.2lf\n", num1, num2, result);

break;

case '-':

result = num1 - num2;

printf("%.2lf - %.2lf = %.2lf\n", num1, num2, result);

break;

case '*':

result = num1 * num2;

printf("%.2lf * %.2lf = %.2lf\n", num1, num2, result);

break;

case '/':

if (num2 != 0) {

result = num1 / num2;

printf("%.2lf / %.2lf = %.2lf\n", num1, num2, result);

} else {

printf("Division by zero is not allowed.\n");

break;

default:

printf("Invalid operator!\n");

return 0;

OUTPUT:- Enter an operator (+, -, *, /): +

Enter two numbers: 5 3

5.00 + 3.00 = 8.00


4. Write a program to display the number of days in a given month using switch case.

#include <stdio.h>

int main() {

int month;

printf("Enter the month number (1-12): ");

scanf("%d", &month);

switch (month) {

case 1: case 3: case 5: case 7: case 8: case 10: case 12:

printf("31 days\n");

break;

case 4: case 6: case 9: case 11:

printf("30 days\n");

break;

case 2:

printf("28 or 29 days (depending on leap year)\n");

break;

default:

printf("Invalid month number!\n");

return 0;

OUTPUT:- Enter the month number (1-12): 4

30 days

5. Implement a switch case to convert a character to uppercase or lowercase based on user


input.
#include <stdio.h>

int main() {

char ch, choice;

printf("Enter a character: ");

scanf("%c", &ch);

printf("Convert to (U)ppercase or (L)owercase: ");

getchar();

scanf("%c", &choice);

switch (choice) {

case 'U':

if (ch >= 'a' && ch <= 'z')

ch -= 32;

printf("Uppercase: %c\n", ch);

break;

case 'L':

if (ch >= 'A' && ch <= 'Z')

ch += 32;

printf("Lowercase: %c\n", ch);

break;

default:

printf("Invalid choice!\n");

return 0;

OUTPUT:- Enter a character: a


Convert to (U)ppercase or (L)owercase: U

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;

printf("Enter a year: ");

scanf("%d", &year);

switch ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {

case 1:

printf("%d is a leap year.\n", year);

break;

case 0:

printf("%d is not a leap year.\n", year);

break;

return 0;

OUTPUT:- Enter a year: 2020

2020 is a leap year.

7. Use a switch case to determine if a given number is odd or even.

#include <stdio.h>

int main() {

int num;

printf("Enter a number: ");


scanf("%d", &num);

switch (num % 2) {

case 0:

printf("%d is even.\n", num);

break;

case 1:

printf("%d is odd.\n", num);

break;

return 0;

OUTPUT:- Enter a number: 4

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;

printf("Enter marks: ");

scanf("%d", &marks);

switch (marks / 10) {

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;

OUTPUT:- Enter marks: 85

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;

printf("Enter temperature: ");

scanf("%lf", &temp);

printf("Convert to (F)ahrenheit or (C)elsius: ");

getchar();
scanf("%c", &choice);

switch (choice) {

case 'F':

printf("Celsius to Fahrenheit: %.2lf\n", (temp * 9/5) + 32);

break;

case 'C':

printf("Fahrenheit to Celsius: %.2lf\n", (temp - 32) * 5/9);

break;

default:

printf("Invalid choice!\n");

return 0;

OUTPUT:- Enter temperature: 212

Convert to (F)ahrenheit or (C)elsius: C

Fahrenheit to Celsius: 100.00

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;

printf("Enter value: ");

scanf("%lf", &value);
printf("Choose conversion:\n1. km to miles\n2. inches to cm\n");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("km to miles: %.2lf\n", value * 0.621371);

break;

case 2:

printf("inches to cm: %.2lf\n", value * 2.54);

break;

default:

printf("Invalid choice!\n");

return 0;

OUTPUT:- Enter value: 10

Choose conversion:

1. km to miles

2. inches to cm

inches to cm: 25.40

11. Write a switch case to display a menu for food items with corresponding prices.

#include <stdio.h>

int main() {

int choice;

printf("Menu:\n1. Pizza - Rs 250\n2. Burger - Rs 150\n3. Pasta - Rs 200\n");


printf("Enter your 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

Enter your choice: 1

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;

printf("Enter a digit (0-9): ");

scanf("%d", &digit);

switch (digit) {

case 0: printf("Zero\n"); break;

case 1: printf("One\n"); break;

case 2: printf("Two\n"); break;

case 3: printf("Three\n"); break;

case 4: printf("Four\n"); break;

case 5: printf("Five\n"); break;

case 6: printf("Six\n"); break;

case 7: printf("Seven\n"); break;

case 8: printf("Eight\n"); break;

case 9: printf("Nine\n"); break;

default: printf("Invalid input!\n");

return 0;

OUTPUT:- Enter a digit (0-9): 3

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;

double radius, side, length, breadth;

printf("Choose shape:\n1. Circle\n2. Square\n3. Rectangle\n");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter radius: ");

scanf("%lf", &radius);

printf("Area of Circle: %.2lf\n", 3.1416 * radius * radius);

break;

case 2:

printf("Enter side length: ");

scanf("%lf", &side);

printf("Area of Square: %.2lf\n", side * side);

break;

case 3:

printf("Enter length and breadth: ");

scanf("%lf %lf", &length, &breadth);

printf("Area of Rectangle: %.2lf\n", length * breadth);

break;

default:

printf("Invalid choice!\n");

return 0;

OUTPUT:- Choose shape:

1. Circle
2. Square

3. Rectangle

Enter length and breadth: 3 4

Area of Rectangle: 12.00

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;

printf("Enter a number: ");

scanf("%d", &num);

switch ((num > 0) - (num < 0)) {

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;

printf("Enter planet number (1-8): ");

scanf("%d", &planet);

switch (planet) {

case 1: printf("Mercury\n"); break;

case 2: printf("Venus\n"); break;

case 3: printf("Earth\n"); break;

case 4: printf("Mars\n"); break;

case 5: printf("Jupiter\n"); break;

case 6: printf("Saturn\n"); break;

case 7: printf("Uranus\n"); break;

case 8: printf("Neptune\n"); break;

default: printf("Invalid input!\n");

return 0;

OUTPUT:- Enter planet number (1-8): 1

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;

printf("Enter lengths of three sides: ");

scanf("%d %d %d", &a, &b, &c);

switch ((a == b) + (b == c) + (c == a)) {

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;

OUTPUT:- Enter lengths of three sides: 3 3 3

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;

printf("Enter an operator (+, -, *, /): ");

scanf(" %c", &op);

switch (op) {

case '+':

case '-':

case '*':

case '/':

printf("Valid operator\n");

break;

default:

printf("Invalid operator\n");

return 0;

OUTPUT:- Enter an operator (+, -, *, /): +

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: ");

scanf(" %c", &ch);

switch (1) {

case 1:

if (ch >= '0' && ch <= '9')

printf("%c is a digit\n", ch);

else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))

printf("%c is an alphabet\n", ch);

else

printf("%c is a special symbol\n", ch);

break;

return 0;

OUTPUT:- Enter a character: 5

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;

printf("Enter total units consumed: ");

scanf("%d", &units);
switch (units / 100) {

case 0:

bill = units * 1.5;

break;

case 1:

bill = 100 * 1.5 + (units - 100) * 2.0;

break;

case 2:

bill = 100 * 1.5 + 100 * 2.0 + (units - 200) * 3.0;

break;

default:

bill = 100 * 1.5 + 100 * 2.0 + 100 * 3.0 + (units - 300) * 4.0;

printf("Total bill: %.2f\n", bill);

return 0;

OUTPUT:- Enter total units consumed: 250

Total bill: 400.00

20. Implement a switch case to simulate a simple banking application (deposit, withdraw,
balance inquiry).

#include <stdio.h>

int main() {

int choice;

float balance = 1000.0, amount;

printf("1. Deposit\n2. Withdraw\n3. Balance Inquiry\n");

printf("Enter your choice: ");


scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter amount to deposit: ");

scanf("%f", &amount);

balance += amount;

printf("New Balance: %.2f\n", balance);

break;

case 2:

printf("Enter amount to withdraw: ");

scanf("%f", &amount);

if (amount <= balance) {

balance -= amount;

printf("New Balance: %.2f\n", balance);

} else {

printf("Insufficient balance!\n");

break;

case 3:

printf("Balance: %.2f\n", balance);

break;

default:

printf("Invalid choice!\n");

return 0;

OUTPUT:- 1. Deposit

2. Withdraw
3. Balance Inquiry

Enter your choice: 3

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;

float balance = 1000.0, amount;

printf("1. Balance Check\n2. Deposit\n3. Withdraw\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Balance: %.2f\n", balance);

break;

case 2:

printf("Enter amount to deposit: ");

scanf("%f", &amount);

balance += amount;

printf("New Balance: %.2f\n", balance);

break;

case 3:

printf("Enter amount to withdraw: ");

scanf("%f", &amount);

if (amount <= balance) {

balance -= amount;
printf("New Balance: %.2f\n", balance);

} else {

printf("Insufficient balance!\n");

break;

default:

printf("Invalid choice!\n");

return 0;

OUTPUT:- 1. Balance Check

2. Deposit

3. Withdraw

Enter your choice: 1

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;

printf("Enter 1 for Red, 2 for Yellow, 3 for Green: ");

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;

OUTPUT:- Enter 1 for Red, 2 for Yellow, 3 for Green: 1

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;

printf("Enter month number (1-12): ");

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;

OUTPUT:- Enter month number (1-12): 5

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;

printf("Enter marks: ");

scanf("%d", &marks);

switch (marks / 10) {

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;

OUTPUT:- Enter marks: 88

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() {

int choice, num;

printf("Choose operation:\n1. Square\n2. Cube\n3. Square Root\n");

scanf("%d", &choice);

printf("Enter a number: ");

scanf("%d", &num);

switch (choice) {

case 1:

printf("Square: %d\n", num * num);

break;

case 2:

printf("Cube: %d\n", num * num * num);

break;

case 3:

printf("Square Root: %.2f\n", sqrt(num));

break;

default:

printf("Invalid choice!\n");

return 0;

OUTPUT:- Choose operation:

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;

printf("Select class:\n1. 1st AC\n2. 2nd AC\n3. Sleeper\n");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("You selected 1st AC.\n");

break;

case 2:

printf("You selected 2nd AC.\n");

break;

case 3:

printf("You selected Sleeper.\n");

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;

printf("Choose operation:\n1. Area\n2. Perimeter\n");

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() {

int num, choice;

printf("Enter an integer: ");

scanf("%d", &num);

printf("Choose conversion:\n1. Binary\n2. Octal\n3. Hexadecimal\n");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Binary: %b\n", num);

break;

case 2:

printf("Octal: %o\n", num);

break;

case 3:

printf("Hexadecimal: %X\n", num);

break;

default:

printf("Invalid choice!\n");
}

return 0;

OUTPUT:- Enter an integer: 10

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;

printf("Select item:\n1. Apples\n2. Bananas\n3. Oranges\n");

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;

OUTPUT:- Select item:

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() {

int choice, price = 100, discount;

printf("Select discount:\n1. 10%%\n2. 20%%\n3. 30%%\n");

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;

printf("Final cost: Rs %d\n", price - (price * discount / 100));

return 0;

OUTPUT:- Select discount:

1. 10%

2. 20%

3. 30%

Invalid choice!

You might also like