Horje
simplify fractions C Code Example
simplify fractions 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
windows defender not working in windows 11 Code Example windows defender not working in windows 11 Code Example
check if ahci is enabled windows 10 Code Example check if ahci is enabled windows 10 Code Example
runner up score hackerrank Code Example runner up score hackerrank Code Example
next buton using selenium Code Example next buton using selenium Code Example
allen Code Example allen Code Example

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