Jump to content
xisto Community
• Sign Up

# Multiplication Via Addition

## Recommended Posts

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.

#### Share this post

##### Share on other sites

I think a for loop would be easier:

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

#### Share this post

##### Share on other sites

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

#### Share this post

##### Share on other sites

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

int total=0;int i=0;while(i<firstnumber){total+=secondnumber;i++;}

#### Share this post

##### Share on other sites

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

#### Share this post

##### Share on other sites

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.

Edited by alex7h3pr0gr4m3r (see edit history)

#### Share this post

##### Share on other sites

Sorry Alex - I am very new at this. Here is what I have:

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?

#### Share this post

##### Share on other sites

try this... num3 is the result of num1 times num2

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

Thanks

#### Share this post

##### Share on other sites

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!

#### Share this post

##### Share on other sites

Nope, no prolems.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

## Sign in

Already have an account? Sign in here.

Sign In Now

×

• #### Activity

• Leaderboard
×
• Create New...

## Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.