Help me with Pascal

I want to use the mod and div functions properly in pascal for a college project
Can anyone help?
LVL 4
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
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

0
Commented:
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

0
Commented:
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."
0
Author Commented:
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
0
Commented:
This is basically a currency conversion problem. You know what you are going to do:
- change between pennies to notes and notes to pennies
But to change from one form to another, you need to know how to convert it. At this point, programming is not needed yet. Consider how you will convert money in pennies to notes in real life. As you had already known, to convert money in pennies to notes:
1500 pennies = 1 £10 pound note and one £5 pound note
From this, we get the following information:
£10 pound note = 1000 pennies
£5 pound note   = 500 pennies
Since this is the information we have, we will assume that only 2 types of notes are available, the £10 pound note and £5 pound note.
Basically we want to have as little notes as possible, so that we don't have to handle so many notes. Hence, we want to use the £10 pound note when we can represent the same amount of pennies with £10 pound note and £5 pound notes. So we will find out the maximum number of £10 pound notes we can use to represent as much of the pennies as possible. Then we will represent rest of the pennies with £5 pound notes. However, the £5 notes may not represent all of the rest of the pennies. So we will leave the rest of the pennies as it is.

Here is what we have to do:
- Use as much £10 notes as possible to represent as much of the pennies
- Use as much £5 notes as possible to represent as much of the remainder of pennies
- Keep the rest of the pennies as it is

By simple arithmetic, we know that to maximize the number of £10 notes used, we divide the number of pennies by 1000 and round down. In Pascal, we use / and div for division purposes. The / is the same as what we usually do in arithmetic, that is it will give the decimals. The div however, does the division only until the ones' place. Hence you can say that div, is the normal division followed by rounding down the result. We use it as how we normally use a division in arithmetic:
c:= a div b
with the exception that we have to assign the result to a variable.
Hence to find the number of £10 notes needed:

tenpound_num := numofpennies div 1000;

The modulo function complements the div function, it gives the remainder when you divide by a number, like what you do in normal arithmetic.
Hence to find the number of pennies left:

numofpennies := numofpennies mod 1000;

Do not be confuse by this statement, the 2nd numofpennies is the original value, we take the remainder of this value divided by 1000 and store it in numofpennies. Numofpennies is updated at the last step.

Next we will convert the remainder of the pennies into £5 notes using the same method:

fivepound_num := numofpennies div 500;
numofpennies := numofpennies mod 500;

Hence we have tenpound_num of £10 notes, fivepound_num of £5 notes and numofpennies of pennies.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Cheers Pear49
How do I convert it back?
0
Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Pascal

From novice to tech pro — start learning today.