Ok, just as my topic says, I'm having trouble why it cannot consider negative multipliers.

Code below:

//Multiplication  by Repeated addition
#include<stdio.h>
main()
{
int k, l, m, n;

printf("Enter Multiplicand: ");
scanf("%d", &k);
printf("Enter Multiplier: ");
scanf("%d",&l);
	
	n=0;
	for(m=0;m<l;m++){
	n=n+k;
	}
printf("Result is: %d", n);
printf("\nThank you for using Multiplication. Have a Nice day.\n");
}

Any ideas?

I am NOT going to tell you this answer - you would hate that, surely.

if(hint, hint), EITHER the multiplicater, OR the multiplicand, (but not both!) are negative, what will you have to put at the very end of your function, to make the logic correct?

C'mon! ;)

Umm, does the trick work at basic C? I'm not sure since we've only been taught of Basic C at the moment. so far, with loops.

Oh, and at <stdio.h>.

Edited 6 Years Ago by CodeAerial: n/a

Umm, does the trick work at basic C? I'm not sure since we've only been taught of Basic C at the moment. so far, with loops.

Oh, and at <stdio.h>.

You can do this in C. Don't focus too much on the technology; the question is about a mathematical algorithm, and the answer will be the same no matter what language you use to implement it.

You read my post, but not really studied it.

Study my previous post, and and this one, and think of your arithmetic. What times what is negative? What times what is positive? How could that be used in your case, as mentioned previously?

Your having problem because of the loop's condition. When the multiplier is less than 0, the condition will not be satisfied and it will go inside the loop. For e.g, you have multiplicand=3, multiplier=-2, the answer would be -6 but the program will output 0. Let's trace the code.

product: 0
multiplicand: 3
multiplier: -2
m: 0
if m is less than multiplier go inside the loop and update product by adding multiplicand to it. do it while the condition is satisfied.
Since m = 0 is NOT LESS THAN multiplier = -2, it will not enter the loop and will output the initial product 0.

I suggest you check first if your multiplier is less than 0 and if both your multiplier and multiplicand are less than.

Here is the modified code:

//Multiplication  by Repeated addition
#include<stdio.h>
main()
{
int k, l, m, n;
 
printf("Enter Multiplicand: ");
scanf("%d", &k);
printf("Enter Multiplier: ");
scanf("%d",&l);
 n=0;
 if (l<0 && k >0) {
	for (m=0;m>l;m--) {
		n=n-k;
	}
 }
 else if (l<0 && k<0) {
	for (m=0;m>l;m--) {
		n=n+k;
	}
 }
 else {
	for(m=0;m<l;m++){
	n=n+k;
	}
 }
	
	
printf("Result is: %d", n);
printf("\nThank you for using Multiplication. Have a Nice day.\n");
}
This question has already been answered. Start a new discussion instead.