Product of given N fractions in reduced form in C

Given the numerator and denominator of N fractions, the task is to find the product of all N fractions and output the result in reduced form. The product of fractions is calculated by multiplying all numerators together and all denominators together, then reducing the resulting fraction to its simplest form.

For example, if we have fractions 4/5 and 3/4, their product would be (4×3)/(5×4) = 12/20, which reduces to 3/5.

Fraction Multiplication: 4/5 × 3/4 = (4×3)/(5×4) = 12/20 = 3/5 4 5 × 3 4 = 3 5

Syntax

struct fraction {
    int num;    // numerator
    int den;    // denominator
};

int GCD(int a, int b);
void product(int n, struct fraction f[]);

Algorithm

  1. Define a structure to represent fractions with numerator and denominator
  2. Multiply all numerators together to get the product numerator
  3. Multiply all denominators together to get the product denominator
  4. Find the GCD of the product numerator and denominator
  5. Divide both by their GCD to get the reduced form

Example

Here's a complete program that calculates the product of N fractions in reduced form −

#include <stdio.h>

struct fraction {
    int num;
    int den;
};

// 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 calculate and print the product in reduced form
void product(int n, struct fraction f[]) {
    int prod_num = 1, prod_den = 1;
    
    // Calculate product of all numerators and denominators
    for (int i = 0; i < n; i++) {
        prod_num *= f[i].num;
        prod_den *= f[i].den;
    }
    
    // Find GCD and reduce the fraction
    int gcd = GCD(prod_num, prod_den);
    prod_num /= gcd;
    prod_den /= gcd;
    
    printf("Product in reduced form: %d/%d<br>", prod_num, prod_den);
}

int main() {
    struct fraction f[3] = {
        {1, 2},  // 1/2
        {2, 1},  // 2/1
        {5, 6}   // 5/6
    };
    
    int n = sizeof(f) / sizeof(f[0]);
    
    printf("Input fractions: ");
    for (int i = 0; i < n; i++) {
        printf("%d/%d ", f[i].num, f[i].den);
    }
    printf("<br>");
    
    product(n, f);
    return 0;
}
Input fractions: 1/2 2/1 5/6 
Product in reduced form: 5/6

How It Works

  • Step 1: The product of 1/2 × 2/1 × 5/6 = (1×2×5)/(2×1×6) = 10/12
  • Step 2: GCD(10, 12) = 2
  • Step 3: Reduced form: (10÷2)/(12÷2) = 5/6

Key Points

  • Always reduce the final result using GCD to get the simplest form
  • The GCD function uses the Euclidean algorithm for efficiency
  • Handle the case where denominators might become zero in real applications
  • Time complexity: O(n + log(min(num, den))) where n is the number of fractions

Conclusion

This approach efficiently calculates the product of N fractions by multiplying numerators and denominators separately, then reducing using GCD. The method ensures the result is always in its simplest form.

Updated on: 2026-03-15T12:58:24+05:30

290 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements