Help me, error keep occurs. Your program should handle the operation of addition
ID: 3766286 • Letter: H
Question
Help me, error keep occurs.
Your program should handle the operation of addition, subtraction, multiplication and division. The answer does not have to be in lowest term for this version of program. Plan your program so that it is modular. Have the input done is one module, the output done in a second module, and the calculation done in a third. Put each module in a separate source code file. The function main() should reside in its own module and be a driver program. If possible, try to write the program without using any global variables.Explanation / Answer
Answer :
#include <stdio.h>
#include <stdlib.h>
struct fraction
{
int real, img;
};
int main()
{
int choice, temp1, temp2, temp3;
struct fraction a, b, c;
int k,l,m,n;
while(1)
{
printf("Press 1 to add two fraction numbers. ");
printf("Press 2 to subtract two fraction numbers. ");
printf("Press 3 to multiply two fraction numbers. ");
printf("Press 4 to divide two fraction numbers. ");
printf("Press 5 to exit. ");
printf("Enter your choice ");
scanf("%d",&choice);
if( choice == 5)
exit(0);
if(choice >= 1 && choice <= 4)
{
printf("Enter a and b where a + ib is the first fraction number.");
printf(" a = ");
scanf("%d", &a.real);
printf("b = ");
scanf("%d", &a.img);
printf("Enter c and d where c + id is the second fraction number.");
printf(" c = ");
scanf("%d", &b.real);
printf("d = ");
scanf("%d", &b.img);
}
if ( choice == 1 )
{
c.real = a.real /a.img;
c.img = b.real/ b.img;
k=c.real+c.img;
if ( k>=0)
printf("Sum of two fraction numbers = %d ",k);
else
printf("Sum of two fraction numbers is not possible");
}
else if ( choice == 2 )
{
c.real = a.real /a.img;
c.img = b.real/ b.img;
l=c.real-c.img;
if ( l>=0)
printf("Difference of two fraction numbers = %d",l);
else
printf("Difference of two fraction numbers is not possible");
}
else if ( choice == 3 )
{
c.real = a.real /a.img;
c.img = b.real/ b.img;
m=c.real*c.img;
if ( m>=0)
printf("Multiplication of two fraction numbers = %d ",m);
else
printf("Multiplication of two fraction numbers is not possible");
}
else if ( choice == 4 )
{
c.real = a.real /a.img;
c.img = b.real/ b.img;
n=c.real/c.img;
if ( n>=0)
printf("Division of two fraction numbers = %d ",n);
else
printf("Division of two fraction numbers is not possible");
}
else
printf("Invalid choice.");
printf(" Press any key to enter choice again... ");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.