# Thread: Ouputing GCD and simplest fractions!?!?!?!?!?!?!?!

1. ## HELP ME!?!?!

How can i make it find the GCD. My function i have does not reduce it. How can i make it reduce. Please HELP!

Code:
```#include <stdio.h>
#include <math.h>

typedef struct fraction{
int n;
int d;
}Frac;

Frac getFrac(void);
void addFrac(Frac a,Frac b,Frac c,Frac e,Frac *tot);
Frac gcd(Frac tot);

//////////////////////////////////////////////MAIN/////////////////////////////////////////////
int main()
{
Frac a={0,1},b={0,1},c={0,1},e={0,1},tot;
a=getFrac();
b=getFrac();
c=getFrac();
e=getFrac();
gcd(tot);
printf("GCD is:%d",a);
return 0;
}

////////////////////////////////////////////FUNCTIONS//////////////////////////////////////////

//getting fractions
Frac getFrac(void)
{
Frac a;

printf( "Enter Fraction\n" );
scanf( "%d%*c%d",&a.n,&a.d );
while ( a.d==0 ) {
printf( "Enter Fraction Again\n" );
scanf( "%d%*c%d",&a.n,&a.d );
}
return a;
}

void addFrac(Frac a,Frac b,Frac c,Frac e,Frac *tot)
{
int sum1,sum2,numer1,denom1,sum3,sum4,sum5,sum6,numer2,denom2,totalnumer,totaldenom;
float z;
sum1=a.d*b.n;
sum2=a.n*b.d;
numer1=sum1+sum2;
denom1=a.d*b.d;
printf( "\na+b= %d/%d",numer1,denom1 );

sum3=c.d*e.n;
sum4=c.n*e.d;
numer2=sum3+sum4;
denom2=c.d*e.d;

printf( "\nc+e= %d/%d\n",numer2,denom2 );

sum5=denom1*numer2;
sum6=denom2*numer1;
totalnumer=sum5+sum6;
totaldenom=denom1*denom2;
tot->n=totalnumer;
tot->d=totaldenom;
//total frac result
printf("\nTotal Fraction Added is: %d/%d\n",tot->n,tot->d );
//decimal
z=(float)tot->n/tot->d;
printf("\nTotal Decimal is: %.5f\n",z);
}

//greatest common denominator
int gcd(Frac tot)
{
if(tot.d==0)
{
}
else
{
return gcd(tot);
}
}```
thanks for the help!

2. >How can i make it reduce.
Code:
```int gcd ( int m, int  n )
{
return ( n != 0 ) ? gcd ( n, m % n ) : m;
}

void reduce ( int *n, int *d )
{
int div = gcd ( *n, *d );

if ( div != 0 ) {
*n /= div;
*d /= div;
}
}```
-Prelude

3. ## ok?

where do i put that in my code and get it 2 work?

4. >where do i put that in my code and get it 2 work?
Just call it wherever you need to reduce a fraction and place the definition before the first use. :P

-Prelude

5. ## Ouputing GCD and simplest fractions!?!?!?!?!?!?!?!

everything else works, besides the dang GCD and the simplfy, i jus dont get it, please help me as much as u can!

Code:
```#include <stdio.h>
#include <math.h>
#include <conio.h>

typedef struct fraction{
int n;
int d;
}Frac;

Frac getFrac(void);
int addFrac(Frac a,Frac b,Frac c,Frac e,Frac *tot);
int gcd(d,r,denom1,denom2);
int simp(reduceNumer,reduceDenom,d);

//////////////////////////////////////////////MAIN/////////////////////////////////////////////
int main()
{
Frac a={0,1},b={0,1},c={0,1},e={0,1},tot,d,r,denom1,denom2,reduceNumer,reduceDenom;
a=getFrac();
b=getFrac();
c=getFrac();
e=getFrac();

//printing inputed
printf("\t\t%d/%d\t",a.n,a.d);
printf("\n\t\t%d/%d\t",b.n,b.d);
printf("\n\t\t%d/%d\t",c.n,c.d);
printf("\n\t\t%d/%d\t",e.n,e.d);

gcd(d,r,denom1,denom2);
printf("GCD is: %d\n",d);
simp(reduceNumer,reduceDenom,d);
return 0;
}

////////////////////////////////////////////FUNCTIONS//////////////////////////////////////////

//getting fractions
Frac getFrac(void)
{
Frac a;

printf( "Enter Fraction\n" );
scanf( "%d%*c%d",&a.n,&a.d );
while ( a.d==0 ) {
printf( "Enter Fraction Again\n" );
scanf( "%d%*c%d",&a.n,&a.d );
}
system("cls");
return a;

}

int addFrac(Frac a,Frac b,Frac c,Frac e,Frac *tot)
{

int sum1,sum2,numer1,denom1,sum3,sum4,sum5,sum6,numer2,denom2,totalnumer,totaldenom;
float z;
sum1=a.d*b.n;
sum2=a.n*b.d;
numer1=sum1+sum2;
denom1=a.d*b.d;

sum3=c.d*e.n;
sum4=c.n*e.d;
numer2=sum3+sum4;
denom2=c.d*e.d;

sum5=denom1*numer2;
sum6=denom2*numer1;
totalnumer=sum5+sum6;
totaldenom=denom1*denom2;
tot->n=totalnumer;
tot->d=totaldenom;
//total frac result
if(tot->d==1)
{
printf("Fraction is: %d",(tot->n/tot->d));
}
else
{
printf("\nTotal Fraction Added is: %d/%d\n",tot->n,tot->d );
}//decimal
z=(float)tot->n/tot->d;
printf("\nTotal Decimal is: %.5f\n",z);
return denom1,denom2;
}

//greatest common denominator
int gcd(d,r,denom1,denom2)
{
d = denom2;
r = denom1;

while( r != 0 )
{
d = r;
r = d % r;
}
return r,d;
}

//simplify
int simp(Frac tot,int d)
{
int reduceNumer,reduceDenom;
reduceNumer=tot.n=tot.n/d;
reduceDenom=tot.d=tot.d/d;

printf("\nLowest Terms is: %d/%d\n",reduceNumer,reduceDenom);
return 0;
}```
thanks for your help. I appreciate it VERY much. Jus edit my code and tell me what it is u are doing, comments are cool. Thanx!

6. >everything else works, besides the dang GCD and the simplfy, i jus dont get it, please help me as much as u can!
No, your question has been answered in at least three different threads all started by you. I even gave you the FULL code to reduce a fraction AND told you how to use it. If you still can't solve your problem then I suggest you pick up a book on C and start reading because everything has been handed to you already, all you have to do is cut and paste. Last time I checked that wasn't a difficult thing to do.

Do NOT start another thread asking the same thing just because we didn't write your program for you.

-Prelude