Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Program to compare two fractions in C
In C programming, comparing two fractions requires cross-multiplication to avoid floating-point precision issues. Given two fractions with numerators and denominators, we need to determine which fraction has the greater value.
Syntax
struct Fraction {
int nume, deno;
};
struct Fraction greater(struct Fraction first, struct Fraction second);
Approach
To compare fractions a/b and c/d, we use cross-multiplication: if a*d > b*c, then a/b > c/d. This avoids floating-point division and maintains precision.
Example 1: Basic Fraction Comparison
This example compares 4/5 and 3/4 using cross-multiplication −
#include <stdio.h>
struct Fraction {
int nume, deno;
};
struct Fraction greater(struct Fraction first, struct Fraction sec) {
// Cross multiply: first.nume * sec.deno vs first.deno * sec.nume
int crossProduct = first.nume * sec.deno - first.deno * sec.nume;
return (crossProduct > 0) ? first : sec;
}
int main() {
struct Fraction first = {4, 5};
struct Fraction sec = {3, 4};
printf("Comparing fractions: %d/%d and %d/%d<br>",
first.nume, first.deno, sec.nume, sec.deno);
struct Fraction result = greater(first, sec);
printf("The greater fraction is: %d/%d<br>", result.nume, result.deno);
return 0;
}
Comparing fractions: 4/5 and 3/4 The greater fraction is: 4/5
Example 2: Multiple Fraction Comparison
This example demonstrates comparing different pairs of fractions −
#include <stdio.h>
struct Fraction {
int nume, deno;
};
struct Fraction compareFractions(struct Fraction f1, struct Fraction f2) {
// Cross multiplication: f1.nume * f2.deno vs f1.deno * f2.nume
if (f1.nume * f2.deno > f1.deno * f2.nume) {
return f1;
} else {
return f2;
}
}
void printComparison(struct Fraction f1, struct Fraction f2) {
struct Fraction greater = compareFractions(f1, f2);
printf("%d/%d vs %d/%d: Greater is %d/%d<br>",
f1.nume, f1.deno, f2.nume, f2.deno,
greater.nume, greater.deno);
}
int main() {
struct Fraction pairs[][2] = {
{{2, 3}, {4, 3}},
{{1, 2}, {4, 3}},
{{5, 6}, {7, 8}}
};
int numPairs = sizeof(pairs) / sizeof(pairs[0]);
for (int i = 0; i < numPairs; i++) {
printComparison(pairs[i][0], pairs[i][1]);
}
return 0;
}
2/3 vs 4/3: Greater is 4/3 1/2 vs 4/3: Greater is 4/3 5/6 vs 7/8: Greater is 7/8
How It Works
- Cross-multiplication: To compare a/b with c/d, we calculate a×d - b×c
- If positive: First fraction is greater
- If negative: Second fraction is greater
- If zero: Both fractions are equal
Key Points
- Cross-multiplication avoids floating-point precision errors
- The method works for both positive and negative fractions
- Time complexity is O(1) for each comparison
- No division operations are needed
Conclusion
Comparing fractions in C using cross-multiplication is efficient and precise. This approach eliminates floating-point errors and provides reliable results for fraction comparison operations.
Advertisements
