0% found this document useful (0 votes)
27 views5 pages

Day 18 Coding Solutions PDF

The document provides code snippets in C, C++, Java, and Python to add two fractions. It explains that to add fractions x1/y1 and x2/y2, the numerator x3 is calculated as (x1*y2) + (x2*y1) and the denominator y3 is calculated as y1*y2. The code gets the numerator and denominator values for two fractions as input, calculates the sum, and outputs the final fraction. It also includes code to simplify the resulting fraction by dividing both the numerator and denominator by their greatest common divisor.

Uploaded by

sajeed malagi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views5 pages

Day 18 Coding Solutions PDF

The document provides code snippets in C, C++, Java, and Python to add two fractions. It explains that to add fractions x1/y1 and x2/y2, the numerator x3 is calculated as (x1*y2) + (x2*y1) and the denominator y3 is calculated as y1*y2. The code gets the numerator and denominator values for two fractions as input, calculates the sum, and outputs the final fraction. It also includes code to simplify the resulting fraction by dividing both the numerator and denominator by their greatest common divisor.

Uploaded by

sajeed malagi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Talent Battle 100 Days Coding Series

Write a program to Add two fractions

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 Program

#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;
Talent Battle 100 Days Coding Series

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 ++ Program

#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;
Talent Battle 100 Days Coding Series

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 Program

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;
Talent Battle 100 Days Coding Series

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):
Talent Battle 100 Days Coding Series

if(x3%i==0 and y3%i==0):

x3=x3//i

y3=y3//i

print("Sum of fractions is",x3,"/",y3)

You might also like