### Squaring_Without_Multiplication !!

Code
Diff
• ``````inline float multiply(const float &number1, const float &number2 )
{
if (number1 == 0 || number2 == 0) return 0;
if (number2 > 0) return number1 + multiply(number1, number2 - 1);
if (number2 < 0) return -multiply(number1, -number2);
return -1;
}

void shift_dec_point(float &number, int &decimal_positions)
{
while(number - static_cast<int>(number) > 0.1)
{
number = multiply(number, 10);
decimal_positions++;
}
}

float squared(const float &number)
{
float temp = number;
if(temp < 0) temp = -temp;

int decimal_positions = 0;
shift_dec_point(temp, decimal_positions);
float factor = temp;
if(decimal_positions > 0) {
temp = multiply(temp, temp);
switch (decimal_positions){
case 1: factor = 0.01; break;
case 2: factor = 0.001; break;
case 3: factor = 0.0001; break;
case 4: factor = 0.00001;
}
}
return multiply(factor,temp);
}``````
• inline float multiply(float number1, float number2 )
• inline float multiply(const float &number1, const float &number2 )
• {
• float answer = 0;
• while( number2 > 0 )
• {
• answer += number1;
• number2--;
• }
• if (number1 == 0 || number2 == 0) return 0;
• if (number2 > 0) return number1 + multiply(number1, number2 - 1);
• if (number2 < 0) return -multiply(number1, -number2);
• return -1;
• }
• float squared(float number)
• void shift_dec_point(float &number, int &decimal_positions)
• {
• if( number < 0 ) number = -number;
• int decimal_positions = 0;
• while( number - int(number) > 0.1 )
• while(number - static_cast<int>(number) > 0.1)
• {
• number = multiply(number, 10);
• decimal_positions++;
• }
• float factor = number;
• if( decimal_positions > 0 ) {
• number = multiply(number,number);
• switch (decimal_positions){
• case 1: factor = 0.01; break;
• case 2: factor = 0.001; break;
• case 3: factor = 0.0001; break;
• case 4: factor = 0.00001;
• }}
• return multiply(factor,number);
• }
• float squared(const float &number)
• {
• float temp = number;
• if(temp < 0) temp = -temp;
• int decimal_positions = 0;
• shift_dec_point(temp, decimal_positions);
• float factor = temp;
• if(decimal_positions > 0) {
• temp = multiply(temp, temp);
• switch (decimal_positions){
• case 1: factor = 0.01; break;
• case 2: factor = 0.001; break;
• case 3: factor = 0.0001; break;
• case 4: factor = 0.00001;
• }
• }
• return multiply(factor,temp);
• }