C Program to perfrom addition, subtract or multiply of two fractions -IProgramx

Q. 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


Program

#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;
    }
}

Output:

enter 1 st fraction
numerator:4
denominator:7
enter 2 st fraction
numerator:3
denominator:6
1.add
2.sub
3.mul :2
sub is 3/42

Post a Comment

0 Comments