+ 3
multiplication using addition
i want to write a C program that calculates multiplication using addition, can someone help me?
9 Antworten
+ 20
//edited code of J Dombino , without 3rd variable "mult"
int num1;
int num2
;
for(int=1;i<=numb2;i++){
num1+=num1;
}
+ 18
if(num 2!=0)
while(num2-->1) num1+=num1;
else num1=0;
//in case num2 is 0
//note : num2 can be -ve also , make code by yourself first for every possibility ... if any problem comes ... then ask , I am following the thread
//hint : use if else
+ 3
You can use a for loop wich iterate num2 times adding num1 each time:
int num1;
int num2
int mult=0; //result
for(int=1;i<=numb2;i++){
mult += num1;
}
+ 2
thank u all :)
+ 2
yeah, you save 32 bits, but you lose the original value of num1 which could be used after the for loop.
+ 1
i know but my teacher asked us to multiply two numbers without using the multiplication so i have to use the loop
+ 1
for(i=1;i<=num2;i++){
prod=num1*i;
}
The code will multiply two numbers correctly but it will do stupid iterations for example if num1 = 2 and num2 = 3:
The first iteration will set prod=2*1=2
The second iteration prod=2*2=4
The third prod=2*3=6
so in the first two iteration the machine is wasting its time. You can directly multiply the two numbers prod=num1*num2 without any for loop;
+ 1
Then use my first code, your teacher is going to give you a 10/10
0
is this correct? https://code.sololearn.com/caa72w8IM022/?ref=app