0% found this document useful (0 votes)
20 views7 pages

Switch Case

Cse

Uploaded by

Atharva Singh
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)
20 views7 pages

Switch Case

Cse

Uploaded by

Atharva Singh
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/ 7

Write a C program which computes the area of various geometrical

shapes using a menu-driven approach.

#include <stdio.h> // Include the standard input/output header file.

int main ()

int choice,r,l,w,b,h; // Declare variables to store user input and


dimensions.

float area; // Declare a variable to store the calculated area.

// Display the options to the user.

printf("Input 1 for area of circle\n");

printf("Input 2 for area of rectangle\n");

printf("Input 3 for area of triangle\n");

printf("Input your choice : ");

scanf("%d",&choice); // Read and store the user's choice.

switch(choice) // Start a switch statement based on the user's choice.

case 1:

printf("Input radius of the circle : "); // Prompt user for circle's


radius.

scanf("%d",&r); // Read and store the radius.

area=3.14*r*r; // Calculate the area of the circle.

break;

case 2:

printf("Input length and width of the rectangle : "); // Prompt


user for rectangle's dimensions.

scanf("%d%d",&l,&w); // Read and store length and width.

area=l*w; // Calculate the area of the rectangle.


break;

case 3:

printf("Input the base and height of the triangle :"); // Prompt


user for triangle's base and height.

scanf("%d%d",&b,&h); // Read and store base and height.

area=.5*b*h; // Calculate the area of the triangle.

break;

printf("The area is : %f\n",area); // Display the calculated area.

return 0;

Write a C program to read any day number in integer and display the day
name in word format.
#include <stdio.h> // Include the standard input/output header file.

void main()

int dayno; // Declare a variable to store the day number.

printf("Input Day No : "); // Prompt the user to input the day number.

scanf("%d",&dayno); // Read and store the day number.

switch(dayno) // Start a switch statement based on the day number.

case 1:

printf("Monday \n"); // Print the corresponding day for day


number 1.

break;

case 2:
printf("Tuesday \n"); // Print the corresponding day for day
number 2.

break;

case 3:

printf("Wednesday \n"); // Print the corresponding day for day


number 3.

break;

case 4:

printf("Thursday \n"); // Print the corresponding day for day


number 4.

break;

case 5:

printf("Friday \n"); // Print the corresponding day for day


number 5.

break;

case 6:

printf("Saturday \n"); // Print the corresponding day for day


number 6.

break;

case 7:

printf("Sunday \n"); // Print the corresponding day for day


number 7.

break;

default:

printf("Invalid day number. \nPlease try again ....\n"); // Print a


message for invalid day number.

break;

1. Write a program in C to accept a grade and declare the equivalent


description :
Grade Description

E Excellent

V Very Good

G Good

A Average

F Fail

#include <stdio.h> // Include the standard input/output header file.

#include <ctype.h> // Include the header file for character handling


functions.

#include <string.h> // Include the header file for string handling


functions.

void main()

char notes[15]; // Declare a character array to store notes.

char grd; // Declare a character variable to store the grade.

printf("Input the grade :"); // Prompt user for input of grade.

scanf("%c", &grd); // Read and store the grade.

grd = toupper(grd); // Convert the grade to uppercase using 'toupper'


function.

switch(grd) // Start a switch statement based on the grade.

{
case 'E':

strcpy(notes, " Excellent"); // Copy corresponding note for grade


'E'.

break;

case 'V':

strcpy(notes, " Very Good"); // Copy corresponding note for grade


'V'.

break;

case 'G':

strcpy(notes, " Good "); // Copy corresponding note for grade 'G'.

break;

case 'A':

strcpy(notes, " Average"); // Copy corresponding note for grade


'A'.

break;

case 'F':

strcpy(notes, " Fails"); // Copy corresponding note for grade 'F'.

break;

default :

strcpy(notes, "Invalid Grade Found. \n"); // Copy message for


invalid grade.

break;

printf("You have chosen : %s\n", notes); // Print the chosen note.

1. Write a program in C to calculate and print the electricity bill of a given


customer. The customer ID, name, and unit consumed by the user should
be captured from the keyboard to display the total amount to be paid to
the customer. The charge are as follow :
Unit Charge/unit

upto 199 @1.20

200 and above but less than 400 @1.50

400 and above but less than 600 @1.80

600 and above @2.00

If bill exceeds Rs. 400 then a surcharge of 15% will be charged and the
minimum bill should be of Rs. 100/-
#include <stdio.h> // Include the standard input/output header file.

#include <string.h> // Include the string header file.

void main()

int custid, conu; // Declare variables to store customer ID and


consumed units.

float chg, surchg = 0, gramt, netamt; // Declare variables for charge,


surcharge, gross amount, and net amount.

char connm[25]; // Declare a character array to store customer name.

printf("Input Customer ID :"); // Prompt user for input of customer ID.

scanf("%d", &custid); // Read and store the customer ID.

printf("Input the name of the customer :"); // Prompt user for input of
customer name.

scanf("%s", connm); // Read and store the customer name.

printf("Input the unit consumed by the customer : "); // Prompt user for
input of consumed units.

scanf("%d", &conu); // Read and store the consumed units.


if (conu < 200)

chg = 1.20; // Set charge based on consumed units.

else if (conu >= 200 && conu < 400)

chg = 1.50; // Set charge based on consumed units.

else if (conu >= 400 && conu < 600)

chg = 1.80; // Set charge based on consumed units.

else

chg = 2.00; // Set charge based on consumed units.

gramt = conu * chg; // Calculate gross amount.

if (gramt > 300)

surchg = gramt * 15 / 100.0; // Calculate surcharge if gross amount


is greater than 300.

netamt = gramt + surchg; // Calculate net amount.

if (netamt < 100)

netamt = 100; // Set minimum net amount to 100.

printf("\nElectricity Bill\n");

printf("Customer IDNO :%d\n", custid);

printf("Customer Name :%s\n", connm);

printf("unit Consumed :%d\n", conu);

printf("Amount Charges @Rs. %4.2f per unit :%8.2f\n", chg, gramt);

printf("Surchage Amount :%8.2f\n", surchg);

printf("Net Amount Paid By the Customer :%8.2f\n", netamt);

You might also like