Multiply integer number with 3.5
Multiplying an integer number with 3.5 involves multiplying a whole number with the decimal value of 3.5. This operation can be performed using basic arithmetic principles, specifically multiplication. The resulting product will be a decimal number that is equivalent to the integer number multiplied by 3.5.
it is possible to use shift operators to multiply an integer number by 3.5. In binary, 3.5 can be represented as 11.1, where the 1 to the right of the decimal point represents 0.5. To multiply an integer by 3.5, we can shift the binary representation of the integer one bit to the left (which is equivalent to multiplying by 2), and then add the original integer to the shifted value, and then shift the result to the right by one bit (which is equivalent to dividing by 2).
Code Solution
// C Program
// Multiply integer number with 3.5
#include <stdio.h>
void multiplyBy3Dot5(int num)
{
printf("\n Given Number : %d",num);
// Normal way
int a = 3.5 * num;
// By Using bitwise
int b = (num << 1) + (num >> 1) + num;
if(b < 0 && (-num) % 2 != 0)
{
// Handle negative odd number
b+=1;
}
printf("\n Result a : %d",a);
printf("\n Result b : %d",b);
}
int main()
{
// Test A
// num = 6
// (6 << 1) = 12
// (6 >> 1) = 3
// num = 6
// --------------
// Result = 21
multiplyBy3Dot5(6);
// Test B
// num = 31
// (31 << 1) = 62
// (31 >> 1) = 15
// num = 31
// ---------------
// Result = 108
multiplyBy3Dot5(31);
// Test C
// num = -5
// (-5 << 1) = -10
// (-5 >> 1) = -3
// num = -5
// ---------------
// (-18 + 1)
// Result = -17
multiplyBy3Dot5(-5);
return 0;
}
Output
Given Number : 6
Result a : 21
Result b : 21
Given Number : 31
Result a : 108
Result b : 108
Given Number : -5
Result a : -17
Result b : -17
// Java program for
// Multiply integer number with 3.5
public class Multiply
{
public void multiplyBy3Dot5(int num)
{
System.out.print("\n Given Number : " + num );
// Normal way
int a = (int)(3.5 * num);
// By Using bitwise
int b = (num << 1) + (num >> 1) + num;
if (b < 0 && (-num) % 2 != 0)
{
// Handle negative odd number
b += 1;
}
System.out.print("\n Result a : " + a );
System.out.print("\n Result b : " + b );
}
public static void main(String[] args)
{
Multiply task = new Multiply();
// Test A
// num = 6
// (6 << 1) = 12
// (6 >> 1) = 3
// num = 6
// --------------
// Result = 21
task.multiplyBy3Dot5(6);
// Test B
// num = 31
// (31 << 1) = 62
// (31 >> 1) = 15
// num = 31
// ---------------
// Result = 108
task.multiplyBy3Dot5(31);
// Test C
// num = -5
// (-5 << 1) = -10
// (-5 >> 1) = -3
// num = -5
// ---------------
// (-18 + 1)
// Result = -17
task.multiplyBy3Dot5(-5);
}
}
Output
Given Number : 6
Result a : 21
Result b : 21
Given Number : 31
Result a : 108
Result b : 108
Given Number : -5
Result a : -17
Result b : -17
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Multiply integer number with 3.5
class Multiply
{
public: void multiplyBy3Dot5(int num)
{
cout << "\n Given Number : " << num;
// Normal way
int a = (int)(3.5 *num);
// By Using bitwise
int b = (num << 1) + (num >> 1) + num;
if (b < 0 && (-num) % 2 != 0)
{
// Handle negative odd number
b += 1;
}
cout << "\n Result a : " << a;
cout << "\n Result b : " << b;
}
};
int main()
{
Multiply *task = new Multiply();
// Test A
// num = 6
// (6 << 1) = 12
// (6 >> 1) = 3
// num = 6
// --------------
// Result = 21
task->multiplyBy3Dot5(6);
// Test B
// num = 31
// (31 << 1) = 62
// (31 >> 1) = 15
// num = 31
// ---------------
// Result = 108
task->multiplyBy3Dot5(31);
// Test C
// num = -5
// (-5 << 1) = -10
// (-5 >> 1) = -3
// num = -5
// ---------------
// (-18 + 1)
// Result = -17
task->multiplyBy3Dot5(-5);
return 0;
}
Output
Given Number : 6
Result a : 21
Result b : 21
Given Number : 31
Result a : 108
Result b : 108
Given Number : -5
Result a : -17
Result b : -17
// Include namespace system
using System;
// Csharp program for
// Multiply integer number with 3.5
public class Multiply
{
public void multiplyBy3Dot5(int num)
{
Console.Write("\n Given Number : " + num);
// Normal way
int a = (int)(3.5 * num);
// By Using bitwise
int b = (num << 1) + (num >> 1) + num;
if (b < 0 && (-num) % 2 != 0)
{
// Handle negative odd number
b += 1;
}
Console.Write("\n Result a : " + a);
Console.Write("\n Result b : " + b);
}
public static void Main(String[] args)
{
Multiply task = new Multiply();
// Test A
// num = 6
// (6 << 1) = 12
// (6 >> 1) = 3
// num = 6
// --------------
// Result = 21
task.multiplyBy3Dot5(6);
// Test B
// num = 31
// (31 << 1) = 62
// (31 >> 1) = 15
// num = 31
// ---------------
// Result = 108
task.multiplyBy3Dot5(31);
// Test C
// num = -5
// (-5 << 1) = -10
// (-5 >> 1) = -3
// num = -5
// ---------------
// (-18 + 1)
// Result = -17
task.multiplyBy3Dot5(-5);
}
}
Output
Given Number : 6
Result a : 21
Result b : 21
Given Number : 31
Result a : 108
Result b : 108
Given Number : -5
Result a : -17
Result b : -17
package main
import "fmt"
// Go program for
// Multiply integer number with 3.5
type Multiply struct {}
func getMultiply() * Multiply {
var me *Multiply = &Multiply {}
return me
}
func(this Multiply) multiplyBy3Dot5(num int) {
fmt.Print("\n Given Number : ", num)
// Normal way
var a int = (int)(3.5 * float32(num))
// By Using bitwise
var b int = (num << 1) + (num >> 1) + num
if b < 0 && (-num) % 2 != 0 {
// Handle negative odd number
b += 1
}
fmt.Print("\n Result a : ", a)
fmt.Print("\n Result b : ", b)
}
func main() {
var task * Multiply = getMultiply()
// Test A
// num = 6
// (6 << 1) = 12
// (6 >> 1) = 3
// num = 6
// --------------
// Result = 21
task.multiplyBy3Dot5(6)
// Test B
// num = 31
// (31 << 1) = 62
// (31 >> 1) = 15
// num = 31
// ---------------
// Result = 108
task.multiplyBy3Dot5(31)
// Test C
// num = -5
// (-5 << 1) = -10
// (-5 >> 1) = -3
// num = -5
// ---------------
// (-18 + 1)
// Result = -17
task.multiplyBy3Dot5(-5)
}
Output
Given Number : 6
Result a : 21
Result b : 21
Given Number : 31
Result a : 108
Result b : 108
Given Number : -5
Result a : -17
Result b : -17
<?php
// Php program for
// Multiply integer number with 3.5
class Multiply
{
public function multiplyBy3Dot5($num)
{
echo("\n Given Number : ".$num);
// Normal way
$a = (int)(3.5 * $num);
// By Using bitwise
$b = ($num << 1) + ($num >> 1) + $num;
if ($b < 0 && (-$num) % 2 != 0)
{
// Handle negative odd number
$b += 1;
}
echo("\n Result a : ".$a);
echo("\n Result b : ".$b);
}
}
function main()
{
$task = new Multiply();
// Test A
// num = 6
// (6 << 1) = 12
// (6 >> 1) = 3
// num = 6
// --------------
// Result = 21
$task->multiplyBy3Dot5(6);
// Test B
// num = 31
// (31 << 1) = 62
// (31 >> 1) = 15
// num = 31
// ---------------
// Result = 108
$task->multiplyBy3Dot5(31);
// Test C
// num = -5
// (-5 << 1) = -10
// (-5 >> 1) = -3
// num = -5
// ---------------
// (-18 + 1)
// Result = -17
$task->multiplyBy3Dot5(-5);
}
main();
Output
Given Number : 6
Result a : 21
Result b : 21
Given Number : 31
Result a : 108
Result b : 108
Given Number : -5
Result a : -17
Result b : -17
# Python 3 program for
# Multiply integer number with 3.5
class Multiply :
def multiplyBy3Dot5(self, num) :
print("\n Given Number : ", num, end = "")
# Normal way
a = (int)(3.5 * num)
# By Using bitwise
b = (num << 1) + (num >> 1) + num
if (b < 0 and(-num) % 2 != 0) :
# Handle negative odd number
b += 1
print("\n Result a : ", a, end = "")
print("\n Result b : ", b, end = "")
def main() :
task = Multiply()
# Test A
# num = 6
# (6 << 1) = 12
# (6 >> 1) = 3
# num = 6
# --------------
# Result = 21
task.multiplyBy3Dot5(6)
# Test B
# num = 31
# (31 << 1) = 62
# (31 >> 1) = 15
# num = 31
# ---------------
# Result = 108
task.multiplyBy3Dot5(31)
# Test C
# num = -5
# (-5 << 1) = -10
# (-5 >> 1) = -3
# num = -5
# ---------------
# (-18 + 1)
# Result = -17
task.multiplyBy3Dot5(-5)
if __name__ == "__main__": main()
Output
Given Number : 6
Result a : 21
Result b : 21
Given Number : 31
Result a : 108
Result b : 108
Given Number : -5
Result a : -17
Result b : -17
# Ruby program for
# Multiply integer number with 3.5
class Multiply
def multiplyBy3Dot5(num)
print("\n Given Number : ", num)
# Normal way
a = (3.5 * num).to_i
# By Using bitwise
b = (num << 1) + (num >> 1) + num
if (b < 0 && (-num) % 2 != 0)
# Handle negative odd number
b += 1
end
print("\n Result a : ", a)
print("\n Result b : ", b)
end
end
def main()
task = Multiply.new()
# Test A
# num = 6
# (6 << 1) = 12
# (6 >> 1) = 3
# num = 6
# --------------
# Result = 21
task.multiplyBy3Dot5(6)
# Test B
# num = 31
# (31 << 1) = 62
# (31 >> 1) = 15
# num = 31
# ---------------
# Result = 108
task.multiplyBy3Dot5(31)
# Test C
# num = -5
# (-5 << 1) = -10
# (-5 >> 1) = -3
# num = -5
# ---------------
# (-18 + 1)
# Result = -17
task.multiplyBy3Dot5(-5)
end
main()
Output
Given Number : 6
Result a : 21
Result b : 21
Given Number : 31
Result a : 108
Result b : 108
Given Number : -5
Result a : -17
Result b : -17
// Scala program for
// Multiply integer number with 3.5
class Multiply()
{
def multiplyBy3Dot5(num: Int): Unit = {
print("\n Given Number : " + num);
// Normal way
var a: Int = (3.5 * num).toInt;
// By Using bitwise
var b: Int = (num << 1) + (num >> 1) + num;
if (b < 0 && (-num) % 2 != 0)
{
// Handle negative odd number
b += 1;
}
print("\n Result a : " + a);
print("\n Result b : " + b);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Multiply = new Multiply();
// Test A
// num = 6
// (6 << 1) = 12
// (6 >> 1) = 3
// num = 6
// --------------
// Result = 21
task.multiplyBy3Dot5(6);
// Test B
// num = 31
// (31 << 1) = 62
// (31 >> 1) = 15
// num = 31
// ---------------
// Result = 108
task.multiplyBy3Dot5(31);
// Test C
// num = -5
// (-5 << 1) = -10
// (-5 >> 1) = -3
// num = -5
// ---------------
// (-18 + 1)
// Result = -17
task.multiplyBy3Dot5(-5);
}
}
Output
Given Number : 6
Result a : 21
Result b : 21
Given Number : 31
Result a : 108
Result b : 108
Given Number : -5
Result a : -17
Result b : -17
// Swift 4 program for
// Multiply integer number with 3.5
class Multiply
{
func multiplyBy3Dot5(_ num: Int)
{
print("\n Given Number : ", num, terminator: "");
// Normal way
let a: Int = Int((3.5 * Double(num)));
// By Using bitwise
var b: Int = (num << 1) + (num >> 1) + num;
if (b < 0 && (-num) % 2 != 0)
{
// Handle negative odd number
b += 1;
}
print("\n Result a : ", a, terminator: "");
print("\n Result b : ", b, terminator: "");
}
}
func main()
{
let task: Multiply = Multiply();
// Test A
// num = 6
// (6 << 1) = 12
// (6 >> 1) = 3
// num = 6
// --------------
// Result = 21
task.multiplyBy3Dot5(6);
// Test B
// num = 31
// (31 << 1) = 62
// (31 >> 1) = 15
// num = 31
// ---------------
// Result = 108
task.multiplyBy3Dot5(31);
// Test C
// num = -5
// (-5 << 1) = -10
// (-5 >> 1) = -3
// num = -5
// ---------------
// (-18 + 1)
// Result = -17
task.multiplyBy3Dot5(-5);
}
main();
Output
Given Number : 6
Result a : 21
Result b : 21
Given Number : 31
Result a : 108
Result b : 108
Given Number : -5
Result a : -17
Result b : -17
// Kotlin program for
// Multiply integer number with 3.5
class Multiply
{
fun multiplyBy3Dot5(num: Int): Unit
{
print("\n Given Number : " + num);
// Normal way
val a: Int = (3.5 * num).toInt();
// By Using bitwise
var b: Int = (num shl 1) + (num shr 1) + num;
if (b < 0 && (-num) % 2 != 0)
{
// Handle negative odd number
b += 1;
}
print("\n Result a : " + a);
print("\n Result b : " + b);
}
}
fun main(args: Array < String > ): Unit
{
val task: Multiply = Multiply();
// Test A
// num = 6
// (6 << 1) = 12
// (6 >> 1) = 3
// num = 6
// --------------
// Result = 21
task.multiplyBy3Dot5(6);
// Test B
// num = 31
// (31 << 1) = 62
// (31 >> 1) = 15
// num = 31
// ---------------
// Result = 108
task.multiplyBy3Dot5(31);
// Test C
// num = -5
// (-5 << 1) = -10
// (-5 >> 1) = -3
// num = -5
// ---------------
// (-18 + 1)
// Result = -17
task.multiplyBy3Dot5(-5);
}
Output
Given Number : 6
Result a : 21
Result b : 21
Given Number : 31
Result a : 108
Result b : 108
Given Number : -5
Result a : -17
Result b : -17
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