Advertisement
finalshare

b1

Nov 14th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. /*Function prototype*/
  6. void input(int *num, int *den); // input numerator (num) and denominator of a fraction (den)
  7. void display(int num, int den); // display the fraction
  8. void simplify(int *num, int *den); // simplify the fraction
  9.  
  10. void input(int *num, int *den)
  11. {
  12.     printf("Enter numerator: ");
  13.     scanf("%d",num);
  14.     do
  15.     {
  16.         printf("Enter denominator: ");
  17.         scanf("%d",den);
  18.     }
  19.     while( *den==0);
  20.  
  21. }
  22.  
  23. void display(int num, int den)
  24. {
  25.     printf("%d/%d",num,den);
  26. }
  27. void simplify(int *num, int *den)
  28. {
  29.     int m;
  30.     if (*num>*den) m=*num;
  31.     else m=*den;
  32.     int i;
  33.     for (i=m; i>1; i--)
  34.     {
  35.         if(*num%i==0 && *den%i==0 )
  36.         {
  37.             *num=*num/i;
  38.             *den=*den/i;
  39.         }
  40.  
  41.     }
  42.     if (*num>0 && *den<0)
  43.     {
  44.         *num*=-1;
  45.         *den*=-1;
  46.     }
  47.     printf("%d/%d\n",*num,*den);
  48.  
  49. }
  50.  
  51. int main(int argc,char * argv[])
  52. {
  53.     int n,d,*pn,*pd;
  54.     pn=&n;
  55.     pd=&d;
  56.     char c;
  57.     do
  58.     {
  59.         c=0;
  60.         printf("1. Input fraction\n\n");
  61.         input(&n,&d);
  62.         fflush(stdin);
  63.         printf("2. Simplify fraction\n\n");
  64.         display(n,d);
  65.         printf("\n = \n");
  66.         simplify(&n,&d);
  67.         while (c!='n' && c!='y')
  68.         {
  69.             printf("Another run?(y/n)  ");
  70.             c=getchar();
  71.         }
  72.  
  73.  
  74.     }
  75.     while (c=='y');
  76.  
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement