# Fraction

A fraction is a ratio of two values. Fractions have the form a/b where a is called the *numerator*, b is called the *denominator* and b cannot equal 0 (since division by 0 is undefined).The denominator gives how many equal parts are there. The numerator represents how many of these are taken. For example, one-half, eight-fifths, three-quarters (1/2, 8/5, 3/4).

**Fact about Fraction : **

- Fractions can be reduced if the numerator and denominator have a greatest common divisor(gcd) greater than 1.
**Addition and Subtraction of Fractions :**When adding or subtracting fractions, they must have the same denominator. If they do not have the same denomimator, we must find a common one for both. To do this, we first need to find the lowest common multiple(lcm) of the two denominators or multiply each fraction by the proper integers so that there will be the same denominator.**Multiplication and Division of Fractions :**When multiplying two fractions, simply multiply the two numerators and multiply the two denominators.When dividing two fractions, the first fraction must be multiplied by the reciprocal of the second fraction.- There are
**three types**of fractions : **Proper Fractions:**The numerator is less than the denominator. For Example, 1/3, 3/4, 2/7**Improper Fractions:**The numerator is greater than (or equal to) the denominator. For Example, 4/3, 11/4, 7/7.**Mixed Fractions:**A whole number and proper fraction together. For Example, 1 1/3, 2 1/4, 16 2/5.

**How to add two fraction ?**

Add two fraction a/b and c/d and print answer in simplest form.

**Examples :**

Input:1/2 + 3/2Output:2/1Input:1/3 + 3/9Output:2/3Input:1/5 + 3/15Output:2/5

**Algorithm to add two fractions**

- Find a common denominator by finding the LCM (Least Common Multiple) of the two denominators.
- Change the fractions to have the same denominator and add both terms.
- Reduce the final fraction obtained into its simpler form by dividing both numerator and denominator by there largest common factor.

## C++

`// C++ program to add 2 fractions ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return gcd of a and b ` `int` `gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(a == 0) ` ` ` `return` `b; ` ` ` `return` `gcd(b % a, a); ` `} ` ` ` `// Function to convert the obtained fraction ` `// into it's simplest form ` `void` `lowest(` `int` `& den3, ` `int` `& num3) ` `{ ` ` ` `// Finding gcd of both terms ` ` ` `int` `common_factor = gcd(num3, den3); ` ` ` ` ` `// Converting both terms into simpler ` ` ` `// terms by dividing them by common factor ` ` ` `den3 = den3 / common_factor; ` ` ` `num3 = num3 / common_factor; ` `} ` ` ` `// Function to add two fractions ` `void` `addFraction(` `int` `num1, ` `int` `den1, ` `int` `num2, ` ` ` `int` `den2, ` `int` `& num3, ` `int` `& den3) ` `{ ` ` ` `// Finding gcd of den1 and den2 ` ` ` `den3 = gcd(den1, den2); ` ` ` ` ` `// Denominator of final fraction obtained ` ` ` `// finding LCM of den1 and den2 ` ` ` `// LCM * GCD = a * b ` ` ` `den3 = (den1 * den2) / den3; ` ` ` ` ` `// Changing the fractions to have same denominator ` ` ` `// Numerator of the final fraction obtained ` ` ` `num3 = (num1) * (den3 / den1) + (num2) * (den3 / den2); ` ` ` ` ` `// Calling function to convert final fraction ` ` ` `// into it's simplest form ` ` ` `lowest(den3, num3); ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `int` `num1 = 1, den1 = 500, num2 = 2, den2 = 1500, den3, num3; ` ` ` ` ` `addFraction(num1, den1, num2, den2, num3, den3); ` ` ` ` ` `printf` `(` `"%d/%d + %d/%d is equal to %d/%d\n"` `, num1, den1, ` ` ` `num2, den2, num3, den3); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to add 2 fractions ` ` ` `# Function to return gcd of a and b ` `def` `gcd(a, b): ` ` ` `if` `(a ` `=` `=` `0` `): ` ` ` `return` `b ` ` ` `return` `gcd(b ` `%` `a, a) ` ` ` `# Function to convert the obtained ` `# fraction into it's simplest form ` `def` `lowest(den3, num3): ` ` ` ` ` `# Finding gcd of both terms ` ` ` `common_factor ` `=` `gcd(num3, den3) ` ` ` ` ` `# Converting both terms ` ` ` `# into simpler terms by ` ` ` `# dividing them by common factor ` ` ` `den3 ` `=` `int` `(den3 ` `/` `common_factor) ` ` ` `num3 ` `=` `int` `(num3 ` `/` `common_factor) ` ` ` `print` `(num3, ` `"/"` `, den3) ` ` ` `# Function to add two fractions ` `def` `addFraction(num1, den1, num2, den2): ` ` ` ` ` `# Finding gcd of den1 and den2 ` ` ` `den3 ` `=` `gcd(den1, den2) ` ` ` ` ` `# Denominator of final ` ` ` `# fraction obtained finding ` ` ` `# LCM of den1 and den2 ` ` ` `# LCM * GCD = a * b ` ` ` `den3 ` `=` `(den1 ` `*` `den2) ` `/` `den3 ` ` ` ` ` `# Changing the fractions to ` ` ` `# have same denominator Numerator ` ` ` `# of the final fraction obtained ` ` ` `num3 ` `=` `((num1) ` `*` `(den3 ` `/` `den1) ` `+` ` ` `(num2) ` `*` `(den3 ` `/` `den2)) ` ` ` ` ` `# Calling function to convert ` ` ` `# final fraction into it's ` ` ` `# simplest form ` ` ` `lowest(den3, num3) ` ` ` `# Driver Code ` `num1 ` `=` `1` `; den1 ` `=` `500` `num2 ` `=` `2` `; den2 ` `=` `1500` ` ` `print` `(num1, ` `"/"` `, den1, ` `" + "` `, num2, ` `"/"` `, ` ` ` `den2, ` `" is equal to "` `, end ` `=` `"") ` ` ` `addFraction(num1, den1, num2, den2) ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to add ` `// 2 fractions ` ` ` `// Function to return ` `// gcd of a and b ` `function` `gcd(` `$a` `, ` `$b` `) ` `{ ` ` ` `if` `(` `$a` `== 0) ` ` ` `return` `$b` `; ` ` ` `return` `gcd(` `$b` `% ` `$a` `, ` `$a` `); ` `} ` ` ` `// Function to convert the ` `// obtained fraction into ` `// it's simplest form ` `function` `lowest(&` `$den3` `, &` `$num3` `) ` `{ ` ` ` `// Finding gcd of both terms ` ` ` `$common_factor` `= gcd(` `$num3` `, ` `$den3` `); ` ` ` ` ` `// Converting both terms ` ` ` `// into simpler terms by ` ` ` `// dividing them by common factor ` ` ` ` ` `$den3` `= (int)` `$den3` `/ ` `$common_factor` `; ` ` ` `$num3` `= (int) ` `$num3` `/ ` `$common_factor` `; ` `} ` ` ` `// Function to add ` `// two fractions ` `function` `addFraction(` `$num1` `, ` `$den1` `, ` `$num2` `, ` ` ` `$den2` `, &` `$num3` `, &` `$den3` `) ` `{ ` ` ` `// Finding gcd of den1 and den2 ` ` ` `$den3` `= gcd(` `$den1` `, ` `$den2` `); ` ` ` ` ` `// Denominator of final ` ` ` `// fraction obtained finding ` ` ` `// LCM of den1 and den2 ` ` ` `// LCM * GCD = a * b ` ` ` `$den3` `= (` `$den1` `* ` `$den2` `) / ` `$den3` `; ` ` ` ` ` `// Changing the fractions to ` ` ` `// have same denominator Numerator ` ` ` `// of the final fraction obtained ` ` ` `$num3` `= (` `$num1` `) * (` `$den3` `/ ` `$den1` `) + ` ` ` `(` `$num2` `) * (` `$den3` `/ ` `$den2` `); ` ` ` ` ` `// Calling function to convert ` ` ` `// final fraction into it's ` ` ` `// simplest form ` ` ` `lowest(` `$den3` `, ` `$num3` `); ` `} ` ` ` `// Driver Code ` `$num1` `= 1; ` `$den1` `= 500; ` `$num2` `= 2; ` `$den2` `= 1500; ` `$den3` `; ` `$num3` `; ` `addFraction(` `$num1` `, ` `$den1` `, ` `$num2` `, ` ` ` `$den2` `, ` `$num3` `, ` `$den3` `); ` `echo` `$num1` `, ` `"/"` `, ` `$den1` `, ` `" + "` `, ` ` ` `$num2` `, ` `"/"` `, ` `$den2` `, ` `" is equal to "` `, ` ` ` `$num3` `, ` `"/"` `, ` `$den3` `, ` `"\n"` `; ` ` ` ` ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

1/500 + 2/1500 is equal to 1/300

**More problems related to Fraction:**

- LCM and HCF of fractions
- Represent the fraction of two numbers in the string format
- Program to compare two fractions
- Convert Binary fraction to Decimal
- Convert decimal fraction to binary number
- Fractional Knapsack Problem
- Find Recurring Sequence in a Fraction

## Recommended Posts:

- Find ΔX which is added to numerator and denominator both of fraction (a/b) to convert it to another fraction (c/d)
- Fraction module in Python
- First occurrence of a digit in a given fraction
- Reduce the fraction to its lowest form
- Find Recurring Sequence in a Fraction
- Greedy Algorithm for Egyptian Fraction
- Convert Binary fraction to Decimal
- Print first N terms of series (0.25, 0.5, 0.75, ...) in fraction representation
- Convert decimal fraction to binary number
- as_integer_ratio() in Python for reduced fraction of a given rational
- Maximum rational number (or fraction) from an array
- Represent the fraction of two numbers in the string format
- Expressing a fraction as a natural number under modulo 'm'
- Largest proper fraction with sum of numerator and denominator equal to a given number

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.