Multiplication Without Using '*' Operator

Tuesday, July 19, 2011 0 Comments A+ a-

C++ program for Multiplication without using the * operator


Multiplication can be implemented without using the multiplication operator which is asterisks ( * ). This can be done through addition since multiplication is a series of addition.
In the code below, the first number is used as loop counter as to how many times the second number is added to the product variable initialized to 0.

/*
Multiplication without using * operator
*/
#include <iostream>

using namespace std;

int main(void)
{
 //variables
 int number1, number2, product;
 char stay;

 cout << "Input first number: ";
 cin >> number1;

 cout << endl << "Input second number: ";
 cin >> number2;

 cout <<endl << number1 << " * " << number2 << " = ";

 product = 0;

 // choose either of the two numbers for the addition
 // choose the other number as a counter
 while (number2 > 0)
 {
  //add the number
  product += number1;

  //decrement counter
  number2--;
 }
 
 cout << product <<endl;
 
 //wait for an input before exit 
 cin >> stay;

 return 0;
}