Pascal Program: 10 Methods to Add Two Numbers
Pascal Program: 10 Methods to Add Two Numbers
In this article, we will discuss the concept of the Pascal Program: 10 Methods to Add Two Numbers
In this post, we are going to learn how to find the sum of two numbers using Pascal Program: 10 Methods to Add Two Numbers.
10 Methods to Add Two Numbers
Simple Addition Using + operator
In this program, we are using the normal Addition (+) operator with a variable to subtract two numbers in pascal language
Program 1
program AddTwoNum;
var
num1,num2,sum:Integer;
begin
writeln ('Simple Addition Using "+" operator');
num1:=10;
num2:=20;
sum:=num1+num2;
writeln ('Sum=',sum);
end.
When the above code is executed, it produces the following result
Sum=30
Add two numbers using a function
In this program, we are using the user defined function to add two numbers in pascal language
Program 2
//add two numbets using function
program AddTwoNumUsingFunction;
function Add(x,y:Integer):Integer;
begin
writeln ('Add two numbers using function');
add:=x+y;
end;
var
num1,num2,sum:Integer;
begin
num1:=500;
num2:=180;
sum:=add(num1,num2);
writeln('sum= ',sum);
end.
When the above code is executed, it produces the following result
sum= 680
Add two numbers using procedure
In this program, we are using the Procedure to add two numbers in pascal language
Program 3
//add two numbets using Proceedure
program AddTwoNumWithProcedure;
procedure Add(x,y:Integer; var result:Integer);
begin
writeln ('Add two numbers using Procedure');
result:=x+y;
end;
var
num1,num2,sum:Integer;
begin
num1:=20;
num2:=18;
Add(num1,num2,sum);
writeln('Sum= ',sum);
end.
When the above code is executed, it produces the following result
Sum= 38
Add two numbers using Array
In this program, we are using the Array to add two numbers in pascal language
Program 4
//add two numbets using Proceedure
program AddTwoNumArrays;
var
nums:array[1..2] of Integer;
sum:Integer;
begin
nums[1] := 40;
nums[2] := 60;
sum := nums[1] + nums[2];
writeln('Sum= ',sum);
end.
When the above code is executed, it produces the following result
Sum= 100
Add two numbers using Loop
In this program, we are using the for loop to add two numbers in pascal language
Program 5
//Add two numbets Using Loop
program AddTwoNumUsingLoop;
var
num1,num2,result,i : integer;
begin
writeln ('Add two numbers using loop');
num1:=200;
num2:=180;
result:=num1;
for i:=1 to num2 do
result := result +1;
writeln('Addition = ',result);
end.
When the above code is executed, it produces the following result
Add two numbers using loop Addition = 380
Add two numbers using while loop
In this program, we are using the while loop to add two numbers in pascal language
Program 6
//Add two numbers using while
program AddTwoNumUsingWhile;
var
i,sum:Integer;
nums: array[1..2] of Integer;
begin
writeln ('Add two numbers using While');
nums[1]:=12;
nums[2]:=13;
sum:=0;
i:=1;
while i<=2 do
begin
sum := sum + nums[i];
i:=i+1;
end;
writeln('Sum:= ',sum);
end.
When the above code is executed, it produces the following result
Add two numbers using While Sum:= 25
Add two numbers using user input
In this program, we are using the normal Addition (+) operator with user-input to subtract two numbers in pascal language
Program 7
//Add two numbers using user input
program Add_Two_Num_Input;
var num1,num2,sum:Integer;
begin
writeln ('enter two number: ');
readln(num1,num2);
sum:=num1+num2;
writeln('Sum=', sum);
end.
When the above code is executed, it produces the following result
34 43 Sum=77
Add two numbers using repeat..until
In this program, we are using the repeat..until to add two numbers in pascal language
Program 8
//Add two numbers using repeat...until loop
program Add_Two_Num_Repeat;
var
i,sum:Integer;
nums: array[1..2] of Integer;
begin
writeln ('Add two numbers using repeat...until');
nums[1]:=12;
nums[2]:=13;
sum:=0;
i:=1;
repeat
sum:=sum+nums[i];
i:=i+1;
until i>2;
writeln('Sum=', sum);
end.
When the above code is executed, it produces the following result
Add two numbers using repeat...until Sum=25
Add two numbers using pointer
In this program, we are using the pointer to add two numbers in pascal language
Program 9
//Add two numbers using pointer
program Add_Two_Num_Pointer;
var
num1,num2,sum:Integer;
pa,pb:^Integer;
begin
num1:=13;
num2:=27;
pa:=@num1;
pb:=@num2;
sum:=pa^ +pb^;
writeln ('Sum = ',sum)
end.
When the above code is executed, it produces the following result
Sum = 40
Add two numbers using Inline Expression in writeln
In this program, we are using the Inline Expression in writeln to add two numbers in pascal language
Program 10
//Add two numbers using Inline Expression in Writeln
program Add_Two_Num_Inline;
var
num1,num2:Integer;
begin
num1:=130;
num2:=270;
writeln ('Sum = ',num1+num2)
end.
When the above code is executed, it produces the following result
Sum = 400
These are 10 different ways of writing pascal programs to add two numbers.
Similar post
Java program to add two numbers – 10 ways
Java program to subtract two numbers – 20 ways
Java language addition of two numbers – 5 ways
Java Code- Addition of Two Numbers using 5 Ways
Java Code- Division of Two Numbers using 5 Ways