Multiply a number with 10 without using multiplication operator
Last Updated :
11 Sep, 2023
Given a number, the task is to multiply it with 10 without using multiplication operator?
Examples:
Input : n = 50
Output: 500
// multiplication of 50 with 10 is = 500
Input : n = 16
Output: 160
// multiplication of 16 with 10 is = 160
A simple solution for this problem is to run a loop and add n with itself 10 times. Here we need to perform 10 operations.
C++
#include<bits/stdc++.h>
using namespace std;
int multiplyTen( int n)
{
int sum=0;
for ( int i=0;i<10;i++)
{
sum=sum+n;
}
return sum;
}
int main()
{
int n = 50;
cout << multiplyTen(n);
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static int multiplyTen( int n) {
int sum = 0 ;
for ( int i = 0 ; i < 10 ; i++) {
sum = sum + n;
}
return sum;
}
public static void main(String[] args) {
int n = 50 ;
System.out.println(multiplyTen(n));
}
}
|
Python3
def multiplyTen(n):
sum = 0
for i in range ( 10 ):
sum + = n
return sum
n = 50
print (multiplyTen(n))
|
C#
using System;
public class GFG {
static int MultiplyTen( int n) {
int sum = 0;
for ( int i = 0; i < 10; i++) {
sum += n;
}
return sum;
}
static void Main( string [] args) {
int n = 50;
Console.WriteLine(MultiplyTen(n));
}
}
|
Javascript
function multiplyTen(n) {
let sum = 0;
for (let i = 0; i < 10; i++) {
sum += n;
}
return sum;
}
let n = 50;
console.log(multiplyTen(n));
|
Time Complexity: O(1)
Auxiliary Space: O(1)
A better solution is to use bit manipulation. We have to multiply n with 10 i.e; n*10, we can write this as n*(2+8) = n*2 + n*8 and since we are not allowed to use multiplication operator we can do this using left shift bitwise operator. So n*10 = n<<1 + n<<3.
C++
#include<bits/stdc++.h>
using namespace std;
int multiplyTen( int n)
{
return (n<<1) + (n<<3);
}
int main()
{
int n = 50;
cout << multiplyTen(n);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int multiplyTen( int n)
{
return (n << 1 ) + (n << 3 );
}
public static void main(String[] args)
{
int n = 50 ;
System.out.println(multiplyTen(n));
}
}
|
Python 3
def multiplyTen(n):
return (n << 1 ) + (n << 3 )
n = 50
print (multiplyTen(n))
|
C#
using System;
class GFG {
public static int multiplyTen( int n)
{
return (n << 1) + (n << 3);
}
public static void Main()
{
int n = 50;
Console.Write(multiplyTen(n));
}
}
|
PHP
<?php
function multiplyTen( $n )
{
return ( $n << 1) + ( $n << 3);
}
$n = 50;
echo multiplyTen( $n );
?>
|
Javascript
<script>
function multiplyTen(n)
{
return (n<<1) + (n<<3);
}
let n = 50;
document.write(multiplyTen(n));
</script>
|
Output:
500
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...