Pascal Program to Find The Average Of Only Positive Numbers Using While Do loop

Given below, is a Pascal Program to find the average of only positive numbers using the WHILE DO looping statement.

Keep in mind that a WHILE DO statement continues the execution of that statement as long as the condition is true, and terminates when the condition is false.

The WHILE DO program structure or syntax in Pascal is usually given by:

WHILE  (Condition) 

DO 

Statement(s)

Hence,

You can repeat the execution of a statement or groups of statements in a subprogram till the condition is satisfied.


Logic of The Program


This program calculates the sum and average of only three positive numbers. 

This is made possible with a looping statement called WHILE DO as stated above, and it allows the user enter another number after the first until we have our three numbers.

The decision on whether positive or negative numbers are to be calculated is determined by a conditional IF-THEN statement that either proceeds on calculating the sum and average of the number if positive, or requests for a new number if negative.

This means that any negative number entered by the user is ignored and at least the first 3 positive numbers entered will be computed.


                   Pascal Arithmetic Program Using IF THEN statement
                   Pascal Program to Create and Save Records in a Textfile
                   Pascal Program of Matrix Multiplication

Program Code


Program Positive_Numbers_Only;
Label 10;
Var
Sum, Ave, N: real;
I : integer;
Begin
I:=0; sum:=0;
While i<=3 do
Begin
10: Writeln ('enter the numbers');
       Readln (N);
        If N < 0 then goto 10
          Else
          Sum:= sum + N;
          I:= i+1;
          End;
        Ave:= sum/3;
    Writeln ('the average',ave);
     Readln;
     End.


Conclusion


Pascal Program codes are the simplest you'll have to write if you're learning to program for the first time.

If you can understand the control statements and the way they are used to solve simple arithmetic problems, then you'll have no issues writing complex programming codes, especially in complex programming languages.

If you have questions regarding this Pascal code example, you can leave a comment below and we'll respond as soon as we can.

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