Multiplication Via Addition

Pages: 1, 2
free web hosting
Open Discussion > CONTRIBUTE > Computers > Programming Languages > C/C++ Programming

Multiplication Via Addition

htdefiant
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.

Reply

galexcd
I think a for loop would be easier:

CODE
int total=0;
for(int i=0;i<firstnumber;i++) total+=secondnumber;

Reply

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

Thanks for your help.

Reply

galexcd
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++;
}

Reply

htdefiant
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

Reply

galexcd
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.

Reply

htdefiant
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?

Reply

galexcd
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;
     }

Reply

htdefiant
Thanks

Reply

galexcd
QUOTE(htdefiant @ Sep 24 2007, 11:02 AM) *
Thanks


Everything works how it should? No more problems? If so great and good luck with the rest of the project.

Feel free to PM me with any questions you still may have!

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Pages: 1, 2
Similar Topics

Keywords : multiplication addition


    Looking for multiplication, addition

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for multiplication, addition

*MORE FROM TRAP17.COM*
advertisement



Multiplication Via Addition



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE