#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);
}
Code Example |
---|
C :: close file in c |
C :: how to print int in c |
C :: lerp function c |
C :: Write a C program to find reverse of an array |
C :: is 33 prime number |
C :: print 2d array in c |
C :: arduino digital read |
C :: random in c |
C :: fast inverse square root explained |
C :: c concatenate strings |
C :: c iterate string |
C :: va_list in c |
C :: selection sort in c |
C :: differnce between spooling and buffering |
C :: c program strtok use |
C :: for loop in c |
C :: PATH_MAX |
C :: read a document from console in c |
C :: c substring |
C :: geom boxplot remove outliers |
C :: print variable adress c |
C :: memory layout in c |
C :: syntax |
C :: addition of matrix |
C :: function array median |
C :: ecrire programme en C une fonction remplir tableau et un fonction inverser |
C :: getchar c |
C :: how to use pointer in c to print char |
C :: script in c |
C :: size of float in c |