Russian peasant multiplication
Here given code implementation process.
// C Program
// Russian peasant multiplication
#include <stdio.h>
void multiplication(int a, int b)
{
// Auxiliary variables
int ans = 0;
int x = a;
int y = b;
// Assume that given number is non negative
// When number is negative then
// handle negative numbers
if (x < 0)
{
x = -x;
}
if (y < 0)
{
y = -y;
}
// Main logic
while (y > 0)
{
if (y & 1 == 1)
{
ans = ans + x;
}
// Left shift by 1
x = x << 1;
// Righ shift by 1
y = y >> 1;
}
if (ans > 0 && !(a < 0 && b < 0 || (a >= 0 && b >= 0)))
{
// Change number to its negative form
ans = -ans;
}
// Display calculated result
printf(" (%d x %d) : %d\n", a, b, ans);
}
int main()
{
// Test
multiplication(6, 7);
multiplication(32, 10);
multiplication(3, 5);
multiplication(-3, 5);
multiplication(-3, -5);
multiplication(2, -5);
return 0;
}
Output
(6 x 7) : 42
(32 x 10) : 320
(3 x 5) : 15
(-3 x 5) : -15
(-3 x -5) : 15
(2 x -5) : -10
/*
Java program
Russian peasant multiplication
*/
public class RussianMultiplication
{
public void multiplication(int a, int b)
{
// Auxiliary variables
int ans = 0;
int x = a;
int y = b;
// Assume that given number is non negative
// When number is negative then
// handle negative numbers
if (x < 0)
{
x = -x;
}
if (y < 0)
{
y = -y;
}
// Main logic
while (y > 0)
{
if ((y & 1) == 1)
{
ans = ans + x;
}
// Left shift by 1
x = x << 1;
// Righ shift by 1
y = y >> 1;
}
if (ans > 0 &&
!(a < 0 && b < 0 || (a >= 0 && b >= 0)))
{
// Change number to its negative form
ans = -ans;
}
// Display calculated result
System.out.println(" (" + a + " x " + b + ") : " + ans);
}
public static void main(String[] args)
{
RussianMultiplication task = new RussianMultiplication();
// Test
task.multiplication(6, 7);
task.multiplication(32, 10);
task.multiplication(3, 5);
task.multiplication(-3, 5);
task.multiplication(-3, -5);
task.multiplication(2, -5);
}
}
Output
(6 x 7) : 42
(32 x 10) : 320
(3 x 5) : 15
(-3 x 5) : -15
(-3 x -5) : 15
(2 x -5) : -10
// Include header file
#include <iostream>
using namespace std;
/*
C++ program
Russian peasant multiplication
*/
class RussianMultiplication
{
public: void multiplication(int a, int b)
{
// Auxiliary variables
int ans = 0;
int x = a;
int y = b;
// Assume that given number is non negative
// When number is negative then
// handle negative numbers
if (x < 0)
{
x = -x;
}
if (y < 0)
{
y = -y;
}
// Main logic
while (y > 0)
{
if ((y &1) == 1)
{
ans = ans + x;
}
// Left shift by 1
x = x << 1;
// Righ shift by 1
y = y >> 1;
}
if (ans > 0 && !(a < 0 && b < 0 || (a >= 0 && b >= 0)))
{
// Change number to its negative form
ans = -ans;
}
// Display calculated result
cout << " (" << a << " x " << b << ") : " << ans << endl;
}
};
int main()
{
RussianMultiplication *task = new RussianMultiplication();
// Test
task->multiplication(6, 7);
task->multiplication(32, 10);
task->multiplication(3, 5);
task->multiplication(-3, 5);
task->multiplication(-3, -5);
task->multiplication(2, -5);
return 0;
}
Output
(6 x 7) : 42
(32 x 10) : 320
(3 x 5) : 15
(-3 x 5) : -15
(-3 x -5) : 15
(2 x -5) : -10
// Include namespace system
using System;
/*
Csharp program
Russian peasant multiplication
*/
public class RussianMultiplication
{
public void multiplication(int a, int b)
{
// Auxiliary variables
int ans = 0;
int x = a;
int y = b;
// Assume that given number is non negative
// When number is negative then
// handle negative numbers
if (x < 0)
{
x = -x;
}
if (y < 0)
{
y = -y;
}
// Main logic
while (y > 0)
{
if ((y & 1) == 1)
{
ans = ans + x;
}
// Left shift by 1
x = x << 1;
// Righ shift by 1
y = y >> 1;
}
if (ans > 0 && !(a < 0 && b < 0 || (a >= 0 && b >= 0)))
{
// Change number to its negative form
ans = -ans;
}
// Display calculated result
Console.WriteLine(" (" + a + " x " + b + ") : " + ans);
}
public static void Main(String[] args)
{
RussianMultiplication task = new RussianMultiplication();
// Test
task.multiplication(6, 7);
task.multiplication(32, 10);
task.multiplication(3, 5);
task.multiplication(-3, 5);
task.multiplication(-3, -5);
task.multiplication(2, -5);
}
}
Output
(6 x 7) : 42
(32 x 10) : 320
(3 x 5) : 15
(-3 x 5) : -15
(-3 x -5) : 15
(2 x -5) : -10
package main
import "fmt"
/*
Go program
Russian peasant multiplication
*/
func multiplication(a, b int) {
// Auxiliary variables
var ans int = 0
var x int = a
var y int = b
// Assume that given number is non negative
// When number is negative then
// handle negative numbers
if x < 0 {
x = -x
}
if y < 0 {
y = -y
}
// Main logic
for (y > 0) {
if (y & 1) == 1 {
ans = ans + x
}
// Left shift by 1
x = x << 1
// Righ shift by 1
y = y >> 1
}
if ans > 0 && !(a < 0 && b < 0 || (a >= 0 && b >= 0)) {
// Change number to its negative form
ans = -ans
}
// Display calculated result
fmt.Println(" (", a, " x ", b, ") : ", ans)
}
func main() {
// Test
multiplication(6, 7)
multiplication(32, 10)
multiplication(3, 5)
multiplication(-3, 5)
multiplication(-3, -5)
multiplication(2, -5)
}
Output
(6 x 7) : 42
(32 x 10) : 320
(3 x 5) : 15
(-3 x 5) : -15
(-3 x -5) : 15
(2 x -5) : -10
<?php
/*
Php program
Russian peasant multiplication
*/
class RussianMultiplication
{
public function multiplication($a, $b)
{
// Auxiliary variables
$ans = 0;
$x = $a;
$y = $b;
// Assume that given number is non negative
// When number is negative then
// handle negative numbers
if ($x < 0)
{
$x = -$x;
}
if ($y < 0)
{
$y = -$y;
}
// Main logic
while ($y > 0)
{
if (($y & 1) == 1)
{
$ans = $ans + $x;
}
// Left shift by 1
$x = $x << 1;
// Righ shift by 1
$y = $y >> 1;
}
if ($ans > 0 &&
!($a < 0 && $b < 0 || ($a >= 0 && $b >= 0)))
{
// Change number to its negative form
$ans = -$ans;
}
// Display calculated result
echo(" (".$a.
" x ".$b.
") : ".$ans.
"\n");
}
}
function main()
{
$task = new RussianMultiplication();
// Test
$task->multiplication(6, 7);
$task->multiplication(32, 10);
$task->multiplication(3, 5);
$task->multiplication(-3, 5);
$task->multiplication(-3, -5);
$task->multiplication(2, -5);
}
main();
Output
(6 x 7) : 42
(32 x 10) : 320
(3 x 5) : 15
(-3 x 5) : -15
(-3 x -5) : 15
(2 x -5) : -10
/*
Node JS program
Russian peasant multiplication
*/
class RussianMultiplication
{
multiplication(a, b)
{
// Auxiliary variables
var ans = 0;
var x = a;
var y = b;
// Assume that given number is non negative
// When number is negative then
// handle negative numbers
if (x < 0)
{
x = -x;
}
if (y < 0)
{
y = -y;
}
// Main logic
while (y > 0)
{
if ((y & 1) == 1)
{
ans = ans + x;
}
// Left shift by 1
x = x << 1;
// Righ shift by 1
y = y >> 1;
}
if (ans > 0 && !(a < 0 && b < 0 || (a >= 0 && b >= 0)))
{
// Change number to its negative form
ans = -ans;
}
// Display calculated result
console.log(" (" + a + " x " + b + ") : " + ans);
}
}
function main()
{
var task = new RussianMultiplication();
// Test
task.multiplication(6, 7);
task.multiplication(32, 10);
task.multiplication(3, 5);
task.multiplication(-3, 5);
task.multiplication(-3, -5);
task.multiplication(2, -5);
}
main();
Output
(6 x 7) : 42
(32 x 10) : 320
(3 x 5) : 15
(-3 x 5) : -15
(-3 x -5) : 15
(2 x -5) : -10
# Python 3 program
# Russian peasant multiplication
class RussianMultiplication :
def multiplication(self, a, b) :
# Auxiliary variables
ans = 0
x = a
y = b
# Assume that given number is non negative
# When number is negative then
# handle negative numbers
if (x < 0) :
x = -x
if (y < 0) :
y = -y
# Main logic
while (y > 0) :
if ((y & 1) == 1) :
ans = ans + x
# Left shift by 1
x = x << 1
# Righ shift by 1
y = y >> 1
if (ans > 0 and not(a < 0 and b < 0 or(a >= 0 and b >= 0))) :
# Change number to its negative form
ans = -ans
# Display calculated result
print(" (", a ," x ", b ,") : ", ans)
def main() :
task = RussianMultiplication()
# Test
task.multiplication(6, 7)
task.multiplication(32, 10)
task.multiplication(3, 5)
task.multiplication(-3, 5)
task.multiplication(-3, -5)
task.multiplication(2, -5)
if __name__ == "__main__": main()
Output
( 6 x 7 ) : 42
( 32 x 10 ) : 320
( 3 x 5 ) : 15
( -3 x 5 ) : -15
( -3 x -5 ) : 15
( 2 x -5 ) : -10
# Ruby program
# Russian peasant multiplication
class RussianMultiplication
def multiplication(a, b)
# Auxiliary variables
ans = 0
x = a
y = b
# Assume that given number is non negative
# When number is negative then
# handle negative numbers
if (x < 0)
x = -x
end
if (y < 0)
y = -y
end
# Main logic
while (y > 0)
if ((y & 1) == 1)
ans = ans + x
end
# Left shift by 1
x = x << 1
# Righ shift by 1
y = y >> 1
end
if (ans > 0 && !(a < 0 && b < 0 || (a >= 0 && b >= 0)))
# Change number to its negative form
ans = -ans
end
# Display calculated result
print(" (", a ," x ", b ,") : ", ans, "\n")
end
end
def main()
task = RussianMultiplication.new()
# Test
task.multiplication(6, 7)
task.multiplication(32, 10)
task.multiplication(3, 5)
task.multiplication(-3, 5)
task.multiplication(-3, -5)
task.multiplication(2, -5)
end
main()
Output
(6 x 7) : 42
(32 x 10) : 320
(3 x 5) : 15
(-3 x 5) : -15
(-3 x -5) : 15
(2 x -5) : -10
/*
Scala program
Russian peasant multiplication
*/
class RussianMultiplication()
{
def multiplication(a: Int, b: Int): Unit = {
// Auxiliary variables
var ans: Int = 0;
var x: Int = a;
var y: Int = b;
// Assume that given number is non negative
// When number is negative then
// handle negative numbers
if (x < 0)
{
x = -x;
}
if (y < 0)
{
y = -y;
}
// Main logic
while (y > 0)
{
if ((y & 1) == 1)
{
ans = ans + x;
}
// Left shift by 1
x = x << 1;
// Righ shift by 1
y = y >> 1;
}
if (ans > 0 && !(a < 0 && b < 0 || (a >= 0 && b >= 0)))
{
// Change number to its negative form
ans = -ans;
}
// Display calculated result
println(" (" + a + " x " + b + ") : " + ans);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: RussianMultiplication = new RussianMultiplication();
// Test
task.multiplication(6, 7);
task.multiplication(32, 10);
task.multiplication(3, 5);
task.multiplication(-3, 5);
task.multiplication(-3, -5);
task.multiplication(2, -5);
}
}
Output
(6 x 7) : 42
(32 x 10) : 320
(3 x 5) : 15
(-3 x 5) : -15
(-3 x -5) : 15
(2 x -5) : -10
/*
Swift 4 program
Russian peasant multiplication
*/
class RussianMultiplication
{
func multiplication(_ a: Int, _ b: Int)
{
// Auxiliary variables
var ans: Int = 0;
var x: Int = a;
var y: Int = b;
// Assume that given number is non negative
// When number is negative then
// handle negative numbers
if (x < 0)
{
x = -x;
}
if (y < 0)
{
y = -y;
}
// Main logic
while (y > 0)
{
if ((y & 1) == 1)
{
ans = ans + x;
}
// Left shift by 1
x = x << 1;
// Righ shift by 1
y = y >> 1;
}
if (ans > 0 && !(a < 0 && b < 0 || (a >= 0 && b >= 0)))
{
// Change number to its negative form
ans = -ans;
}
// Display calculated result
print(" (", a ," x ", b ,") : ", ans);
}
}
func main()
{
let task: RussianMultiplication = RussianMultiplication();
// Test
task.multiplication(6, 7);
task.multiplication(32, 10);
task.multiplication(3, 5);
task.multiplication(-3, 5);
task.multiplication(-3, -5);
task.multiplication(2, -5);
}
main();
Output
( 6 x 7 ) : 42
( 32 x 10 ) : 320
( 3 x 5 ) : 15
( -3 x 5 ) : -15
( -3 x -5 ) : 15
( 2 x -5 ) : -10
/*
Kotlin program
Russian peasant multiplication
*/
class RussianMultiplication
{
fun multiplication(a: Int, b: Int): Unit
{
// Auxiliary variables
var ans: Int = 0;
var x: Int = a;
var y: Int = b;
// Assume that given number is non negative
// When number is negative then
// handle negative numbers
if (x < 0)
{
x = -x;
}
if (y < 0)
{
y = -y;
}
// Main logic
while (y > 0)
{
if ((y and 1) == 1)
{
ans = ans + x;
}
// Left shift by 1
x = x shl 1;
// Righ shift by 1
y = y shr 1;
}
if (ans > 0 && !(a < 0 && b < 0 || (a >= 0 && b >= 0)))
{
// Change number to its negative form
ans = -ans;
}
// Display calculated result
println(" (" + a + " x " + b + ") : " + ans);
}
}
fun main(args: Array < String > ): Unit
{
val task: RussianMultiplication = RussianMultiplication();
// Test
task.multiplication(6, 7);
task.multiplication(32, 10);
task.multiplication(3, 5);
task.multiplication(-3, 5);
task.multiplication(-3, -5);
task.multiplication(2, -5);
}
Output
(6 x 7) : 42
(32 x 10) : 320
(3 x 5) : 15
(-3 x 5) : -15
(-3 x -5) : 15
(2 x -5) : -10
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment