Horje
Reduce fractions in C Code Example
Reduce fractions in C
#include <stdio.h>

/** Struct describing a Fraction */
typedef struct Fraction
{
    int num; // numerator
    int den; // denominator
} Fraction;

/** Returns the gcd of the fraction */
int frc_gcd (Fraction f)
{
    int reminder;
    while ( f.num != 0 )
    {
        reminder = f.num; 
        f.num = f.den % f.num;  
        f.den = reminder;
    }

    return f.den;
}

/** Simplify the given fraction 
 *  Both numerator and denominator gets divided by the gcd of them 
 **/
void frc_simplify(Fraction* f) {
    int gcdValue = frc_gcd(*f);
    f->num = f->num / gcdValue;
    f->den = f->den / gcdValue;
}

/** Prints the fraction formatted
 *  If denominator is 1 it's omitted
 **/
void frc_print(Fraction f){
    f.den != 1 ?
        printf("%d/%d", f.num, f.den) : 
        printf("%d", f.num);
}

int main (int argc, const char * argv[]) {

    Fraction f = {6,3};
    frc_simplify(&f);

    printf("In lowest terms:");
    frc_print(f);
}




Whatever

Related
mi 9t no audio Code Example mi 9t no audio Code Example
how to import buttonbehaviour in kivy Code Example how to import buttonbehaviour in kivy Code Example
websites to cure boredom Code Example websites to cure boredom Code Example
why red blood cell change their shape in anemia Code Example why red blood cell change their shape in anemia Code Example
finally block does not complete normallyJava(536871096) Code Example finally block does not complete normallyJava(536871096) Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7