
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Program to compare two fractions in C
Given two fractions with some numerator nume1 and nume2 and deno1 and deno2 as their respective denominator, the task is to compare both the fractions and find out the greater one. Like we have a fraction 1/2 and 2/3 and the higher one is 2/3 because the value of 1/2 is 0.5 and the value of 2/3 is 0.66667 which is higher.
Input
first.nume = 2, first.deno = 3 second.nume = 4, second.deno = 3
Output
4/3
Explanation
2/3 = 0.66667 < 4/3 = 1.33333
Input
first.nume = 1, first.deno = 2 second.nume = 4, second.deno = 3
Output
4/3
Approach used below is as follows to solve the problem
//baadme likhunga
Algorithm
Start Declare a struct Fraction with elements nume, deno In function Fraction greater(Fraction first, Fraction sec) Step 1→ Declare and Initialize t Y = first.nume * sec.deno - first.deno * sec.nume Step 2→ Return (Y > 0) ? first : sec In function int main() Step 1→ Declare Fraction first = { 4, 5 } Step 2→Fraction sec = { 3, 4 } Step 3→ Fraction res = greater(first, sec) Step 4→ Print res.nume, res.deno Stop
Example
#include <stdio.h> struct Fraction { int nume, deno; }; // Get max of the two fractions Fraction greater(Fraction first, Fraction sec){ //check if the result is in negative then the //second fraction is greater else first is greater int Y = first.nume * sec.deno - first.deno * sec.nume; return (Y > 0) ? first : sec; } int main(){ Fraction first = { 4, 5 }; Fraction sec = { 3, 4 }; Fraction res = greater(first, sec); printf("The greater fraction is: %d/%d
", res.nume, res.deno); return 0; }
Output
If run the above code it will generate the following output −
The greater fraction is: 4/5
- Related Articles
- C Program to add two fractions
- C++ Program to Compare two strings lexicographically
- C program to compare two files and report mismatches
- Program to Compare two strings in Java
- Java Program to Compare Two Objects
- Java Program to Compare Two Strings
- Java Program to compare two sets
- PHP program to compare two dates
- Golang program to compare two strings
- Python Program to compare elements in two dictionaries
- Golang program to compare elements in two slices
- How to compare two arrays in C#?
- How to compare two Dates in C#?
- How to compare two dictionaries in C#?
- How to compare two tuples in C#?

Advertisements