Saturday, 2 April 2016

Q.Create a four-function calculator for fractions. (See Exercise 9 in Chapter 2, and Exercise 4 in this chapter.) Here are the formulas for the four arithmetic operations applied to fractions:
                                                              i.      Addition: a/b + c/d = (a*d + b*c) / (b*d)
                                                            ii.      Subtraction: a/b - c/d = (a*d - b*c) / (b*d)
                                                          iii.      Multiplication: a/b * c/d = (a*c) / (b*d)
                                                          iv.      Division: a/b / c/d = (a*d) / (b*c)

#include<stdio.h>
#include<conio.h>
#include<math.h>

float n1,d1,n2,d2,R,x=1;

char ch;
void main()
{

printf("****MENU****\n");
printf("+ for Addition\n");
printf("- for Subtraction\n");
printf("* for Multi[lication\n");
printf("/ for Division\n");
while(x!=0)
{

printf("\nFor first Fraction\n");
printf("Enter First number:");
scanf("%f",&n1);
printf("Enter second Number:");
scanf("%f",&d1);
printf("\nEnter the operator:");
ch=getche();
printf("\n\nFor Second Fraction\n");
printf("Enter First number:");
scanf("%f",&n2);
printf("Enter second Number:");
scanf("%f",&d2);
switch(ch)
{
  case'+':
{
 
printf("\n%.1f",(n1/d1)+(n2/d2));
break;
}
  case'-':
{
 printf("\n%.1f",(n1/d1)-(n2/d2));
break;
}
  case'*':
printf("\n%.1f",(n1/d1)*(n2/d2));
break;
}
  case'/':
{
  printf("\n%.1f",(n1/d1)/(n2/d2));
break;
}
  default:
 printf("\nINVALID OPERATOR");
}
printf("\nEnter any number>0 to continue and '0' to EXIT:");
scanf("%d",&x);
}
  getch();
}

No comments:

Post a Comment