-
Notifications
You must be signed in to change notification settings - Fork 344
/
Copy pathMultiplicationWithoutUsingMultiplicationOperator.cpp
80 lines (44 loc) · 1.54 KB
/
MultiplicationWithoutUsingMultiplicationOperator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//C++ program for Multiplication of numbers without using multiplication operator
#include<bits/stdc++.h>
using namespace std;
//Performs multiplication
int multiply(int firstNumber, int secondNumber){
int result = 0; //result of multiplication
int flag = 1; //Decide whether result is positive or negative
//Return 0, if any number is equal to 0
if( firstNumber == 0 || secondNumber == 0 ){
return 0;
}
//If both numbers are negative, then find their absolute
if( firstNumber < 0 && secondNumber < 0 ){
firstNumber = -(firstNumber);
secondNumber = -(secondNumber);
}
//If first number is negative, then change to absolute
else if( firstNumber < 0 ){
firstNumber = -(firstNumber);
flag = 0; //Result of multiplication must be negative
}
//If second number is negative, then change to absolute
else if( secondNumber < 0 ){
secondNumber = -(secondNumber);
flag = 0; //Result of multiplication must be negative
}
//Calculates multiplication of numbers
for(int i=0; i<secondNumber; i++){
result += firstNumber; //add number1, number2 times
}
if( flag == 0 ){ //If only one number is negative
return -result;
}
return result;
}
int main(){
int firstNumber, secondNumber;
cout << "Enter the first number: ";
cin >> firstNumber; //Initialize number1
cout << "Enter the second number: ";
cin >> secondNumber; //Initialize number2
cout << "Multiplication of " << firstNumber << " and " << secondNumber << " is: ";
cout << multiply(firstNumber, secondNumber); //Display result of Multiplication
}