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.

Add two fractions in C

Concept

 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) = (6 / 9) = (2 / 3)

  • 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)
  • This will be your simplified answer for the given problem.

Algorithm

  • Initialize variables of numerator and denominator
  • Take user input of two fraction
  • Find numerator using this condition (n1*d2) +(d1*n2 ) where n1,n2 are numerator and d1 and d2 are denominator
  • Find denominator using this condition (d1*d2) for lcm
  • Calculate GCD of a this new numerator and denominator
  • Display a two value of this condition x / gcd, y/gcd

C code

Run
#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("Enter the numerator for 1st number   : ");
    scanf("%d",&numerator1);
    printf("Enter the denominator for 1st number : ");
    scanf("%d",&denominator1);
    printf("Enter the numerator for 2nd number   : ");
    scanf("%d",&numerator2);
    printf("Enter 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("(%d / %d) + (%d / %d) = (%d / %d)", numerator1, denominator1, numerator2, denominator2, x/gcd_no, y/gcd_no);
    
    return 0;
}

Output

Enter the numerator for 1st number   : 1
Enter the denominator for 1st number : 3
Enter the numerator for 2nd number   : 3
Enter the denominator for 2nd number : 9
(1 / 3) + (3 / 9) = (2 / 3)

For similar Questions click on the given button.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

3 comments on “Add two fractions in C | C Program”


  • shailendra

    #include

    int main()
    {

    int numerator1, denominator1,numerator2,denominator2,x,y,z;

    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);
    y=(denominator1*numerator2);
    //denominator
    z=denominator1*denominator2;

    printf(“\nThe added fraction is %d/%d “,x+y,z);
    printf(“\n”);
    return 0;
    }


  • 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)