Pascal program to add two numbers using various ways

Pascal program to add two numbers

In this tutorial, we will discuss “Pascal program to add two numbers using various ways”

add two numbers

Pascal program to add two numbers

You want to see different ways of adding two numbers in Pascal. Let’s go step by step with a few approaches. In this article, we use 5 different ways.

Simple Addition with Variable

In this program, we are using the normal addition operator with a variable

Program 1

program AddTwoNumbers;
var 
    a,b,sum:Integer;
begin
  writeln ('Add two numbers');
  a:=5;
  b:=8;
  sum:=a+b;
  writeln('Sum= ',sum);
end.

When the above code is executed, it produces the following result

Add two numbers
Sum= 13

 

Input from user

In this program, we are using the normal addition operator with a variable, and also providing the value to the variable using user input

Program 2

program add_Num(input,output);
var     num1,num2,total:Integer;
        avg:Real;
begin
        writeln ('Welcome pascal programming');
        writeln ('Enter first number');
        read(num1);
        writeln ('Enter second number');
        read(num2);
        total:=num1+num2;
        avg:=total/2;
        writeln ('Total is:',total);
        writeln ('Average is:',avg);
        
end.

When the above code is executed, it produces the following result

Welcome pascal programming
Enter first number
5
Enter second number
6
Total is:11
Average is: 5.5

 

Using a Function

In this program, we are using the function to add two numbers

Program 3

program AddTwoNumWithFunction;
function Add(x,y:Integer):Integer;

begin
  writeln ('Add two numbers using function');
    Add:=x+y;
  end;
  var 
    a,b,result:Integer;
    begin
  a:=50;
  b:=18;
  result:=Add(a,b);
  writeln('sum= ',result);
end.

When the above code is executed, it produces the following result

Add two numbers using function
sum= 68

Using a Procedure

In this program, we are using the Procedure to add two numbers

Program 4

program AddTwoNumWithProcedure;
procedure Add(x,y:Integer; var result:Integer);

begin
  writeln ('Add two numbers using Procedure');
    result:=x+y;
  end;
  var 
    a,b,sum:Integer;
    begin
  a:=20;
  b:=18;
  Add(a,b,sum);
  writeln('Sum= ',sum);
end.

When the above code is executed, it produces the following result

Add two numbers using Procedure
Sum= 38

 

Inline Expression in writeln

In this program, we are using the Inline Expression in writeln to add two numbers

Program 5

program AddTwoNumWithInLineExpression;
begin
  writeln ('Add two numbers using Procedure');
  writeln('Sum= ',12+15);
end.

When the above code is executed, it produces the following result

Add two numbers using Procedure
Sum= 27

 

Similar post

10 ways to add two numbers in Java

Calculate addition of two numbers using method in Java language

Calculate addition of two numbers using function in C language

Calculate addition of two numbers using function in C++ language

Calculate addition of two numbers using function in Python language

 

10 simple ways to add two numbers in Java
Pascal Program: 10 Methods to Add Two Numbers
Pascal