Pascal: Working with fractures

I was really disappointed of the lack of information about Pascal. I know that it is not very popular language now and there are many better languages around us. However, I do believe that many people need information and examples of Pascal tasks. Moreover, there are still some schools where students learn this language as their first programming experience.

Today I will try to introduce you with some fractures and how to divide them properly in Pascal.So the main point is to add some fractures and then get integer part and a new adjusted fracture.

We will try to write a procedure which will add to fractures.

procedure add_two(var a, b:integer; c, d:integer);
  var numerator, denominator:integer;
    numerator := a * d + c * b;
    denominator := b * d;
    a := numerator;
    b := denominator;

The second procedure which will we use is going to get the integer part of whole number and make nominator and denominator as small as we can get.

procedure exclude(var a, b, int_part:integer);
  var gcd:integer;      // GCD value
    gcd := greatest_common_divider(abs(a), abs(b));
    a := a div db;
    b := b div db;
    int_part := (a div b);
    a := a - (int_part * b);

In this example we are using and algorithm to find the greatest common divider which code you can found at my last entry on this blog.

And now we just need to make everything to one working and quite useful Pascal program which code you can see here or download from here.

WriteLn('How many fractures we will have');
WriteLn('Input fractures: ');
ReadLn(a, b);
for i := 2 to n do
    ReadLn(c, d);
    add_two(a, b, c, d);
exclude(a, b, int_part);
if int_part <> 0
  then WriteLn('Result: ', int_part,' ', a, '/', b)
  else WriteLn('Result: ', a, '/', b);

Leave a comment

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. Cookies are only used for statistical purposes.