C Prompt the user for two integers Using a loop multiply the

C++

Prompt the user for two integers. Using a loop, multiply the first number by the second number using only addition. For example:

Solution

#include<stdio.h>

/* function to multiply two numbers x and y*/

int multiply(int x, int y)

{

/* 0 multiplied with anything gives 0 */

if(y == 0)

                return 0;

/* Add x one by one */

if(y > 0 )

                return (x + multiply(x, y-1));

/* the case where y is negative */

if(y < 0 )

                return -multiply(x, -y);

}

int main()

{

printf(\"\ %d\", multiply(5, -11));

getchar();

return 0;

}

C++ Prompt the user for two integers. Using a loop, multiply the first number by the second number using only addition. For example:Solution#include<stdio.h&

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site