#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 :: get chunks of a mp4 in ffmpeg |
C :: c how to get an integer from user input |
C :: how to get user input in c |
C :: how to convert string to integer in c |
C :: multiplication table using c |
C :: take array as input in c |
C :: arduino client disconnect |
C :: reattach screen linux |
C :: how to read space separated words in c |
C :: strcasecmp in c |
C :: add field to model rails |
C :: create empty vector in rust |
C :: copy string c |
C :: stdio.h in c |
C :: c program to find minimum of 4 numbers using conditional operator in c |
C :: c syntax |
C :: c pass int by reference |
C :: read a document in c getting name from console |
C :: arduino wifi client |
C :: C Arithmetic Operators |
C :: c programming language |
C :: simple bootstrap form example |
C :: typedef c struct |
C :: second largest element in an array |
C :: calculate median |
C :: pandoc set margins pdf |
C :: c memcpy |
C :: prime numbers |
C :: armstrong in c |
C :: unused variable in c |