Welcome Guest ( Log In | Register)



2 Pages V   1 2 >  
Reply to this topicStart new topic
> Multiplication Via Addition
htdefiant
post Sep 23 2007, 06:45 PM
Post #1


Advanced Member
*******

Group: Members
Posts: 126
Joined: 21-February 06
Member No.: 18,973



I'm looking for some brainstorming help. The task is to create a program where the user enters two integers, and have them multiply without using the multiplication function. For example, if the user enters 5 and 10, the program is supposed to go 10+10+10+10+10, not 5*10. I have gotten as far getting input from the user. I am thinking of using a while loop for this. Does anyone have suggestions as to the best way to do this with the while statement? Thanks.
Go to the top of the page
 
+Quote Post
galexcd
post Sep 23 2007, 07:25 PM
Post #2


Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software.
***********

Group: [HOSTED]
Posts: 1,023
Joined: 25-September 05
From: L.A.
Member No.: 12,251



I think a for loop would be easier:

CODE
int total=0;
for(int i=0;i<firstnumber;i++) total+=secondnumber;
Go to the top of the page
 
+Quote Post
htdefiant
post Sep 23 2007, 07:34 PM
Post #3


Advanced Member
*******

Group: Members
Posts: 126
Joined: 21-February 06
Member No.: 18,973



I am not familiar with or loops. Perhaps you could describe the best method to use with a while loop?

Thanks for your help.
Go to the top of the page
 
+Quote Post
galexcd
post Sep 23 2007, 08:25 PM
Post #4


Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software.
***********

Group: [HOSTED]
Posts: 1,023
Joined: 25-September 05
From: L.A.
Member No.: 12,251



Sure it just takes a little bit more code for the same results:

CODE
int total=0;
int i=0;
while(i<firstnumber){
total+=secondnumber;
i++;
}
Go to the top of the page
 
+Quote Post
htdefiant
post Sep 23 2007, 08:38 PM
Post #5


Advanced Member
*******

Group: Members
Posts: 126
Joined: 21-February 06
Member No.: 18,973



Sorry to keep perpetuating this - you've been very helpful - what is that programming in C, not C++?

Thanks for you help,
A fellow OS X geek
Go to the top of the page
 
+Quote Post
galexcd
post Sep 23 2007, 08:49 PM
Post #6


Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software.
***********

Group: [HOSTED]
Posts: 1,023
Joined: 25-September 05
From: L.A.
Member No.: 12,251



It actually should work in both c and c++, and even java if you declare those variables inside a method. Most programming languages have extremely similar syntax.

This post has been edited by alex7h3pr0gr4m3r: Sep 23 2007, 08:52 PM
Go to the top of the page
 
+Quote Post
htdefiant
post Sep 23 2007, 10:27 PM
Post #7


Advanced Member
*******

Group: Members
Posts: 126
Joined: 21-February 06
Member No.: 18,973



Sorry Alex - I am very new at this. Here is what I have:
CODE
main ()

{
int num1, num2, num3;
printf("Please an integer:\n");
num1=GetInteger();
printf("Please a second integer:\n");
num2=GetInteger();
while (num2>1)
{
}
getchar ();
return 0;
}


How do I apply the code you gave me to this base?
Go to the top of the page
 
+Quote Post
galexcd
post Sep 24 2007, 02:45 AM
Post #8


Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software.
***********

Group: [HOSTED]
Posts: 1,023
Joined: 25-September 05
From: L.A.
Member No.: 12,251



try this... num3 is the result of num1 times num2
CODE
int num1, num2, num3=0;
     printf("Please an integer:\n");
     num1=GetInteger();
     printf("Please a second integer:\n");
     num2=GetInteger();
     int i=0;
     while(i<num1){
          num3+=num2;
          i++;
     }
     getchar ();
     return 0;
     }
Go to the top of the page
 
+Quote Post
htdefiant
post Sep 24 2007, 06:02 PM
Post #9


Advanced Member
*******

Group: Members
Posts: 126
Joined: 21-February 06
Member No.: 18,973



Thanks