Wednesday, November 11, 2015
Pascal Program Calculate The Sum and Average of Ten Positive Numbers
Also Reead:Pascal Program of Matrix Multiplication
Program Arithmetic Operation (Input, Output);
Uses Crt;
Label 5;
Var Sum, Ave, Number: Real;
i: integer;
Begin
5: Writeln ('Enter a number');
Readln (number);
If Number<0 then goto 10
Else
Sum:=Sum+Number;
i:=i +1;
IF i<10 then goto 10;
AVE:= sum/10
Writeln ('sum=', sum);
Writeln ('Average=' , Ave);
Readln;
End.
Line 1 'program' specifies the name of the program. The 'label 5' is a goto statement and also a global identifier. A global identifier is one which is specified at the beginning of the program and can be used anywhere in the program.
The next line 'var' is used to declare our variables which are sum, ave, number before it is used in the program. 'i' is also declared because it will serve as a counter in the program. It will be used to know when the number exceeds ten. 'Begin' signifies the beginning of the program and if you notice a semi colon is not attached to it.
Also Read: C++ Program to Check If a Number Is A Perfect Number or Not
C++ Program to Find All The Roots Of A Quadratic Equation
C++ Program to Compute The Least Common Multiple of Integers
The next line '5' uses the goto statement that was already identified as a label. 'Writeln' is a command that will write a line on the user's screen asking for any random number with the question 'enter a number'. A semicolon is attached to the end showing it is the end of the statement.
'ReadLn' prompts the pascal compiler to read the above statement and identify it as 'number' which we also declared a s a variable. The first IF statement with a goto statement tells the compiler to return to line 5 if the number the user entered was negative which is less than 0. If this number was greater than 0, then the ELSE clause is implemented and the program continues by assigning 'SUM+Number' to 'SUM' which was globally declared at the beginning of the program.
The next line increments our counter by 1 (i+1) such that we will expect the second number to be entered till the tenth number as required by the question. This will be so from the next line IF the counter is less then ten, it should go back to our label 5, which is 'enter a number'. This will continue until the condition (IF i<10 then goto 5) is satisfied by we having ten numbers. It goes on to AVE and calculates the average by the formula from the program which is the total sum of these numbers divided by 10.
The 'writeln' prints the answer to our program on the screen. The answer to sum and average. The program is terminated with 'end.' having a full stop.