Simplify Fractions (GCD) in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to simplify fractions using the Greatest Common Divisor (GCD) algorithm in C programming. The lab covers the following steps: reading the numerator and denominator from user input, computing the GCD, and then dividing both the numerator and denominator by the GCD to obtain the simplified fraction. This lab aims to help you understand the concept of fraction simplification and implement it using C programming techniques.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/operators("`Operators`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435194{{"`Simplify Fractions (GCD) in C`"}} c/variables -.-> lab-435194{{"`Simplify Fractions (GCD) in C`"}} c/operators -.-> lab-435194{{"`Simplify Fractions (GCD) in C`"}} c/user_input -.-> lab-435194{{"`Simplify Fractions (GCD) in C`"}} c/math_functions -.-> lab-435194{{"`Simplify Fractions (GCD) in C`"}} end

Read Numerator and Denominator

In this step, you will learn how to read the numerator and denominator from user input in a C program for fraction simplification.

First, let's create a new C file for our fraction simplification program:

cd ~/project
nano fraction_simplify.c

Now, add the following code to read the numerator and denominator:

#include <stdio.h>

int main() {
    int numerator, denominator;

    printf("Enter the numerator: ");
    scanf("%d", &numerator);

    printf("Enter the denominator: ");
    scanf("%d", &denominator);

    printf("Numerator: %d\n", numerator);
    printf("Denominator: %d\n", denominator);

    return 0;
}

Let's compile and run the program:

gcc fraction_simplify.c -o fraction_simplify
./fraction_simplify

Example output:

Enter the numerator: 12
Enter the denominator: 18
Numerator: 12
Denominator: 18

Code Explanation:

  • scanf() is used to read integer input from the user
  • %d is the format specifier for integers
  • &numerator and &denominator pass the memory addresses to store the input values
  • printf() is used to display the entered numerator and denominator

Compute GCD and Divide Both

In this step, you will implement the Greatest Common Divisor (GCD) algorithm and use it to simplify the fraction.

Let's modify the previous program to add a GCD calculation function:

cd ~/project
nano fraction_simplify.c

Update the code with the following implementation:

#include <stdio.h>

// Function to compute GCD using Euclidean algorithm
int computeGCD(int a, int b) {
    // Ensure positive values
    a = (a > 0) ? a : -a;
    b = (b > 0) ? b : -b;

    // Euclidean algorithm
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }

    return a;
}

int main() {
    int numerator, denominator, gcd;

    printf("Enter the numerator: ");
    scanf("%d", &numerator);

    printf("Enter the denominator: ");
    scanf("%d", &denominator);

    // Compute GCD
    gcd = computeGCD(numerator, denominator);

    // Simplify fraction
    int simplified_numerator = numerator / gcd;
    int simplified_denominator = denominator / gcd;

    printf("Original Fraction: %d/%d\n", numerator, denominator);
    printf("Simplified Fraction: %d/%d\n", simplified_numerator, simplified_denominator);

    return 0;
}

Compile and run the program:

gcc fraction_simplify.c -o fraction_simplify
./fraction_simplify

Example output:

Enter the numerator: 12
Enter the denominator: 18
Original Fraction: 12/18
Simplified Fraction: 2/3

Code Explanation:

  • computeGCD() implements the Euclidean algorithm to find the Greatest Common Divisor
  • The function handles both positive and negative numbers
  • Fraction is simplified by dividing both numerator and denominator by their GCD
  • Handles edge cases like zero and negative numbers

Print the Simplified Fraction

In this step, you will enhance the fraction simplification program to handle various input scenarios and provide clear output formatting.

Let's update the program to include more robust fraction printing:

cd ~/project
nano fraction_simplify.c

Update the code with the following implementation:

#include <stdio.h>

// Function to compute GCD using Euclidean algorithm
int computeGCD(int a, int b) {
    a = (a > 0) ? a : -a;
    b = (b > 0) ? b : -b;

    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }

    return a;
}

// Function to print fraction with special case handling
void printFraction(int numerator, int denominator) {
    // Handle division by zero
    if (denominator == 0) {
        printf("Error: Division by zero is not allowed.\n");
        return;
    }

    // Compute GCD
    int gcd = computeGCD(numerator, denominator);

    // Simplify fraction
    int simplified_numerator = numerator / gcd;
    int simplified_denominator = denominator / gcd;

    // Handle sign
    if (simplified_denominator < 0) {
        simplified_numerator = -simplified_numerator;
        simplified_denominator = -simplified_denominator;
    }

    // Print results
    printf("Original Fraction: %d/%d\n", numerator, denominator);

    // Different output for whole numbers and fractions
    if (simplified_denominator == 1) {
        printf("Simplified Fraction: %d\n", simplified_numerator);
    } else {
        printf("Simplified Fraction: %d/%d\n",
               simplified_numerator, simplified_denominator);
    }
}

int main() {
    int numerator, denominator;

    printf("Enter the numerator: ");
    scanf("%d", &numerator);

    printf("Enter the denominator: ");
    scanf("%d", &denominator);

    // Call fraction printing function
    printFraction(numerator, denominator);

    return 0;
}

Compile and run the program:

gcc fraction_simplify.c -o fraction_simplify
./fraction_simplify

Example outputs:

Enter the numerator: 12
Enter the denominator: 18
Original Fraction: 12/18
Simplified Fraction: 2/3

Enter the numerator: 15
Enter the denominator: 5
Original Fraction: 15/5
Simplified Fraction: 3

Enter the numerator: -12
Enter the denominator: 18
Original Fraction: -12/18
Simplified Fraction: -2/3

Code Explanation:

  • Added printFraction() function to handle fraction printing
  • Handles special cases like division by zero
  • Manages sign of simplified fraction
  • Prints whole numbers when denominator is 1
  • Preserves the original sign of the fraction

Summary

In this lab, you will learn how to read the numerator and denominator from user input, compute the Greatest Common Divisor (GCD) using the Euclidean algorithm, and then simplify the fraction by dividing both the numerator and denominator by the GCD. The simplified fraction will be printed as the output.

The key steps are: 1) reading the numerator and denominator from user input, 2) implementing the GCD algorithm to find the greatest common divisor, and 3) dividing both the numerator and denominator by the GCD to obtain the simplified fraction.

Other C Tutorials you may like