C program to add, subtract and multiply of fraction numbers - IProgramX

Q. Accept two fraction numbers from the user ( numerator, denominator ). Write a menu driven program to perform the following operations till the user selects Exit.


Program

#include<stdio.h>
void main()
{
int ch,num1,deno1,num2,deno2,opnum,opdeno;
printf("enter two fraction no\n");
printf("1st no. numerator  :");
scanf("%d",&num1);
printf("1st no. denominator:");
scanf("%d",&deno1);
printf("2st no. numerator  :");
scanf("%d",&num2);
printf("2st no. denominator:");
scanf("%d",&deno2);
do
{
printf("1.addition\n2.subtraction\n3.multiplication\n4.exit\n");
printf("enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: opnum=(num1*deno2)+(num2*deno1);
opdeno=(deno1*deno2);
printf("addition of %d/%d and %d/%d is %d/%d",num1,deno1,num2,deno2,opnum,opdeno);
break;
case 2: opnum=(num1*deno2)-(num2*deno1);
opdeno=(deno1*deno2);
printf("subtraction of %d/%d and %d/%d is %d/%d",num1,deno1,num2,deno2,opnum,opdeno);
break;
case 3: opnum=(num1*num2);
opdeno=(deno1*deno2);
printf("multiplication of %d/%d and %d/%d is %d/%d",num1,deno1,num2,deno2,opnum,opdeno);
break;
case 4: break;
}
    } while(ch!=4);
}

Output:

enter two fraction no
1st no. numerator  :4
1st no. denominator:6
2st no. numerator  :2
2st no. denominator:4
1.addition
2.subtraction
3.multiplication
4.exit
enter your choice:1
addition of 4/6 and 2/4 is 28/241.addition
2.subtraction
3.multiplication
4.exit
enter your choice:

Post a Comment

0 Comments