percent to fraction

can anyone help me in this i want to convert percent to fraction

like 75% = 75/100 = 3/4

haha anyway anything that a user wants to supply cconverts to fraction to the lowest term ...


  • There may be an easier way to do it, but one way you could try to attempt this may to do something such as this:

    Num1 := percentage;
    Num2 := 100;

    For count := 2 to 25
    if (num1MODcount = 0) and (num2MODcount) = 0
    writeln(num1DIVcount, '/', num2DIVcount);

    I just came up with that off the top of my head, and so needs some work, because if there are two numbers that divide into both, you will get two answers.
    But then it wouldn't be much fun if I done all the work for you now, would it!? :)
  • Here is the algorithm in pseudocode:
    Read x
    Denom = 1
    while frac(x) > 0.0 do
    x := 10.0 * x
    Denom := 10 * Denom
    Numer := Trunc(x)
    Devisor := Gcd(Numer, Denom) { use Euclid's algorithm to find Gcd }
    Numer := Numer DIV Devisor
    Denom := Denom DIV Devisor
    WriteLn (Numer:0, '/', Denom:0)
    There is a potential bug in the algorithm which arises from the way a computer stores a real number. It is possible that frac(x) will always be greater than zero no matter how many times you multiply x by 10.0 and the loop will run until something overflows. You need to do something like this.
    PRECISION = 0.00001
    Read x
    Denom = 1
    while frac(x) > PRECISION * x


    Of course you also have to write the function Gcd : Integer. If you are not familiar with Euclid's algorithm, Google it.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!