# Thread: Clueless on an Assignment

1. ## Clueless on an Assignment

My assignment is to do the following:

Write a program that executes multiplication of two integers entered by the user , by
So, for example if the inputs are 3 and 4, the program will execute 3 times addition of
4: 4+4+4.

I have to do it using the "while" function. I understand the basic concept of the while function but I have no clue how to do this. Please help me understand it better.

2. Well, if you understand the basic function of a while loop why don't you throw some code together. Go ahead and make an educated guess and post some code. We will go from there.

3. while isn't a function, it is a loop construct.

Do it as you'd do it by hand...suppose....( m x n ):
Count from 1 to n or 0 to n-1.
Each time add m to the sum, which was initially 0.

4. Originally Posted by AndrewHunter
Well, if you understand the basic function of a while loop why don't you throw some code together. Go ahead and make an educated guess and post some code. We will go from there.

Code:
```#include <stdio.h>
#include "genlib.h"
#include "simpio.h"

main()
{
int num1, num2, sum;

printf ("Enter two numbers to multiply: ");
num1 = GetInteger();
num2 = GetInteger();
sum = 0;
while (sum < num1 * num2)
{
sum = + num1 + num1;
}
printf ("product = %d.\n", sum);
getchar();
}```
There you are, Andrew. I had this before I asked the question and it wasn't working. I've tried everything that I could think of. That is why I asked the question.

5. Originally Posted by PYROMANIAC702
.......
There you are, Andrew. I had this before I asked the question and it wasn't working. I've tried everything that I could think of. That is why I asked the question.
Hmm.....good way to get help.

6. Try it with a for loop.

7. Originally Posted by AndrewHunter
Hmm.....good way to get help.
NO! I wasn't being rude in any way. I'm sorry if i offended you, but I was just pointing out that I tried it before I asked. Please help me.

8. Code:
` sum = ???? + num1 + num1;`

9. You could try this while loop:

Code:
```while(counter<=num1)
{
sum += num2;
}```
I don't know if this works,,

10. Originally Posted by jannr
I don't know if this works,,
I know it doesn't work, since you never change counter or num1 inside your loop.

Quzah.

11. Originally Posted by quzah
I know it doesn't work, since you never change counter or num1 inside your loop.

Quzah.
lol,, I forgot,, haha sorry,,

Code:
```while(counter<=num1)
{
sum += num2;
counter++;
}```
I guess this is right,, ^^

12. Originally Posted by PYROMANIAC702
Code:
```#include <stdio.h>
#include "genlib.h"
#include "simpio.h"

main()
{
int num1, num2, sum;

printf ("Enter two numbers to multiply: ");
num1 = GetInteger();
num2 = GetInteger();
sum = 0;
while (sum < num1 * num2)
{
sum = + num1 + num1;
}
printf ("product = %d.\n", sum);
getchar();
}```
There you are, Andrew. I had this before I asked the question and it wasn't working. I've tried everything that I could think of. That is why I asked the question.

You have a start value... you have a counter... how can you use them to construct a simple loop?

Code:
```int x = 234;  // multiply
int y = 37;  // by
int z = 0;   // answer

while (y--)
z += x;```

13. Originally Posted by jannr
lol,, I forgot,, haha sorry,,

Code:
```while(counter<=num1)
{
sum += num2;
counter++;
}```
I guess this is right,, ^^
actually it should be like that
Code:
```sum = 0;
counter = 1;
while(counter<=num1)
{
sum += num2;
counter++;
}```