0% found this document useful (0 votes)
3 views

Assignment 4

assignment

Uploaded by

msgcsexam23
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Assignment 4

assignment

Uploaded by

msgcsexam23
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Assignment 4 : To demonstrate decision making statements (switch case)

Q1. Accept two numbers in variables x and y from the user and perform the following operations

Program

#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,i,quo,rem,n,k;
printf("Enter two numbers:\n");
scanf("%d%d",&x,&y);
printf("\n1: Equality\n");
printf("2: to check x is less than y or not\n");
printf("3: to divide x by y & to display quotient & remainder\n");
printf("4: to check Range\n");
printf("5: Swap\n");
printf("Choose any option:\n");
scanf("%d",&i);
switch(i)
{
case 1:
if(x==y)
printf("x is equal to y");
else
printf("x is not equal to y");
break;
case 2:
if(x<y)
printf("x is less than y");
else
printf("x is not less than y");
break;
case 3:
quo=x/y;
rem=x%y;
printf(" Quotiont of %d & %d is: %d\n Remainder of %d & %d is: %d
",x,y,quo,x,y,rem);
break;
case 4:
printf("Enter a number:\n");
scanf("%d",&n);
if(n>=x && n<=y)
printf("%d lies in between %d & %d",n,x,y);
else
printf("%d is not lies between %d & %d",n,x,y);
break;
case 5:
printf("before swap x=%d & y=%d\n",x,y);
k=x;
x=y;
y=k;
printf("After swap x=%d & y=%d",x,y);
break;
default:
printf("Please choose correct option");
}
}

Q2.Apply all the three program development steps for the following examples.

Accept radius from the user and write a program having menu with the following options and
corresponding actions Options Actions

Option Action
1. Area of Circle nt Compute area of circle and print
2. Circumference of Circle Compute Circumference of circle and
print
3.Volume of Sphere Compute Volume of Sphere and print

#include<stdio.h>

#define PI 3.142

void main()

int r,choice;

float area,c,vol;

printf("\n Enter the radius of circle:- ");

scanf("%d",&r);

printf("\n Please make a choice from the following :");

printf("\n \n Options:\t \t \t \t");

printf("Actions:");
printf("\n \n 1. Area of the circle :");

printf("\t \tCompute area of the circle and print");

printf("\n \n 2. Circumference of the circle ");

printf("\tCompute circumference of circle and print");

printf("\n \n 3. Area of the sphere ");

printf("\t \t Compute volume of sphere and print \n \n ");

scanf("%d",&choice);

switch(choice)

case 1:

area=PI*r*r;

printf("\n The area of the circle is %f \n \n",area);

break;

case 2:

c=2*PI*r;

printf("\n The circumference of the circle is %f \n \n",c);

break;

case 3:

vol=(4/3)*PI*r*r;

printf("\n The volume of the sphere is %f \n \n",vol);

break;

default:

printf("\n You haven made invalid choice \n \n");


}

---------------------------------------------------------------------------------------------------------------------

Q3.Write a program having a menu with the following options and corresponding actions

Options Actions
1. Area of square Accept length ,Compute area of square and
print
2. Area of Rectangle Accept length and breadth, Compute area of
rectangle and print
3. Area of triangle Accept base and height Compute area of triangle and print

#include <stdio.h>
void main ()
{
int choice,r,l,w,b,h;
float area;
printf("1. for area of square\n");
printf("2. for area of rectangle\n");
printf("3. for area of triangle\n");
printf("Input your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("enter length of side of the square : ");
scanf("%d",&r);
area=r*r;
break;
case 2:
printf("enter length and width of the rectangle : ");
scanf("%d%d",&l,&w);
area=l*w;
break;
case 3:
printf("enter the base and height of the triangle :");
scanf("%d%d",&b,&h);
area=0.5*b*h;
break;
}
printf("The area is : %f\n",area);
}

---------------------------------------------------------------------------------------------------------------------

Q4.Write a program having menu that has three options - add, subtract or multiply two fractions.
The two fractions and the options are taken as input and the result is displayed as output. Each
fraction is read as two integers, numerator and denominator.

#include<stdio.h>
int main()
{
int a,b,c,d,ch,nu,de;
printf("enter 1 st fraction \nnumerator:");
scanf("%d",&a);
printf("denominator:");
scanf("%d",&b);
printf("enter 2 st fraction \nnumerator:");
scanf("%d",&c);
printf("denominator:");
scanf("%d",&d);
printf("1.add\n2.sub\n3.mul :");
scanf("%d",&ch);
switch(ch)
{
case 1:nu=(a*d)+(c*b);
de=(b*d);
printf("add is %d/%d",nu,de);
break;
case 2:nu=(a*d)-(c*b);
de=(b*d);
printf("sub is %d/%d",nu,de);
break;
case 3:nu=(a*c);
de=(b*d);
printf("mul is %d/%d",nu,de);
break;
}
}

--------------------------------------------------------------------------------------------------------------------

You might also like