Assignment 4
Assignment 4
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;
scanf("%d",&r);
printf("Actions:");
printf("\n \n 1. Area of the circle :");
scanf("%d",&choice);
switch(choice)
case 1:
area=PI*r*r;
break;
case 2:
c=2*PI*r;
break;
case 3:
vol=(4/3)*PI*r*r;
break;
default:
---------------------------------------------------------------------------------------------------------------------
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;
}
}
--------------------------------------------------------------------------------------------------------------------