I am trying to write a code that will do multiplication with for loops using ++ and -- functions only and when I run the program it returns me with the n1 value I entered no the final product I was wondering what I was doing wrong?
int choice, n1, n2;
int temp=n1;
cout<<"Enter Two Numbers"<< endl;
cin>> n1;
cin>> n2;
for (int i=0;i<n2-1;i++)
{
#include <iostream> // For stream input/output
int main()
{
int n1, n2 ;
std::cout << "enter two numbers: " ;
std::cin >> n1 >> n2 ;
std::cout << n1 << " * " << n2 << " == " ;
// if either n1 or n2 is zero, the result is zero
if( n1==0 || n2==0 ) std::cout << "0\n" ;
else
{
// if one is negative and the other is positive, the result is negative
constbool negative_result = ( n1<0 && n2>0 ) || ( n1>0 && n2<0 ) ;
// now that we know the sign of the result, make both positive
if( n1 < 0 ) n1 = -n1 ;
if( n2 < 0 ) n2 = -n2 ;
// compute the product by using ++
int result = 0 ;
for( int i = 0 ; i < n2 ; ++i ) // n2 times
for( int j = 0 ; j < n1 ; ++j ) ++result ; // increment n1 times
// note: ++result in the inner loop will executes n1*n2 times in all
// note: we assume that the product n1*n2 is within the range of int
// if the result should be negative, make it negative
if(negative_result) result = -result ;
// and then print the result '
std::cout << result << '\n' ;
}
}