Finding multiples of 3 under 1000.

Feb 17, 2011 at 7:01pm
Hey fellas, brand new programmer here! I'm excited to learn and I'm having a blast. Would like some advice from the experienced dudes here. I'm trying to make a program that displays all the multiples of three under 1000 only. I'm close, but something is iffy:


#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int x, i, out;
x = 3;
out = 0;
i = 0;
while (out <= 1000) {
i++;
out= i *= x;
cout << out << endl; }


system ("pause") ;
return EXIT_SUCCESS;
}

What I see in the console is this:

3
12
39
120
363
1092
Press any key to continue......

Obviously I'm on the right page, thing is, they're all multiples of 3, but where's 6, 9, 21, etc...? I think it's my logic more than anything but feel free to critique me about anything.
Feb 17, 2011 at 7:10pm
This is a good time to use a for loop. If you don't know how to use them, then here's a link:
http://www.cplusplus.com/doc/tutorial/control/
A hint:
You could do this using only one variable.

Also, avoid using system("pause"), use something else such as cin.get().
You don't need main to accept arguments in this program. Just use int main()

Finally, please use [code][/code] tags.

Edit: See if you can get it down to 9-12 lines. This is about how long the proper solution will be.
Last edited on Feb 17, 2011 at 7:37pm
Feb 17, 2011 at 7:33pm
If a multiple of three is anything that is divisible by three, why are you multiplying? Just add three.

I think there might be a problem with order of operations at line 14.
You could do the whole thing with just your out integer like Browni3141 said.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int x, i, out;
x = 3;
out = 0;
i = 0;
while (out <= 1000) {
i++;
out= i *= x;
cout << out << endl; }


system ("pause") ;
return EXIT_SUCCESS;
}

Feb 17, 2011 at 7:36pm
1
2
for (int i = 3; i <= 1000; i += 3)
  cout << i << endl;
Feb 17, 2011 at 7:41pm
@Zaita:
Why did you just post a solution like that? To you and me it may be exetremely trivial, but to the OP it is something he still needs to learn.
It's probably not a big deal with such a small assignment though, and it should be i < 1000, the OP said numbers UNDER 1000.
Feb 17, 2011 at 7:50pm
0 is a multiple of 3
Feb 17, 2011 at 7:59pm
@ne555: If you want to be like that then -3, -15 and -1234567890123456789329825541 are also multiples of three, and they're under 1000.
Feb 17, 2011 at 8:13pm
Yeah, guess I didn't specify positive integers only. And as far a Zaita's answer goes, it proves it's all in my logic rather than my (small amount of) knowledge of C++. All this makes total sense now, I don't understand why I made something so simple so complicated lol. Basic concept is, if you start at 3 and just keep adding three every loop, all of those numbers each loop will be multiples of 3 DUH!!! haha Thanks everyone for taking my question seriously, it means a lot!
Last edited on Feb 17, 2011 at 8:22pm
Feb 17, 2011 at 8:21pm
the console you have probably only lets you see so many lines. and the ones above 105 it doesnt show, if you edit the way they print out (maybe 2 per line rather than 1 per line) it should fix it.
Feb 17, 2011 at 8:23pm
I hadn't thought about printing more than one item on a line before, probably cause I've never ran into this problem before lol

How to do that is beyond me though.
Last edited on Feb 17, 2011 at 8:24pm
Feb 17, 2011 at 8:59pm
you could always scrap the endl at the end and put

1
2
3

cout << i << " ";


that will print out 3 6 9 12 15 etc all on one line, but for class purposes the teacher should be fine with your solution, since based on the information you gave the code is right.

Feb 17, 2011 at 9:33pm
Why not do something silly!
1
2
3
4
for (int i = 0; 0x3*i<0x3E8; ++i)
{
   cout << 0x3*i << endl;
}
Feb 17, 2011 at 10:50pm
@Browni3141: Typically I'm against posting whole solutions to assignments etc, but this doesn't really qualify as the answer is a basic use of a for-loop. I also wanted to avoid this thread getting polluted with solutions that are not ideal or helpful and over-complicate what has been asked. Danaigo's solution, while still accurate is keeping with the OP's over-complicated approach.

There is no harm in providing a solution when it's a basic use of a single conditional statement or iterative statement.
Topic archived. No new replies allowed.