Friday, March 4, 2011

User-Defined Functions

Develop a top_down modular program to implement a calculator. The program should request the user to input two numbers and display one of the follwing as per the desire of the user:
(a) Sum fo the numbers
(b) Difference of the numbers
(c) Product of the numbers
(d) Division of the numbers
Provide separate functions for performing various tasks such as reading,calculating and displaying. Calculating module should call second level modules to perform the individual mathematical operations. The main function shoud have ony function calls.

#include <stdio.h>
float input(void);
void calculation(float x,float y);
void output(float z);
float x,y,z;
void main()
{
clrscr();
input();
calculation(x,y);
getch();
}
float input(void)
{
printf("Enter the value of X and Y\n");
scanf("%f %f",&x,&y);
return(x,y);
}
void calculation(float x,float y)
{
int i;
float z;
printf("To Add these values press 1\nTo Subtract press 2\nTo Product press 3\
\nFor Division press 4:");
scanf("%d",&i);
switch(i)
{
case 1:z=x+y;
break;
case 2:z=x-y;
break;
case 3:z=x*y;
break;
case 4:z=x/y;
break;
}
output(z);
}
void output(float z)
{
printf("Result is: %5.2f",z);
}

OUTPUT
Enter the value of X and Y
255  456
To Add these values press 1
To Subtract press2
To Product press 3
Fo Division press 4 : 1
Result is : 711.00

Enter the value of X and Y
255  456
To Add these values press 1
To Subtract press2
To Product press 3
Fo Division press 4 : 2
Result is : -201.00

Enter the value of X and Y
255  456
To Add these values press 1
To Subtract press2
To Product press 3
Fo Division press 4 : 3
Result is : 116280.00

Enter the value of X and Y
255  456
To Add these values press 1
To Subtract press2
To Product press 3
Fo Division press 4 : 4
Result is : 0.56

No comments:

Post a Comment