Pages

Showing posts with label Pascal Programming. Show all posts
Showing posts with label Pascal Programming. Show all posts

Thursday, November 19, 2015

Pascal Program For Matrix Multiplication

Pascal program to multiply arrays A[i , j] and B[i, j] . Each element of the resulting matrix is multiplied by 1/4

Program begn;
Var
A, B, C, D : Array [1..2,1..2] of real;
I, j: integer;

Begin
  For i:= 1 to 2 do
    For j:= 1 to 2 do
      Begin
      Readln (A[i, j]);
      End;
  
  
Begin
  For i:= 1 to 2 do
    For j:= 1 to 2 do
      Readln (B[i, j]);
      End;
  
  
Begin
  For i:= 1 to 2 do
    For j:= 1 to 2 do
      C[i, j]:= A[i, j] * B[i, j];
      End;
  

Begin
  For i:= 1 to 2 do
    For j:= 1 to 2 do
      D[i, j]:= 1/4 * C[i, j];
      End;
  
  
Begin
  For i:= 1 to 2 do
    For j:= 1 to 2 do
      Writeln (D[i, j]);
      Readln;
      End;
End.
  
  
  

Read more »

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.
Read more »

Saturday, November 14, 2015

Pascal Program to Read in Even Numbers Into An Array

This program takes in only even numbers into an array of two rows and two columns. Here, a loop and conditional statement is used.

Program Eve;
Label 10;
Var
A : array[1..2,1..2] of integer;
I, j: integer;
Begin
For I:= 1 to 2 do
   For j:=  1 to 2 do
    Begin
10: readln (A[I , j]);
   If A[I , j] mod 2  < > 0 then goto 10
   Else
   End;
Begin
For I:= 1 to 2 do
For j:= 1 to 2 do
Writeln (A[I , j] );
Readln;
End.

Read more »

Thursday, November 12, 2015

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

Using the while do loop, the program calculates the sum and average of only three positive numbers. Any negative number entered by the user is ignored.

Program Positve;
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.

Read more »

Wednesday, November 11, 2015

Pascal Program to Create and Save Records in a Textfile

program Create_Records;
Type
Gra = String [30];
File_Content = Record
Name : Gra;
Matric_No : Gra;
Dept : Gra;
Sex : Gra;
Date_Of_Birth : Gra;
(*enter as many data as you like and intialise either as string, integer or char*);
End;

Var
 
       FileInfo : File_Content;
       Newfile : text;
        I : Integer;

Begin
Assign (newfile, 'Grace_Joseph.txt');
rewrite (Newfile);
      for i := 1 to 5 do
       begin
      writeln ('Enter name:');
      readln ( fileinfo.name);
       writeln ('matric number');
       readln (fileinfo.matric_No);
        writeln ('department');
       readln (fileinfo.dept);
        writeln ('sex');
        readln (fileinfo.sex);
        writeln ('date of  birth');
        readln (fileinfo.date_of_birth);
(*follow the above procedure to write and read the data you initialised*    )

writeln (newfile,' Name:', fileinfo.name)
writeln (newfile, 'matric number:', fileinfo.matric_no);
writeln (newfile,' Department: ', fileinfo.dept);
writeln (newfile,' Sex:', fileinfo.sex);
writeln (newfile,' Date Of Birth:', fileinfo.date_of_birth);
readln;
end;
close (newfile);
end.

In the program above, students records are saved in a textfile. You can rename the name of the textfile from Grace_Joseph.txt to yourDesired Name.txt. Goto the documents in your computer and search for the file using. the filename.txt.

The program creates a record for 5 students using the For Do loop.

Read more »

Pascal Program of Matrix Multiplication

program NameArray;
var
A , B, C : Array [1..5,1..5] of integer;
d, e : integer;
Begin
  for d := 1 to 5 do
   Begin
  for e := 1 to 5 do
    Begin
writeln ('input your numbers for A');
readln (A[d , e] );
end;
end;
  begin
  writeln;
   for d := 1 to 5 do
     begin
   for e := 1 to 5 do
  begin
  writeln ('enter a number for b');
  readln (B [d , e] );
  end;
  end;
  begin
for d:= 1 to 5
begin
for e:= 1 to 5
C [d , e] := C [d ,e] + A [d , e]  × B [ d, e];
end;
writeln (' The answers are');
readln;
writeln;
writeln;
for d := 1 to 5 do
begin
for e := 1 to 5 do
write ( C[ d, e], '  ' :2);
Writeln;
end;
end;
end;
readln;
End.

Read more »

Pascal Subprogram Of Fibonacci Series Using Function

The Fibonacci series is one in which the addition of two previous terms gives you the next term. That is. 0,1,2,3,5,8,13....1 + 2 is the next consecutive term which is 3, so is 3 + 5 (8)the next term.

This program uses a function subprogram to call by value.

Program Fibnacci;
Var
I : integer;
Function fibonacci (a: integer): integer;
Begin
If a=1 then
   Fibonacci :=0
Else if n=2 then
    Fibonacci :=1
Else
    Fibonacci := Fibonacci(a-1) + Fibonacci (n-2);
End;
Begin
For I:= 1 to 20 do
Writeln(Fibonacci (I));
Readln
End.

Read more »

Pascal program To Calculate Area of a Triangle

Write a pascal program that uses a function subprogram called N to calculate the area of a triangle with the formula: Area=√N(N-X)(N-Y)(N-Z) where N= x + y + z / 2

Program area_Of_triangle;
Var
x, y, z , r, area : real;
Function N (I, j, k: real): real;
Begin
N:=a + b + c / 2;
End;
Begin
Writeln (' enter values for  x, y, z');
Readln (x, y, z);
r := N(x, y, z);
Area:= sqrt( r- x)*(r-y)*(r-z);
Writeln ('Area:', area);
Readln
End.
 

The global variables have been declared at the top which is x, y, z. The function carries the local variables i, j, k which must be of the same type as the global variables (real, integer, string or any other). But in this case, the appropriate type is a real data type.

Using the formula of N in the question, this formula isassigned to N in the program.

Next the values are read in by the user which is prompted by 'enter the values for x, y, z'. These identifiers (x, y, z take the values of I, j,k) replace the local variables when the is function N is called and assigned to 'r'. The later part to the program calculates the area of the triangle.

Read more »

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.

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.
Read more »

Monday, November 9, 2015

Pascal program to find the average of numbers using WHILE DO structure

To calculate the sum and average of three different numbers. You can edit the loop and the average formula to repeat it as many times as you'd like.

Program untitled;
var
sum, ave, n : real;
i : integer;
begin
sum := 0;
i := 1;
while i <=3 do
begin
writeln ('enter your numbers');
readln (n);
sum := sum + n;
ave := sum / 3;
i := i + 1 ;
end;
writeln (ave);
readln;
end.

Read more »