Description
Get the values for numerator and denominator of two fractions, then add that fractions. Consider the following format
x3/y3 = (x1/y1) + (x2/y2)
here x3 = (x1*y2) + (x2*y1)
and y3 = (y1*y2)
Input
2 3
4 3
Output
2/1
C
#include <stdio.h>
int main()
{
int x1,y1,x2,y2,x3,y3,Div,i;
printf("Enter value for x1 and y1: ");
scanf("%d%d",&x1,&y1);
printf("Enter value for x2 and y2: ");
scanf("%d%d",&x2,&y2);
x3=(x1*y2)+(x2*y1);
y3=y1*y2;
if(x3>y3)
Div=y3;
else
Div=x3;
for(i=Div;i>0;i--)
{
if(x3%i==0 && y3%i==0)
{
x3=x3/i;
y3=y3/i;
}
}
printf("Sum of two fractions is %d/%d",x3,y3);
return 0;
}
C++
#include <iostream>
using namespace std;
int main()
{
int x1,y1,x2,y2,x3,y3,Div,i;
cout<<"Enter value for x1 and y1: ";
cin>>x1>>y1;
cout<<"Enter value for x2 and y2: ";
cin>>x2>>y2;
x3=(x1*y2)+(x2*y1);
y3=y1*y2;
if(x3>y3)
Div=y3;
else
Div=x3;
for(i=Div;i>0;i--)
{
if(x3%i==0 && y3%i==0)
{
x3=x3/i;
y3=y3/i;
}
}
cout<<"Sum of two fractions is "<<x3<<"/"<<y3;
return 0;
}
Java
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter the value for x1 and y1: ");
int x1=sc.nextInt();
int y1=sc.nextInt();
System.out.print("Enter the value for x2 and y2: ");
int x2=sc.nextInt();
int y2=sc.nextInt();
int Div;
int x3=(x1*y2)+(x2*y1);
int y3=y1*y2;
if(x3>y3)
Div=y3;
else
Div=x3;
for(int i=Div;i>0;i--)
{
if(x3%i==0 && y3%i==0)
{
x3=x3/i;
y3=y3/i;
}
}
System.out.print("Sum of fractions is "+x3+"/"+y3);
}
}
Python
x1=int(input("Enter value for x1: "))
y1=int(input("Enter value for y1: "))
x2=int(input("Enter value for x2: "))
y2=int(input("Enter value for y2: "))
x3 = (x1*y2)+(x2*y1)
y3 = y1*y2
div =0
if(x3>y3):
div=y3
else:
div=x3
for i in range(div,0,-1):
if(x3%i==0 and y3%i==0):
x3=x3//i
y3=y3//i
print("Sum of fractions is",x3,"/",y3)