Link to home
Start Free TrialLog in
Avatar of dreamvb
dreamvb

asked on

Help me with Pascal

I want to use the mod and div functions properly in pascal for a college project
Can anyone help?
Avatar of Batalf
Batalf
Flag of United States of America image

Hi

What exactly do you need help to?

Mod
var
  a,b : integer;

begin
   a:=123;
   b:=a mod 50;
   (** gives you B = 23 = the
   rest when 123 are divided by 50
   50 * 2 = 100 + rest(23) = 123*)

   b:=a div 50;
   (**gives you b=2 = what whole
   number you could divide 123 by(the dividend).
   50 = 123 : 50 = 2,.... everything
   behind the comma are taken away

   then we can say that
   
   (a mod 50) + (a div 50)*50 = 123 **)

Regards
Batalf
   
Hi

What exactly do you need help to?

Mod
var
  a,b : integer;

begin
   a:=123;
   b:=a mod 50;
   (** gives you B = 23 = the
   rest when 123 are divided by 50
   50 * 2 = 100 + rest(23) = 123*)

   b:=a div 50;
   (**gives you b=2 = what whole
   number you could divide 123 by(the dividend).
   50 = 123 : 50 = 2,.... everything
   behind the comma are taken away

   then we can say that
   
   (a mod 50) + (a div 50)*50 = 123 **)

Regards
Batalf
   
Copy of the help for these operators :

"Arithmetic operators, which take real or integer operands, include +, –, *, /, div, and mod.

Operator      Operation      Operand types      Result type      Example
+      addition      integer, real      integer, real      X + Y
–      subtraction      integer, real      integer, real      Result - 1
*      multiplication      integer, real      integer, real      P * InterestRate
/      real division      integer, real      real      X / 2
div      integer division      integer      integer      Total div UnitSize
mod      remainder      integer      integer      Y mod 6
Operator      Operation      Operand type      Result type      Example
+ (unary)      sign identity      integer, real      integer, real      +7
– (unary)      sign negation      integer, real      integer, real      -X
The following rules apply to arithmetic operators.

The value of x/y is of type Extended, regardless of the types of x and y. For other arithmetic operators, the result is of type Extended whenever at least one operand is a real; otherwise, the result is of type Int64 when at least one operand is of type Int64; otherwise, the result is of type Integer. If an operand’s type is a subrange of an integer type, it is treated as if it were of the integer type.

The value of x div y is the value of x/y rounded in the direction of zero to the nearest integer.
      The mod operator returns the remainder obtained by dividing its operands. In other words, x mod y = x – (x div y) * y.
      A runtime error occurs when y is zero in an expression of the form x/y, x div y, or x mod y."
Avatar of dreamvb
dreamvb

ASKER

Thanks Batalf
I need it to change money in pennies to notes and change
ie 1500 pennies into 1 £10 pound note and one £5 pound note
ASKER CERTIFIED SOLUTION
Avatar of pear49
pear49

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dreamvb

ASKER

Cheers Pear49
How do I convert it back?
Similarly, converting it back is a reverse of what we have done. All we have to know is the value of a £10 note and £5 note in terms of pennies.
So we know,

£10 = 1000 pennies
£5   = 500 pennies

So if we do not want to use notes, we have to use the equivalent amount of pennies to represent the same amount of money. Hence to find out how many pennies needed for then £10 notes, we multiply tenpound_num by 1000. Then we add it to the number of pennies we have:

numofpennies := numofpennies + tenpound_num * 1000;

Notice we add it to numofpennies, not put it in the variable numofpennies. This is so because numofpennies contains the remainder of the pennies that are not converted to notes, hence may not be zero.

Similarly, we convert the £5 notes to pennies:

numofpennies := numofpennies + fivepound_num * 500;

Now we will have 0 £10 notes, 0 £5 notes and numofpennies of pennies.

Cheers dreamvb