add 1st no 2nd no of times...(3*5)is add 5 3 times or add 3 5times..is it correct??
write program.. with no loop..
we can use recursion to accomplish this task..
#include/* 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("\n %d", multiply(5, -11)); getchar(); return 0;}Time Complexity: O(y) where y is the second argument to function multiply().
add 1st no 2nd no of times...(3*5)is add 5 3 times or add 3 5times..is it correct??
ReplyDeletewrite program.. with no loop..
ReplyDeletewe can use recursion to accomplish this task..
ReplyDelete#include
ReplyDelete/* 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("\n %d", multiply(5, -11));
getchar();
return 0;
}
Time Complexity: O(y) where y is the second argument to function multiply().