Pascal Code to Multiply Two Numbers Using Different Methods
Pascal Code to Multiply Two Numbers Using Different Methods
In this tutorial, we will discuss “Pascal Code to Multiply Two Numbers Using Different Methods.”
Subtract Two Numbers
You want to see different ways of multiplying two numbers in Pascal. Let’s go step by step with a few approaches.
In this article, we use 6 different ways to multiply two numbers.
Simple Multiplication with Variable
In this program, we are using the normal Subtraction (-) operator with a variable
Simple Multiplication Operator
Program 1
program MulyiplyTwoNum;
var
num1,num2,product:Integer;
begin
writeln ('Multiply two numbers');
num1:=50;
num2:=18;
product:=num1*num2;
writeln('Product:= ',product);
end.
When the above code is executed, it produces the following result
Multiply two numbers using function
Product:= 900
Multiplication Using user input
Program 2
program MulyiplyTwoNum;
var
num1,num2,product:Integer;
begin
writeln ('Multiply two numbers using user input');
writeln ('Enter first number');
readln(num1);
writeln ('Enter second number');
readln(num2);
product:=num1*num2;
writeln('Product:= ',product);
end.
When the above code is executed, it produces the following result
Multiply two numbers using user input
Enter first number
25
Enter second number
10
Product:= 250
Multiplication Using Repeated Addition(Loops)
Program 3
program MulyiplyTwoNumUsingLoop;
uses crt;
var
num1,num2,product,i:Integer;
begin
writeln ('Multiply two numbers using Repeated addition');
num1:=50;
num2:=18;
product:=0;
for i:=1 to num2 do
product := product + num1;
writeln('Product:= ',product);
end.
When the above code is executed, it produces the following result
Multiply two numbers using Repeated addition
Product:= 900
Multiplication Using While loop
Program 4
program MulyiplyTwoNumUsingWhile;
uses crt;
var
num1,num2,product,count:Integer;
begin
writeln ('Multiply two numbers using While');
num1:=40;
num2:=15;
product:=0;
count :=0;
while count < num2 do
begin
product := product + num1;
count:=count+1;
end;
writeln('Product:= ',product);
end.
When the above code is executed, it produces the following result
Multiply two numbers using function
Product: = 600
Multiplication Using Recursion
Program 5
//multiplication using recursion
program SubtractTwoNumWithFunction;
function multiply(x,y:Integer):Integer;
begin
if y=0 then
multiply := 0
else
multiply := x + multiply(x,y-1);
end;
var
num1,num2,product:Integer;
begin
num1:=60;
num2:=30;
product:=multiply(num1,num2);
writeln('product= ',product);
end.
When the above code is executed, it produces the following result
product= 1800
Multiplication With out using * (Bitwise left Shift for Powers of 2)
Program 6
//multiplication without using *
program MultiplyNumWithShift;
uses crt;
var
num1,num2,product:Integer;
begin
clrscr;
writeln('Enter first number');
readln(num1);
writeln('Enter second number , must be power of 2');
readln(num2);
product:=num1 shl trunc(ln(num2)/ln(2));
{equialant to a*b when b=2^n}
writeln('product= ',product);
end.
When the above code is executed, it produces the following result
Enter first number
46
Enter second number , must be power of 2
4
product= 184
Similar post
Java program to multiply two numbers
C program to multiply two numbers
C++ program to multiply two numbers
Python program to multiply two numbers
Java program to multiply two floating-point numbers
C program to multiply two floating-point numbers