
- 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
C Program to add two fractions
Given with the input as fraction i.e. a/b and c/d where a, b, c and d can be any integer values other than 0 and the task is to add these two fraction to generate their final sum.
Fractions are represented by −
- a / b, where a is known as numerator and b is known as denominator.
- a and b can have any numeric values but b can have any numeric value other than 0.
- Sum of two fractions is represented as a / b + c / d, and the rule for adding the two terms is that their denominator must be equal and if they are not equal they should be made equal and then only addition can be performed.
Example
Input-: 1/4 + 2/12 Output-: 5/12 Since both the fractions denominators are unequal so to make them equal either GCD or LCM can be calculated. So in this case by multiplying the denominator which is 4 by 3 we can make them equal (1 * 3) / (4 * 3) = 3 / 12 Add both the terms: 3 / 12 + 2 / 12 = 5 / 12 Input-: 1/4 + 2/4 Output-: 3/4 Since both the terms have same denominator they can be directly added
Algorithm
In function int gcd(int a, int b) Step 1-> If a == 0 then, return b Step 2-> Return gcd(b%a, a) In function void smallest(int &den3, int &n3) Step 1-> Declare and initialize common_factor as gcd(n3,den3) Step 2-> Set den3 = den3/common_factor Step 3-> Set n3 = n3/common_factor In Function void add_frac(int n1, int den1, int n2, int den2, int &n3, int &den3) Step 1-> Set den3 = gcd(den1,den2) Step 2-> Set den3 = (den1*den2) / den3 Step 3-> Set n3 = (n1)*(den3/den1) + (n2)*(den3/den2) Step 4-> Call function smallest(den3,n3) In Function int main() Step 1-> Declare and initialize n1=1, den1=4, n2=2, den2=12, den3, n3 Step 2-> Call add_frac(n1, den1, n2, den2, n3, den3) Step 3-> Print the values of n1, den1, n2, den2, n3, den3
Example
#include <stdio.h> int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } void smallest(int &den3, int &n3) { // Finding gcd of both terms int common_factor = gcd(n3,den3); den3 = den3/common_factor; n3 = n3/common_factor; } void add_frac(int n1, int den1, int n2, int den2, int &n3, int &den3) { // to find the gcd of den1 and den2 den3 = gcd(den1,den2); // LCM * GCD = a * b den3 = (den1*den2) / den3; // Changing the inputs to have same denominator // Numerator of the final fraction obtained n3 = (n1)*(den3/den1) + (n2)*(den3/den2); smallest(den3,n3); } // Driver program int main() { int n1=1, den1=4, n2=2, den2=12, den3, n3; add_frac(n1, den1, n2, den2, n3, den3); printf("%d/%d + %d/%d = %d/%d
", n1, den1, n2, den2, n3, den3); return 0; }
Output
1/4 + 2/12 = 5/12
- Related Articles
- Program to compare two fractions in C
- C# program to add two matrices
- C++ Program to Add Two Numbers
- C# Program to Add Two TimeSpan
- C Program to Add two Integers
- How to add mixed fractions?
- Program to add two binary strings in C++
- Program to Add Two Complex Numbers in C
- How to add mixed unlike fractions?
- C++ program to overload addition operator to add two matrices
- C++ Program to Add Two Matrix Using Multi-dimensional Arrays
- Write a program to add two complex numbers using C
- C++ program to overload addition operator to add two complex numbers
- Add two numbers represented by two arrays in C Program
- Java program to add two matrices.

Advertisements