Add two fraction a/b and c/d and print answer in simplest form.
Input: 1/2 + 3/2 Output: 2/1 Input: 1/3 + 3/9 Output: 2/3 Input: 1/5 + 3/15 Output: 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.
1/500 + 2/1500 is equal to 1/300
See below for doing the same using library functions.
Ratio Manipulations in C++ | Set 1 (Arithmetic)
This article is contributed by Rahul Agrawal .If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Program to compare two fractions
- HCF of array of fractions (or rational numbers)
- Product of given N fractions in reduced form
- LCM and HCF of fractions
- Sum of given N fractions in reduced form
- Find N fractions that sum upto a given fraction N/D
- Print all proper fractions with denominators less than equal to N
- Count Pandigital Fractions pairs in given Array
- Count perfect square fractions from given array
- Add two integers of different base and represent sum in smaller base of the two
- Program to add two polynomials
- Program to add two binary strings
- Program to add two integers of given base
- Write a program to add two numbers in base 14
- Add two numbers without using arithmetic operators
- Add two numbers using ++ and/or --
- Count the number of carry operations required to add two numbers
- Add two numbers represented by Stacks
- Count ways to divide C in two parts and add to A and B to make A strictly greater than B
- How to add two Hexadecimal numbers