Pascal Code to Add Two Numbers in 10 Ways with User Input
Pascal Code to Add Two Numbers in 10 Ways with User Input
In this article, we will discuss the concept of the Pascal Code to Add Two Numbers in 10 Ways with User Input
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 with user input
In this program, we are using the normal Addition (+) operator with a variable to subtract two numbers in pascal language
Program 1
program AddTwoNumUserInput;
var
num_1,num_2,sumNum:Integer;
begin
writeln ('Simple Addition Using "+" operator');
writeln ('Enter the first number');
readln(num_1);
writeln ('Enter the second number');
readln(num_2);
sumNum:=num_1+num_2;
writeln ('Sum=',sumNum);
end.
When the above code is executed, it produces the following result
Simple Addition Using "+" operator Enter the first number 25 Enter the second number 35 Sum=60
Addition – Using Function with user input
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
num_1,num_2,sumNum:Integer;
begin
writeln ('Enter first number');
readln(num_1);
writeln ('Enter second number');
readln(num_2);
sumNum:=add(num_1,num_2);
writeln('sum= ',sumNum);
end.
When the above code is executed, it produces the following result
Enter first number 35 Enter second number 45 Add two numbers using function sum= 80
Addition – Using Procedure with user input
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
writeln ('Enter the first number');
readln(num1);
writeln ('Enter the second number');
readln(num2);
Add(num1,num2,sum);
writeln('Sum= ',sum);
end.
When the above code is executed, it produces the following result
Enter the first number 12 Enter the second number 23 Add two numbers using Procedure Sum= 35
Addition- using Array with user input
In this program, we are using the array to add two numbers in pascal language
Program 4
//add two numbers using Array with user input
program AddTwoNumArrayInput;
var
nums:array[1..2] of Integer;
sum,i:Integer;
begin
writeln('Enter two numbers ');
for i:=1 to 2 do
begin
write('Number', i , ': ');
readln(nums[i]);
end;
sum := nums[1] + nums[2];
writeln('Sum of = ',nums[1], ' and ' , nums[2], '=',sum);
end.
When the above code is executed, it produces the following result
Enter two numbers Number1: 20 Number2: 30 Sum of = 20 and 30=50
Addition- using loops with user input
In this program, we are using the for loop with user input 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');
writeln ('Enter first Number');
readln(num1);
writeln ('Enter second Number');
readln(num2);
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 Enter first Number 34 Enter second Number 43 Addition = 77
Addition- using While loop with user input
In this program, we are using the while loop with user input to add two numbers in pascal language
Program 6
//Add two numbers using while with user inpiut
program AddTwoNumUsingWhile;
var
i,sum:Integer;
nums: array[1..2] of Integer;
begin
writeln ('Add two numbers using While');
writeln ('Enter two numbers');
i:=1;
while i<=2 do
begin
write('Number ',i, ':');
readln(nums[i]);
i:=i+1;
end;
sum:=0;
i:=1;
while i<=2 do
begin
sum := sum + nums[i];
i:=i+1;
end;
writeln('Sum of ',nums[1], ' and ', nums[2], ' = ',sum);
end.
When the above code is executed, it produces the following result
Enter two numbers Number 1:54 Number 2:46 Sum of 54 and 46 = 100
Addition-using Repeat …. until loop with user input
In this program, we are using the
Repeat …. until loop with user input to add two numbers in pascal language
Program 7
//Add two numbers using repeat...until loop
program Add_Two_Num_Repeat;
var
num1,num2,sum:Integer;
i:Integer;
begin
i:=1;
//writeln ('Add two numbers using repeat...until');
repeat
writeln ('enter first number: ');
readln(num1);
writeln ('enter second number: ');
readln(num2);
sum:=num1+num2;
writeln ('The sum of ',num1, 'and ', num2, ' is: ', sum);
i:=i+1;
until i>1;
writeln('program ended');
end.
When the above code is executed, it produces the following result
enter first number: 32 enter second number: 33 The sum of 32and 33 is: 65 program ended
Addition- using recursion with user input
In this program, we are using the recursive function with user input to add two numbers in pascal language
Program 8
//Add two nunbers using recursion in Pascal
program AddTwoNum_Recursion;
function add(a,b:Integer):Integer;
begin
if b=0 then
add := a
else
add:=add(a+1,b-1);
end;
var
num1,num2,result:Integer;
begin
writeln ('Enter first number:');
readln(num1);
writeln ('Enter second number:');
readln(num2);
result:=add(num1,num2);
writeln ('Sum=:',result);
end.
When the above code is executed, it produces the following result
Enter first number: 35 Enter second number: 65 Sum=:100
Addition-using pointer with user input
In this program, we are using the pointer with user input to add two numbers in pascal language
Program 9
//Add two numbers using pointer with user input
program AddTwoNumbers_Pointer;
var
num1,num2,sum:Integer;
p1,p2:^Integer;
begin
new(p1);
new(p2);
writeln ('enter first number: ');
readln(num1);
writeln ('enter second number: ');
readln(num2);
p1^:=num1;
p2^:=num2;
sum:=p1^ +p2^;
writeln ('Sum = ',sum);
dispose(p1);
dispose(p2);
end.
When the above code is executed, it produces the following result
enter first number: 54 enter second number: 66 Sum = 120
Addition- using Built-in succ() Function
In this program, we are using the Built-in succ() Function with user input to add two numbers in pascal language
Program 10
//Add two numbers using Built-in succ() Function
//(succ(x) returns the successor of x, i.e., x+1)
program AddTwoNumbers_Succ;
var
a,b,i,sum:Integer;
begin
writeln ('enter first number: ');
readln(a);
writeln ('enter second number: ');
readln(b);
sum:=a;
for i:=1 to b do
sum:=succ(sum);
writeln ('Sum = ',sum)
end.
When the above code is executed, it produces the following result
enter first number: 55 enter second number: 25 Sum = 80
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