Pages

Sunday, November 15, 2015

Pascal Arithmetic Program Using IF THEN statement

Pascal program using control statements. Each section performs a different basic mathematical arithmetic.

YOU MIGHT ALSO LIKE: Pascal Program To Create And Save Records In A Textfile
Program Basic_Arithmetic; 
Uses crt;
Label 20;
Var A, B, Sum, Subtract, Divide, Multiply, Average: Real;
Choice : Integer;
YN : Char;


Begin
20 : Clrscr;
Writeln (' For addition, press 1');
Writeln (' For subtraction, press 2');
Writeln (' For Divison, press 3');
Writeln (' For multiplication, press 4');
Writeln ('To calculate Average, press 5');
Writeln ('To exit, Press 6');
Writeln ('Enter a number of your choice');
Choice := Readkey;


If choice = '1' then
Begin
Writeln (' Enter your first number');
Readln (A);
Writeln ('Enter your second number');
Readln (B);
Sum := A + B;
Writeln (' The answer is', Sum);
Goto 20;
End;


If choice = '2' then
Writeln ('Enter your first number');
Readln (A);
Writeln ('Enter your second number');
Readln (B);
Subtract := A - B ;
Writeln ('The answer is', Subtract);
Readln;
Goto 20;
End;


If choice = '3' then
Writeln ('Enter your first number');
Readln (A);
Writeln ('Enter your second number');
Readln (B);
Divide := A / B;
Writeln ('The answer is', divide);
Readln;
Goto 20;
End;


If choice = '4' then
Writeln ('Enter your first number');
Readln (A);
Writeln ('Enter your second number');
Readln (B);
Multiply := A * B
Writeln ('The answer is', multiply);
Readln;
Goto 20;
End;

If choice = '5' then
Writeln ('Enter your first number');
Readln (A);
Writeln ('Enter your second number');
Readln (B);
Sum := A + B;
Average := Sum / 2 ;
Writeln ('The answer is:', Average);
Readln;
Goto 20;
End;


If choice = '6' then
Writeln (' You are about exiting this program, to continue press Y/N');
YN := Readkey;
If YN = 'Y' then
Writeln (' Goodbye');
Halt;
If YN = 'N' then
goto 20;
end;
End.



If you have more numbers you can use a looping statement which will save time instead of the If then statement.