-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha2_fraction.c
More file actions
50 lines (37 loc) · 1.19 KB
/
Copy patha2_fraction.c
File metadata and controls
50 lines (37 loc) · 1.19 KB
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
// Name: Hasin Shadab Rahman
// Net Id : hasinrahman
// Description: a program that asks the user to enter a fraction, then reduces the fraction to lowest terms
#include <stdio.h>
int main(){
int i, a, b;
while(1){
// prompts the user for fraction
printf("Enter a fraction (enter 0 to stop): ");
scanf("%d/%d", &a, &b);
if(a == 0 || b== 0){
break;
}
// if the remainder is 0 then it will the absolute of a/b
if((a%b)==0){
printf("In lowest terms: %d\n",a/b);
continue;
}
int sign = (a * b) / (abs(a*b)); // Determine the sign of the fraction
a = abs(a); // Make numerator positive
b = abs(b); // Make denominator positive
int originalA = a;
int originalB = b;
// Simplify the fraction
while (a != 0 && b != 0) {
if (a > b) {
a %= b;
} else {
b %= a;
}
}
int gcd = a + b; // The remaining value is the GCD
a = originalA / gcd;
b = originalB / gcd;
printf("In lowest terms: %d/%d\n", sign * a, b);
}
return 0; }