Write a program which accepts three real numbers, computes their sum and outputs the result. Amend the program so that the result is output to 2 dp.
Detailed Explanation
program ex3(input,output);
var a, b, c, sum: real;
begin
writeln ('Enter a real number');
readln (a);
writeln ('Enter another real number');
readln (b);
writeln ('Enter one more real number');
readln (c);
sum:=a + b + c;
writeln ('Sum is: ', sum: 8: 2);
readln;
end.
We declare 3 real numbers, 3 for input and 1 for the sum. We read the numbers from the keyboard and then use the + to add them. In the output we use 8 for the width of the output and 2 for the number of decimal places.
What if the user fails to input real numbers? What if they enter integers or, worse still, characters such as 'a' or 't'? A simple read is no guarantee at all of data integrity, we need to check this at all times in a serious program. The term 'user friendly' is not very helpful, what we need is something robust, which works in all circumstances. But can we foresee all circumstances?
Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts