Pascal Arithmetic Program Using IF THEN statement

This is a Pascal Arithmetic Program Using IF-THEN statement which is a Control Statement in programming.

Pascal Programming Language is one of the oldest languages like BASIC, ALGOL, FORTRAN, COBOL, and AL used in writing instructions for a computer.

It is also one of the easiest languages a beginner can program in because it is easy to learn, understand, and its less complicated to codes written in C++, Java, and the rest; but then there are limitations to what you can do with it compared to the latter languages.

Now,

With the Pascal Programming Language, the simplest mathematical operations such as Addition, Subtraction, Division of real numbers, and Average of a range of numbers can be carried out.

Also, mathematical operators can be used in any Pascal code example as is the case in this post.

That being so, let's look at the logic of  the program.

Logic of The Program


In the Pascal codes below, a section or subprogram performs a specific operation using two numbers.

The operation carried out is determined by the Number (Selection Key) entered by the user.

Here,

Our Selection Keys are 1, 2, 3, 4, 5, and 6, where on key presses, an operation must occur be it an arithmetic operation or exit from the Program.

We have used the IF-THEN Statement here, ensuring that one subprogram or the another must be executed depending on the user's select key choice.

Finally,

The program is terminated if the selection key is  6, whereas the other keys will actually compute two random numbers entered by the user.

Now if you get the logic behind this simple program, you can write more complex ones most especially if you're good with numbers.


Also Read:  BASIC Program For Sum of Numbers Using IF Then Statement
                     Pascal Program to Create and Save Records in a Textfile
                     Pascal Program of Matrix Multiplication




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.


Conclusion: 


Following the logic in the above Pascal Calculations, If you have more numbers you can use a looping statement which will save time instead of the If -Then statement.

If you have questions regarding the code above, please drop a comment and we'll respond immediately.



Do you like this post? Please link back to this article by copying one of the codes below.

URL Of Post:


HTML Link Code:

BB (forum) link code:

Comments