0% found this document useful (0 votes)
57 views6 pages

Foc Session 4 Pgms

1. The document contains 4 code snippets that use switch statements to perform different tasks: calculate arithmetic operations, find area of shapes, print branch name based on section, and print Roman numerals for numbers 1-5. 2. The first code snippet uses switch to perform arithmetic operations like addition, subtraction, multiplication, and division based on user input. It checks for divide by zero error. 3. The second code snippet uses switch to calculate and print the area of triangle, square, circle, and rectangle based on user choice. 4. The third code snippet uses switch to print the branch name based on the section chosen by the user. 5. The fourth code snippet uses switch to print the Roman

Uploaded by

Kunal Jain
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)
57 views6 pages

Foc Session 4 Pgms

1. The document contains 4 code snippets that use switch statements to perform different tasks: calculate arithmetic operations, find area of shapes, print branch name based on section, and print Roman numerals for numbers 1-5. 2. The first code snippet uses switch to perform arithmetic operations like addition, subtraction, multiplication, and division based on user input. It checks for divide by zero error. 3. The second code snippet uses switch to calculate and print the area of triangle, square, circle, and rectangle based on user choice. 4. The third code snippet uses switch to print the branch name based on the section chosen by the user. 5. The fourth code snippet uses switch to print the Roman

Uploaded by

Kunal Jain
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/ 6

1.

/*Write and execute a C program to perform a desired arithmetic operation using


switch statement .Declare choice as char data type and check whether the divisor is
zero, if divisor is zero print “Divide by Zero error”. */
#include<stdio.h>
int main()
{
float a,b,res;
char ch;
printf("enter choice");
scanf("%c",&ch);
printf("enter two values \n");
scanf("%f%f",&a,&b);
switch(ch)
{
case '+':
res=a+b;
break;

case '-':
{
res=a-b;
break;
}
case '*':
{
res=a*b;
break;
}
case '/':
{
if(b!=0)
{
res=a/b;
break;
}
else
{
printf("denomintor is 0");
}
break;
}
default:
{
printf("entered choice not found");
}
}
printf("resultant is %f",res);
}

-
2. /*Write and execute a C program to read numbers using keyboard and find the area
of triangle ,square, circle and rectangle using switch statement and display the
result. */

#include<stdio.h>
#define PI 3.147
void main()
{
float radius, length, breadth;
float base, height, area;
int choice;
printf("Enter\n1. To find area of triangle\n2. To find area of Square\n
3. To find area of circle\n4. To find area of rectangle\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter base and height of a triangle\n");
scanf("%f %f", &base, &height);
area = (1.0/2) * base * height;
printf("Area of Triangle:\t%f\n", area);
break;

case 2:
printf("Enter length of a Square\n");
scanf("%f", &length);
area = length * length;
printf("Area of Square:\t%f\n", area);
break;

case 3:
printf("Enter the radius of a Circle\n");
scanf("%f", &radius);
area = PI * radius * radius;
printf("Area of Circle:\t%f\n", area);
break;

case 4:
printf("Enter the length and breadth of a Rectangle\n");
scanf("%f %f", &length, &breadth);
area = length * breadth;
printf("Area of Rectangle:\t%f\n", area);
break;

default:
printf("Invalid Choice\n");
}
}
3. /* Write and execute a C program to print branch name for appropriate section
name using switch statement. Declare choice as char data type and print the result*/

#include<stdio.h>
main()
{
char choice,a,b,A,B;
printf("Enter your section name to find the branch \n");
scanf("%c",&choice);
switch(choice)
{

case 'a': case 'A':case 'b': case 'B':


printf("\nComputer Science Engineering ");
break;

case 'c': case 'C':case 'd': case 'D':


printf("\nElectronics and Communication Engineering");
break;

case 'e': case 'E':


printf("\nElectronics and Instrumentation Engineering");
break;

case 'f': case 'F':


printf("\nChemical Engineering ");
break;

case 'g': case 'G':


printf("\nMedical Electronics ");
break;

case 'h': case 'H':case 'i': case 'I':


printf("\nInformation Science and Engineering");
break;
case 'j': case 'J':case 'k': case 'K':case 'l': case 'L':
printf("\nMechanical Engineering");
break;

case 'm': case 'M':


printf("\nIndustrial and Engineering Management ");
break;

case 'n': case 'N':


printf("\nElectronics & Telecommunication Engineering ");
break;

case 'r': case 'R':


printf("\nBiotechnology Engineering ");
break;

case 'o': case 'O':case 'p': case 'P':


printf("\nCivil Engineering ");
break;

case 'q': case 'Q':


printf("\nElectrical and Electronics Engineering ");
break;

default:
printf("Invalid Choice\n");
}
}
4. /*Write and execute a C program to read a number using keyboard and print the
Roman representation for a given range of numbers 1 to 5 using switch statement*/

#include<stdio.h>
#include<stdlib.h>
int main()
{
int num, rem;
printf("Enter a number: ");
scanf("%d", &num);
printf("Roman numerals: ");
switch(num)
{
case 1: printf("I");
break;
case 2: printf("II");
break;
case 3: printf("III");
break;
case 4: printf("IV");
break;
case 5: printf("V");
break;
default: printf("Wrong choice!");
exit(0);
}
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy