Multiplying 2 numbers without *

Hi I'm trying to multiply 2 numbers without the * operator and using a function and including a for loop. This is what I have so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

float number(float i, int n)
{
	float sum = 0;
	for(i = 3; i >= 4 || i <= 4; 0)
	{
		cout << sum;
	}
	return sum;
}
int main()
{
	float product = number(3, 4);
	cout << product;

	cin.get();
	cin.get();
}


If I was to compile it, it would just keep repeating 0 why is that?
There's a step-by-step description of how a for-loop executes here:
http://www.cplusplus.com/doc/tutorial/control/
take that, together with the fact that every integer i will always satisfy this condition, i >= 4 || i <= 4 and it gives an infinite loop.
I know how a for loop works but I'm trying to figure out how to multiply 2 numbers without the * operator
I'm trying to figure out how to multiply 2 numbers without the * operator:

n*5 == n + n + n + n + n
and
n * -5 == -(n*5) == -( n + n + n + n + n )
Last edited on

If I was to compile it, it would just keep repeating 0 why is that?
1
2
3
4
5
6
7
8
9
float number(float i, int n)
{
	float sum = 0;
	for(i = 3; i >= 4 || i <= 4; 0)
	{
		cout << sum;
	}
	return sum;
}

You aren't manipulating sum in the loop, so it just stays at 0.
From the tutorial, syntax of for-loop
for (initialization; condition; increase) statement;
In your example,
initialization is i = 3
why i, should it not be a separate integer variable? why 3, should it not be 0 or n?

condition is i >= 4 || i <= 4 as mentioned, this is always true.

increase is 0 - a do-nothing, meaning the loop will either not execute at all, or it will execute forever (depending on condition).

I'd expect you want a loop that counts from 0 to n-1
 
    for (int count=0; count<n; ++count)




I'm trying to multiply 3 and 4 sorry I didn't include that
Thank you
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
#include <iostream>

using namespace std;

float number(float i, int n)
{
	int k;
	float sum = 0;
	if(n < 0) {i = -i; n = -n;}

	for(k = 1; k <= n; k++)
	{
		sum += i;
	}

	return sum;
}

int main()
{
	float product = number(3, 4);
	cout << "The product is : " << product << endl;

	cin.get();
	return 0;
}


The product is : 12

http://cpp.sh/6cva
Last edited on
AchillesO23 wrote:
Thank you

Thank who?
you
#include <iostream>
using namespace std;

// You do not need a fuction to do this.

float number(float i, int n)//you are dealing with whole numbers. It is best to use an intiger in your fuction. ** int number(int a, int n)**
{ //what is the point of (float i, int n) if you are not useing them in the fuction???

float sum = 0; // i recommend chaning this to an "int sum = 0"

for (i = 3; i >= 4 || i <= 4; 0) // <<<--- this section of your loop do not actually do anthing and is not the standed structure of a for loop
{ //try this instead -->>> for(int i = 0; i < a ; i++)

cout << sum; // in line 9 "float sum = 0; // i recommend chaning this to an "int sum = 0" " you have not done any operation to the sum, it will alway be 0.
// "sum = sum + n;" you are say: 1. sum = 0 + 4; 2. sum = 4 + 4; 3. sum = 8 + 4; final value of sum will be 12.
}
return sum; // this is right.. good job!!
}


int main()
{
float product = number(3, 4); // there is no need for you to create a new variable... Your fuction has a return value.
cout << product; // cout << number(3,4); ... this will give you what you the answer.

//there is no reason for a the next to lines ... you are not tring to get a value form the user.
cin.get(); // delete
cin.get(); // delete
}


***************************************************************
good luck
And if you want to multiply 2 numbers, positive or negative, with or without decimals ...

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

#include <iostream>
#include<cmath>
using namespace std;
double product(double a, int b)
{
    if (b==1)
    {
        return a;
    }
    else
    {
        return a + product(a, b - 1);
    }
}
int main()
{
    cout<<"Enter first number: \n";
    double i;
    cin>>i;
    double fractional_i, integral_i;
    int rhs_i = 0;
    if(i < 0)
    {
        fractional_i = -modf(i, &integral_i);
    }
    else
    {
        fractional_i = modf(i, &integral_i);
    }
    if(fractional_i == 0)
    {
        rhs_i = 0;
    }
    else
    {
        do
        {
            i *= 10;
            fractional_i = modf(i, &integral_i);
            rhs_i++;
        }   while(fractional_i != 0);
    }
    cout<<"Enter second number: \n";
    double j;
    cin>>j;
    double j_org = j;
    double fractional_j, integral_j;
    int rhs_j = 0;
    if(j < 0)
    {
        j = - j;
    }
    fractional_j = modf(j, &integral_j);

    if(fractional_j == 0)
    {
        rhs_j = 0;
    }
    else
    {
        do
        {
            j *= 10;
            fractional_j = modf(j, &integral_j);
            rhs_j++;
        }   while(fractional_j != 0);
    }
    if(j_org < 0)
    {
        double result = - product(i, j)/pow(10,rhs_i)/pow(10,rhs_j);
        cout<<result<<"\n";
    }
    else
    {
        double result = product(i, j)/pow(10,rhs_i)/pow(10,rhs_j);
        cout<<result<<"\n";
    }
}
Last edited on
Stop press: I just noticed a couple of -1* ... in my program ... working on it

edit: OK, multiplications by -1 have been removed in the above program. modf() does not use any multiplication internally, just bits and masks; pow() for 2 numbers that don't have any fractional parts (as in the program above) can also be done by recursion i.e. w/o multiplicaton
Last edited on
@gunnerfunner
Why does your code have to be so complex?

And also :
j = -1 * j;
Please do not use the operator (*) in your code.
This is broken for negative n. (The loop will never execute)
1
2
3
4
5
6
7
8
9
10
11
12
13
float number(float i, int n)
{
	int k;
	float sum = 0;
	if(n < 0) i = -i;

	for(k = 1; k <= n; k++)
	{
		sum += i;
	}

	return sum;
}


Either of these would be fine:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
double multiply( double a, int n )
{
    if( n == 0  ) return 0 ;
    else if( n > 0 ) return a + multiply( a, n-1 ) ; // a*n == a + a*(n-1)
    else return -multiply( a, -n ) ; // negative n - multiply by positive n and reverse the sign
}

double multiply_h( double a, int n )
{
    if( n >= 0 ) // non-negative n
    {
        double sum = 0 ;
        for( int i = 0 ; i < n ; ++i ) sum += a ; // sum up the value 'n' times
        return sum ;
    }

    else // negative n
    {
        double sum = 0 ;
        for( int i = 0 ; i > n ; --i ) sum -= a ; // sum up the negative value
        return sum ;
    }
}

http://coliru.stacked-crooked.com/a/46d580b98e23efaa
@JLBorges.
if(n < 0) {i = -i; n = -n;}
Thanks for correcting.

Edit :
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
#include <iostream>

using namespace std;

float number(float i, int n)
{
	int k;
	float sum = 0;
	if(n < 0) {i = -i; n = -n;}

	for(k = 1; k <= n; k++)
	{
		sum += i;
	}

	return sum;
}

int main()
{
	float product = number(3, 4);
	cout << "The product is : " << product << endl;

	cin.get();
	return 0;
}


The product is : 12

http://cpp.sh/6cva
Last edited on
@JLBorges
Either of these would be fine

It is fine, your code is longer than mine so it might be inefficient.
Last edited on
Topic archived. No new replies allowed.