Reputation: 19
i'm a newbie and I need how to compare 3 numbers in Pascal.
Here is what I tried so far
BEGIN
max:=A;
IF B>A THEN max:= B;
IF C>B THEN max:= C;
END;
but when I choose, for example, A = 5 , B=2 , C=4
, the output is 4, but it should be 5. Where is the problem?
I want at the end writeln('The Large Number is ',max);
Upvotes: 0
Views: 5574
Reputation: 1
Using the Max Function of Pascal
PROGRAM MaxProgram;
USES math;
VAR
num1,num2,num3,maxNum : INTEGER;
BEGIN
(* Receive the Values *)
WRITELN('Enter First Number');
READLN(num1);
WRITELN('Enter Second Number');
READLN(num2);
WRITELN('Enter Third Number');
READLN(num3);
(* Using the Max Function *)
maxNum := max(num1,max(num2,num3));
(* Display Result *)
writeln('The Highest number is ', MAXNUM);
END.
Upvotes: -1
Reputation: 26376
Or, in a somewhat recent Pascal, like Delphi or Free Pascal, using the max function from the MATH unit.
result:=max(a,max(b,c));
Upvotes: 0
Reputation: 1532
You must compare with max
instead of A
or B
.
Changing your code in a easy way:
BEGIN
max := A;
IF B > max
THEN
max := B;
IF C > max
THEN
max := C;
END;
Upvotes: 1
Reputation: 1679
You could do this (you should be comparing with max
)
BEGIN
max:=A;
IF B>max THEN max:= B;
IF C>max THEN max:= C;
END;
Upvotes: 4