# Add two fractions in C | C Program

## Add two fractions in C

Here, in this page we will discuss 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)

## Working:-

Step 1. Start.

Step 2.Initialize variables of numerator and denominator

Step 3. Take user input of two fraction

Step 4. Find numerator using this condition (n1*d2) +(d1*n2 ) where n1,n2 are numerator and d1 andd d2 are denominator .

Step 5. Find denominator using this condition (d1*d2) for lcm.

Step 6. Calculate GCD of a this new numerator and denominator .

Step 7. Display a two value of this condition x/gcd,y/gcd);

Step 8. Stop.

## C Program:-

`#include <stdio.h>int main(){     //for initialize variables      int numerator1, denominator1,numerator2,denominator2,x,y,c,gcd_no;    //To take user input of numerators and denominators     printf("\nEnter the numerator for 1st number : ");     scanf("%d",&numerator1);     printf("\nEnter the denominator for 1st number : ");     scanf("%d",&denominator1);     printf("\nEnter the numerator for 2nd number : ");     scanf("%d",&numerator2);     printf("\nEnter the denominator for 2nd number : ");     scanf("%d",&denominator2);    //numerator     x=(numerator1*denominator2)+(denominator1*numerator2);     //denominator     y=denominator1*denominator2;    // Trick part. Reduce it to the simplest form by using gcd.   for(c=1; c <= x && c <= y; ++c)   {       if(x%c==0 && y%c==0)          gcd_no = c;   }    //To display fraction of givien numerators and denominators    printf("\nThe added fraction is %d/%d ",x/gcd_no,y/gcd_no);    printf("\n");  return 0;}`

## Output:-

`Enter the numerator of 1st number : 1Enter the denominator for 1st number : 4Enter the numerator of 2nd number : 3Enter the denominator for 2nd number : 8The added fraction is 5/8`

### 2 comments on “Add two fractions in C | C Program”

• Bhoma

#python solution
#GCD use for simply factor.
def gcd(x,y):
while(y>0):
x,y = y , x%y
return x

def AddFraction(num1 , dem1 , num2 , dem2):
nums = num1*(dem1*dem2)//dem1 + num2*(dem1*dem2)//dem2
denums = dem1*dem2
common_factor = gcd(nums,denums)
x1 = nums//common_factor
y1 = denums//common_factor

print(‘GCD of given numbers are: ‘ , x1 , ‘/’ , y1)

#Driver Code
num1 = int(input(‘enter first numerator: ‘))
dem1 = int(input(‘enter first denumerator: ‘))
num2 = int(input(‘enter second numerator: ‘))
dem2 = int(input(‘enter second denumerator: ‘))
AddFraction(num1 , dem1 , num2 , dem2)

• HelpPrepInsta

Hey, thanks Bhoma, for contributing this code in Python.