Advertisement
nguyenhappy92

Rút gọn phân số

Oct 23rd, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. // rut gon phan so
  2. // Khai bao cac ham thu vien neu co
  3.  
  4. #include<stdio.h>
  5. #include<conio.h>
  6. #include<math.h>
  7. int ucln(int a,int b);
  8. void main()
  9. {
  10. // tu so va mau so deu khac 0
  11. int tuso,mauso;
  12. scanf("%d%d",&tuso,&mauso);
  13. int x=abs(tuso);
  14. int y=abs(mauso);
  15. int t=ucln(x,y);
  16. if(mauso<0)
  17. {
  18. printf("%d / %d\n", (-tuso/t),(y/t));
  19. }
  20. else
  21. {
  22. if((y/t)==1)
  23. {
  24. printf("%d\n",(tuso/t));
  25. }
  26. else
  27. {
  28. printf("%d / %d\n", (tuso/t),(y/t));
  29. }
  30. }
  31.  
  32.  
  33. }
  34. int ucln(int a,int b)
  35. {
  36. while(a!=b)
  37. {
  38. if(a>b)
  39. {
  40. a=a-b;
  41. }
  42. else
  43. {
  44. b=b-a;
  45. }
  46. }
  47. return a;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement