Reducing Fractions in C

This is a discussion on Reducing Fractions in C within the C Programming forums, part of the General Programming Boards category; I have a code here that takes in a numerator and a denominator value, and then outputs the result in ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    16

    Reducing Fractions in C

    I have a code here that takes in a numerator and a denominator value, and then outputs the result in a fraction form if needed.

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    int main(int argc, char *argv[])
    {
      int a,b,ans,left;
    
    
      printf("Numerator\n");
      scanf("%d", &a);
      printf("Denominator\n");
      scanf("%d", &b);
         
      ans = a / b;
      left = a%b;
      
      if (left > 0){
               printf("\n%d %d/%d\n", ans, left, b);
    }
      else{ 
            printf("\n%d\n", ans);
    
    
    }
      system("PAUSE");	
      return 0;
    }
    It also needs to reduce the output. I don't know how to reduce my output and not sure how. Ex:

    Numerator: 55
    Denominator: 10

    My ans: 5 5/10

    Anyone know how to reduce the 5/10 in my program?

  2. #2
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You need to find the gcd (greatest common divisor) of the numerator and denominator, then divide them both by it. Euclid's algorithm is commonly used for finding the gcd. Google it.

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    16
    @ javaeyes

    I know how the modulus operator works, that is why i used it in line 15 of my program for the numerator of the mixed fraction.

    @ oogabooga

    I did read Euclid's algorithm, but I'm just stumped on how it works. Yes I am not the smartest :/

    Anyone else have any ideas?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    Quote Originally Posted by Zexious
    I did read Euclid's algorithm, but I'm just stumped on how it works.
    What stumped you about the algorithm? Note that you only need to know how it works so that you can implement it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    OOps, right didnt see that. I think you may need to reduce the numerator and denominator to their prime factorization. 20 / 30 is a simple example.
    20's prime factorization is 2^2 * 5^1 . 30's prime factorization is 2^1 * 3^1 * 5^1. when you start canceling terms (basically subtracting the exponents)
    you're left with 2^1 / 3^1 or 2/3.
    Now that I just read euclids alg, that is actually probably the way to go. This would work too though.

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The only way to reduce a fraction to lowest terms is to divide top and bottom by their greatest common divisor (or cancel common factors, but that amounts to the same thing). Euclid's algorithm is the simplest way to find their gcd, so it's your best bet.

    This is the pseudocode from the wikipedia article on Euclid's algorithm:
    Code:
    function gcd(a, b)
        while b ≠ 0
           t := b
           b := a mod b
           a := t
        return a
    Can you translate that into C?

  8. #8
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59
    Implemented GCD and added it to your program,to reduce the fraction.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int gcd(int,int);
    #define TRUE 1
    #define FALSE 0
    
    
    int main(int argc, char *argv[])
    {
      int a,b,ans,left,g,n,d;
    
    
    
    
      printf("Numerator\n");
      scanf("%d", &a);
      printf("Denominator\n");
      scanf("%d", &b);
    
    
    
    
      ans = a / b;
      left = a%b;
      g=gcd(left,b);
    
    
        if(left==0)
         printf("\n%d\n", ans);
    
    
       else if (g==1 && left > 0)
      {
               printf("\n%d %d/%d\n", ans, left, b);
      }
      else
      {
             left=left/g;
              b=b/g;
               printf("\n%d %d/%d\n", ans, left, b);
      }
    
    
    
    
    
    
      system("PAUSE");
      return 0;
    }
    int gcd(int n,int d)
    {
        int r; //remainder
    
    
        while((n%d!=0) || (d<=0))
    
    
        {
            r=n%d;
            n=d;
            d=r;
        }
         if(n%d==0)
         {
         return d;
         }
         else
         return FALSE;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reducing eye strain
    By bernt in forum General Discussions
    Replies: 6
    Last Post: 06-03-2010, 06:27 PM
  2. zero reducing vocals ?
    By geek@02 in forum Tech Board
    Replies: 5
    Last Post: 04-25-2008, 11:50 PM
  3. Reducing my code
    By limitmaster in forum C++ Programming
    Replies: 7
    Last Post: 01-09-2008, 07:05 AM
  4. Reducing loops
    By Roaring_Tiger in forum C Programming
    Replies: 4
    Last Post: 03-11-2003, 08:17 PM
  5. Reducing Fractions/Entering fraction form
    By csmatheng in forum C Programming
    Replies: 4
    Last Post: 09-08-2002, 03:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21