C Program To Add Two Fractions

C Program To Add Two Fractions. If you are looking for the addition of two fractions program in C, here in this tutorial we will help you to learn how to write a c program to add two fractions.

C Program To Add Two Fractions

Just copy paste the below source code to add two fractions in C compiler to test, how the source code works. Debug and learn how it works. Happy coding.

C Program To Add Two Fractions Source Code

```/* C Program To Add Two Fractions - TwoFractions.C */

#include<stdio.h>
int lcm(int,int);

void main()
{
int NM,NM1,DN1,NM2,DN2,LCM; //variable declaration
clrscr();

//Asking user to enter two numbers of fraction one
printf("ENTER FRACTION-ONE (Numerator Denominator): ");

//Asking user to enter two numbers of fraction two
printf("ENTER FRACTION-TWO (Numerator Denominator): ");

LCM = lcm(DN1,DN2); //lcm function
//printf("LCM OF TWO NUMBERS IS %d",LCM);

printf("FRACTION ADDITION IS : "); //
printf("%d/%d + %d/%d = ",NM1,DN1,NM2,DN2);

NM1=NM1*(LCM/DN1);
NM2=NM2*(LCM/DN2);
NM=NM1+NM2;

printf(" %d/%d",NM,LCM);
getch();
}

//lcm function logic
int lcm(int N1,int N2)
{
static int TEMP = 1;
if(TEMP % N2 == 0 && TEMP % N1 == 0)
return TEMP;
TEMP++;
lcm(N1,N2);
return TEMP;
}```