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.
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%dis the format specifier for integers&numeratorand&denominatorpass the memory addresses to store the input valuesprintf()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.



