-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.c
52 lines (39 loc) · 1 KB
/
Test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <math.h>
void reduce(int* whole,int* num, int* denom){
int num1=*num,num2=*denom;
//Get whole number
if(num1>num2){
*whole=num1/num2;
*num%=num2;
}
//Get the GCF
num1=*num;
while(num1!=num2){
if(num1 > num2)
num1 -= num2;
else
num2 -= num1;
}
//Divide numerator and denominator by GCF
*num/=num1;
*denom/=num1;
}
int main() {
double number,left;
int denom = 10000,num,whole;
//Input decimal value to convert
printf("\nEnter decimal to convert: ");
scanf("%lf",&number);
//Get the left value of decimal
left = (number - floor(number)) * denom;
//Get numerator
num = (int)floor(number) * denom + (int)(left);
//Get reduce numerator and get equivalent denominator
for(;num%10==0;num/=10,denom/=10){}
//Reduce to lowest term
reduce(&whole,&num,&denom);
//Printing Result
printf("\nFraction : %d %d/%d", whole, num, denom);
return 0;
}