Posted on by Kalkicode
Code Mathematics

Multiply integer number with 3.5

The problem entails multiplying an integer number by the floating-point value 3.5 using two different methods. One method involves using the normal multiplication operation, while the other method employs bitwise operations to achieve the same result. Additionally, the code takes into account handling negative odd numbers to ensure accurate results.

Problem Statement

Given an integer number, the goal is to multiply it by 3.5 using both the normal multiplication operation and a combination of bitwise operations. The code also handles negative odd numbers correctly to produce accurate results.

Example

Let's examine the examples provided by the code to better understand the problem:

Example A

  • Given number: 6
  • Multiply by 3.5
  • Result using normal multiplication: 3.5 * 6 = 21
  • Result using bitwise operations: (6 << 1) + (6 >> 1) + 6 = 21

Example B

  • Given number: 31
  • Multiply by 3.5
  • Result using normal multiplication: 3.5 * 31 = 108.5 (approximated as 108)
  • Result using bitwise operations: (31 << 1) + (31 >> 1) + 31 = 108

Example C

  • Given number: -5
  • Multiply by 3.5
  • Result using normal multiplication: 3.5 * -5 = -17.5 (approximated as -17)
  • Result using bitwise operations: (-5 << 1) + (-5 >> 1) + -5 = -17

Idea to Solve

  1. For the normal multiplication method, simply multiply the integer number by 3.5.
  2. For the bitwise operation method:
    • Left shift the number by 1 (multiply by 2).
    • Right shift the number by 1 (divide by 2).
    • Add the original number.
    • Handle the case of negative odd numbers by adding 1 to the result if the result is negative and the original number is odd.

Algorithm Explanation

  1. The multiplyBy3Dot5 function takes an integer as input.
  2. It performs normal multiplication of the number by 3.5.
  3. It then performs the bitwise operations mentioned above to achieve the same multiplication.
  4. The code handles negative odd numbers to ensure accurate results.
  5. The results of both methods are printed.

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

Time Complexity

Both the normal multiplication and bitwise operations take constant time, O(1), as they involve a fixed number of operations that do not depend on the input size. Therefore, the overall time complexity of the code remains constant for any input.

Comment

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