Pascal Program Calculate The Sum and Average of Ten Positive Numbers

This pascal program calculates the sum and average of only ten positive numbers. This means that negative numbers will be excluded and the numbers will not exceed ten. This is how the pascal program will be using the IF-Then-Else statement.


Program Arithmetic Operation (Input, Output);  
Uses Crt;  
Label 5;  
Var Sum, Ave, Number: Real;   
    i: integer;   

Begin  
Sum:=0;
5:   Writeln ('Enter a number');            
Readln (number);            
If Number<0 then goto 5            
Else            
Sum:=Sum+Number;            
i:=i +1;            
IF i<10 then goto 5;             
AVE:= sum/10            
Writeln ('sum=', sum);            
Writeln ('Average=' , Ave);            
Readln; 
End. 


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



Operation of the Pascal Program


  • Line 1 'program' is a keyword which specifies the name of the program, in this case it is Arithmetic operation.

  • The 'label 5' declares a goto statement as a global identifier. A global identifier is an identifier that is specified at the beginning of the program and can be used anywhere in the program.

  • The next line 'var' is used to begin the section for variables and declare our variables. In the program above, they are 'sum, ave, number'. 'i' is also declared because it will serve as a counter or loop control variable 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.

  • The next line '5' uses the goto statement that was declared in the program declaration segment as label.


  •  'Writeln' issues a command to the system to print on the screen 'enter a number'. This lets the user know that they're expected to enter value at this point. 

  • 'ReadLn' prompts the pascal compiler to read the value entered by the user. The value stored will be used to set make a test. 
  • A test is made using the IF statement. Here, the user must enter a value or number that atleast 1 for the program to proceed or the user is required to enter a new value. The goto statement tells the compiler to return to line 5 if the number the user entered was negative i.e 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.e (i+1) such that we will expect the second number to be entered till the tenth number . 
  • The last 'Writeln' prints the answer to our program on the screen. 
  •  The program is terminated with  an 'end.'keyword.


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