A program that asks the user to enter a fraction, then reduce the fraction to lowest terms

PHOTO EMBED

Mon Oct 07 2024 23:37:31 GMT+0000 (Coordinated Universal Time)

Saved by @khadizasultana #c #loop

#include<stdio.h>
// Author- Khadiza Sultana
// 10/8/2024
// Takes a fraction and prints the lowest form of the fraction

int main() {
    int a, b, x, y, gcd; // Take two integers as input
    printf("Enter the fraction (a/b): ");
    scanf("%d/%d", &a, &b);

    x = a;
    y = b;

    // Using Euclidean algorithm to find the GCD of a and b
    while (y != 0) {
        int remainder = x % y;
        x = y;
        y = remainder;
    }

    // x now contains the GCD
    gcd = x;

    // Dividing both numerator and denominator by GCD to get the reduced fraction
    a = a / gcd;
    b = b / gcd;

    printf("In lowest terms: %d/%d\n", a, b);
    
    return 0;
}
content_copyCOPY

To reduce a fraction to lowest term, first compute the GCD of the numerator and denominator by the GCD.