-
Notifications
You must be signed in to change notification settings - Fork 76
/
rat.c
60 lines (46 loc) · 1.08 KB
/
rat.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
53
54
55
56
57
58
59
//write a code to write rational numbers
//check whether they have anything in common and represent the rational numbers as having no common factor
#include<stdio.h>
#include<stdlib.h>
int Rational(int x,int y);
int main(){
int num, den;
printf("Please enter the numerator: ");
scanf("%d", &num);
printf("Please enter the denominator: ");
scanf("%d", &den);
printf("\n===================================\n\n");
Rational(num, den);
printf("\n===================================\n\n");
return 0;
}
int Rational(int x, int y){
int rem, a, fn, fd;
fn=x;
fd=y;
if(y==0){
printf("Error: Denominator can't be zero\n");
printf("\n===================================\n\n");
exit(0);
}
if(x==0){
printf("The fraction is 0/%d = 0\n",y);
printf("\n===================================\n\n");
exit(1);
}
rem = x % y;
if (rem==0){
x=x/y;
y=y/y;
}
else
a = y%rem;
if (a==0){
y=y/rem;
x=x/rem;
}
printf("The fraction is %d/%d = %d/%d", fn,fd,x,y);
//printf("\nnum = %d,den = %d", x,y);
printf("\n");
return(x,y);
}