Pascal Code for Subtracting Two Numbers Using Various Methods

Pascal Code for Subtracting Two Numbers Using Various Methods

In this tutorial, we will discuss “Pascal Code Examples for Subtracting Two Numbers Using Various Methods.”

Subtract two num

Subtract Two Numbers

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

(similar to how you explored “add two numbers in different ways“).

Simple Subtraction with Variable

In this program, we are using the normal Subtraction (-) operator with a variable

Program 1

program SubtractOperator;
var 
   num1,num2,result : integer;
begin
    num1:=34;
    num2:=12;
    result:=num1-num2;
    writeln ('Subtraction is ',result);
end.

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

Subtraction is 22

Subtraction Using  User input

In this program, we are using the normal Subtraction (-) operator and variables with user input.

Program 2

program Subtract_Operator;
var 
   num1,num2,result : integer;
begin
    writeln('Subtract two numbers using user input');
    writeln('Enter first number: ');
    readln(num1);
    writeln('Enter second number: ');
    readln(num2);
    result:=num1-num2;
    writeln ('Subtraction is ',result);
end.

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

Subtract two numbers using user input
Enter first number:
34
Enter first number:
13
Subtraction is 21

 

Subtraction Using  Function

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

Program 3

//subtract two numbets using function
program SubtractTwoNumWithFunction;
function sub(x,y:Integer):Integer;

begin
  writeln ('Subtract two numbers using function');
    sub:=x-y;
  end;
  var 
    num1,num2,result:Integer;
    begin
  num1:=60;
  num2:=38;
  result:=sub(num1,num2);
  writeln('subtract= ',result);
end.

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

Subtract two numbers using function
subtract= 22

 

Subtraction Using  Procedure

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

Program 4

//Substract two numbets using Proceedure
program SubTwoNumWithProcedure;
var
   num1,num2,result : integer;
procedure Subtract(x,y:Integer; var result:Integer);

begin
  writeln ('Subtract two numbers using Procedure');
    result:=x-y;
  end;
    begin
  num1:=20;
  num2:=18;
  Subtract(num1,num2,result);
  writeln('Subtraction= ',result);
end.

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

Subtract two numbers using Procedure
Subtraction= 2

 

Subtraction Using  Loop

In this program, we are using the for loop to Subtract two numbers

Program 5

//subtract two numbers using Subtracting Loop
program SubractUsingLoop;
var
   num1,num2,result,i : integer;

begin
  writeln ('Subtract two numbers using Procedure');
  num1:=200;
  num2:=180;
  result:=num1;
  for i:=1 to num2 do
  result := result -1;
  writeln('Subtraction= ',result);
end.

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

Subtract two numbers using Procedure
Subtraction= 20

 

Subtracting Using Pointer

In this program, we are using the pointer to Subtract two numbers

Program 6

//subtract two numbers using Pointer
program SubractUsingPointer;
var
   num1,num2,result: integer;
   p1,p2:^integer;

begin
  writeln ('Subtract two numbers using Pointer');
  new(p1);
  new(p2);
  num1:=250;
  num2:=150;
  p1^:=num1;
  p2^:=num2;
  result:=p1^ - p2^;
  
  writeln('Subtraction= ',result);
end.

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

Subtract two numbers using Pointer
Subtraction= 100

 

These cover the minus operator, function, procedure, pointer, and more

Related post

10 ways to add two numbers in Java

20 ways to subtract two numbers in Java

Subtraction of two numbers in Java using recursion 

Subtraction of two numbers in PHP

 

Addition of two numbers using pascal in various ways

Pascal Code for Subtracting Two Numbers Using Various Methods

How to Subtract Two Numbers in Java Using a For Loop
Java Program: Subtract Two Numbers Using Loops(Examples)
Pascal