+ 3

multiplication using addition

i want to write a C program that calculates multiplication using addition, can someone help me?

1st Mar 2018, 12:17 PM
Rahni Sihem
Rahni Sihem - avatar
9 Réponses
+ 20
//edited code of J Dombino , without 3rd variable "mult" int num1; int num2 ; for(int=1;i<=numb2;i++){ num1+=num1; }
1st Mar 2018, 12:43 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 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
1st Mar 2018, 1:34 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 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; }
1st Mar 2018, 12:38 PM
J Domingo Jiménez J
J Domingo Jiménez J - avatar
+ 2
thank u all :)
1st Mar 2018, 12:53 PM
Rahni Sihem
Rahni Sihem - avatar
+ 2
yeah, you save 32 bits, but you lose the original value of num1 which could be used after the for loop.
1st Mar 2018, 12:55 PM
J Domingo Jiménez J
J Domingo Jiménez J - avatar
+ 1
i know but my teacher asked us to multiply two numbers without using the multiplication so i have to use the loop
1st Mar 2018, 1:15 PM
Rahni Sihem
Rahni Sihem - avatar
+ 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;
1st Mar 2018, 1:19 PM
J Domingo Jiménez J
J Domingo Jiménez J - avatar
+ 1
Then use my first code, your teacher is going to give you a 10/10
1st Mar 2018, 1:31 PM
J Domingo Jiménez J
J Domingo Jiménez J - avatar
1st Mar 2018, 12:57 PM
Rahni Sihem
Rahni Sihem - avatar