# C++ Program to calculate addition of two Fractions

## Add two fractions in C++ :

In this article we will discuss the program for add two fractions in C++. For this purpose we need to ask the user to enter two fractional values where each fraction must consist a Numerator and a Denominator.

For understanding this in a better way lets suppose an example :

Suppose, First fraction consist of 1 as numerator and 3 as denominator, and Second fraction consist of 3 as numerator and 9 as denominator.

(1 / 3) + (3 / 9)

• Find LCM of 3 and 9 and the result will be 9.
• Multiply 3 in first fraction : (3 / 9) + (3 / 9)
• Add both fractions and then the result will be : (6 / 9)
• Now simplify it by finding the HCF of 6 and 9 and the result will be 3.
• So divide 6 and 9 by 3 and then the result will be : (2 / 3)  ## Algorithm:-

1. User gives 4 inputs (two numerator & two denominator)
2. Inputs are stores in int type variables say num1, den1, num2 and den2.
3. Now LCM of den1 and den2 is found using GCD of den1 and den2.

LCM(den1,den2) = den1 * den2 / GCD(den1,den2)

4. num1 and num2 are now converted into two digits that can be added.

5. These digits are added and the value is stored in another variable say sum.

6. Final numerator and denominator is found using the GCD of sum and LCM

num=sum/GCD(sum, LCM

den=LCM/GCD(sum, LCM)

7. Result is printed.

## C++ Code:-

```    //C++ Program
#include <iostream>
using namespace std;
//main Program
int findGCD(int n1, int n2)
{
int gcd;
for(int i=1; i <= n1 && i <= n2; i++)
{
if(n1%i==0 && n2%i==0)
gcd = i;
}
return gcd;
}
int main()
{
int num1,den1;
cout << "Enter numerator and denominator of first number: ";
//user input
cin >> num1 >> den1;
int num2,den2;
cout << "Enter numerator and denominator of second number: ";
//user input
cin >> num2 >> den2;
//finding lcm of the denominators
int lcm = (den1*den2)/findGCD(den1,den2);
//finding the sum of the numbers
int sum=(num1*lcm/den1) + (num2*lcm/den2);
//normalizing numerator and denominator of result
int num3=sum/findGCD(sum,lcm);
lcm=lcm/findGCD(sum,lcm);
//printing output
cout<<num1<<"/"<<den1<<" + "<<num2<<"/"<<den2<<" = "<<num3<<"/"<<lcm;
return 0;
}```
``` Output
Enter numerator and denominator of first number: 9
7
Enter numerator and denominator of second number: 5
8
9/7 + 5/8 = 107/56``` • Highest Common Factor(HCF): C | C++Java | Python
• Lowest Common Multiple (LCM) : C | C++ | Java | Python
• Greatest Common Divisor : C | C++ | Java | Python
• Binary to Decimal to conversion : C | C++ | Java | Python
• Binary to Octal conversion : C | C++ | Java  | Python
• Decimal to Binary conversion: C | C++ | Java | Python
• Decimal to octal Conversion: C | C++ | Java | Python
• Octal to Binary conversion : C | C++ | Java | Python
• Octal to Decimal conversion : C | C++ | Java | Python
• Quadrants in which a given coordinate lies : C | C++ | Java | Python
• Permutations in which n people can occupy r seats in a classroom : C | C++ | Java | Python
• Maximum number of handshakes: C | C++ | Java | Python
• Addition of two fractions: C | C++ | Java | Python
• Replace all 0’s with 1 in a given integer : C | C++ | Java | Python
• Can a number be expressed as a sum of two prime numbers : C | C++ | Java | Python
• Count possible decoding of a given digit sequence : C | C++ | Java
• Check whether a character is a vowel or consonant : C | C++ | Java | Python
• Check whether a character is a alphabet or not : C | C++ | Java | Python
• Calculate the area of a circle : C | C++ | Java | Python
• Find the ASCII value of a character : C | C++ | Java | Python
• Find the prime numbers between 1 to 100 : C | C++ | Java | Python
• Calculate the number of digits in an integer : C | C++ | Java | Python
• Convert digit/number to words : C | C++ | Java | Python
• Counting number of days in a given month of a year: C | C++ | Java | Python
• Finding Number of times x digit occurs in a given input : C | C++ | Java | Python
• Finding number of integers which has exactly x divisors: C | C++ | Java | Python
• Finding Roots of a quadratic equation : C | C++ | Java | Python