(Type integer, arithmetic operators, integer operators) Write a program which reads two integers and then displays a) their product and b) their quotient when the first is divided by the second.
Detailed Explanation
program ex2(input,output);
var a,b,c,d : integer;
begin
writeln ('Enter an integer');
readln (a);
writeln ('Enter another integer');
readln (b);
c:=a * b;
writeln (c);
d:= a div b;
writeln (d);
readln;
end.
We declare 4 integers, 2 for input and 2 for calculations. We read the integers from the keyboard and then use the * to multiply them and the 'div' operator to find them number of times b goes into a. For example, 3 goes into 10 3 times (remainder 1 in integer division).
Be aware from this point that you must always check a program for potential errors, not only in the code but also in what the user might do when using it. In this case, if the user enters 0 for b then a division by zero error will result. We will develop an awareness of potential sources of error as we proceed and we will also learn programming techniques to deal with them.
Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts