- Code: Select all
// implement square() without using the multiplication operator; that is, do the
// x*x by repeated addition (start a variable result at 0 and add x to it x times).
//Then run some version of "the first program" using that square().
#include<iostream>
using namespace std;
int square(int i)
{
int result=0; //initializing the result
for (int count=0; count <= i; ++count) { //while count is less than i; result = result + i:
result += i;
}
cout<< result;
}
int main()
{
for (int i=0; i<= 5; ++i ) {
cout<< i <<'\t'<< square(i) << endl;
}
}
As i wrote in the program; I am trying to create a square function without actually multiplying the numbers but rather by adding i to result i times.
I'm sure there is an easy solution to this, but i cant for the love of god see it.
Any hints (not quite ready to give up just yet) are appreciated.
I believe this is the first time i post on these forums as well.. So hello. ;P

