View Full Version : *simple* Program to display all prime numbers from 1 to 100
any idea why this doesnt work??
im new to C++, any help would be GREATLY appreciated.
thanks
// Lab2.cpp : Defines the entry point for the console application.
//
#include <iostream>
using namespace std;
int main()
{
bool isPrime;
for (int number=1; number<=100; number++)
if ((number % 2 == 1) || (2 % 2 == 0))
{
isPrime = true;
}
else
isPrime = false;
cout << isPrime << " ";
getchar();
return 0;
}
the program doesnt seem to run how id like it to. can anybody see the errors?
One real quick Style thing i see is the for loop doesn't have "{ }"s but I just like those,
Check you if ((number % 2 == 1) || (2 % 2 == 0)) though ... the (2%2==0) will always be true so every one of 100 times you hit this statement it'll be true (saying all your numbers between 1 and 100 are prime). As for the (number%2 == 1) it'll be true for every even number you have (making the isPrime to be true for 2,4,6,8,10,...)
to really test if it's prime you need to test it with every prime number before it
for 2 you would check nothing to see if it's divisible (1 is neither prime nor composite)
for 5 you would only test 3 and 2 to see if it's divisible
for 13 you would only test 11,7,5,3 and 2 to see if it is divisible (you get the point)
and if it is divisible you would set it to false not true (you make isPrime true if it's even or always).
I'll let you figure it out from there
Hope this helped
God bless
-Gryfang
Wyatt_Earp
05-26-04, 08:52
gryfang is correct. One thing I'd add to make it a little simpler. Don't worry about testing 5 with *only* 3 and 2, or 13 with *only* 11, 7, 5, 3 and 2. Just test each number with every number before it excluding 1 and 0 (obviously). *Hint* - I'd say a nested for loop would be the best way. :)
i.e.
for 2 - test nothing
for 3 - test it with 2
for 4 - test it with 3 and 2
for 5 - test it with 4, 3, and 2
for 6 - test it with 5, 4, 3, and 2
for 7 - test it with 6, 5, 4, 3, and 2
...
for 100 - test it with 99, 98, 97, etc.
Then, you can do the output however you want. I'd suggest doing something like this:
-----------------------------------
Prime Numbers
-----------------------------------
1, 2, 3, 5, 7,...
Monkey_Junk
06-26-04, 22:26
#include <bitset>
then in your function
const int size = 1024; // or whatever number you choose.
int value;
bitset <size> sieve;
sieve.flip ();
int finalBit = sqrt (sieve.size () ) + 1;
for (int i = 2; i < finalBit; ++i)
{
if (sieve.test ( i ) )
sieve.reset ( j );
}
This will display all the prime numbers from 2 to 1023.
jonathan03
06-30-04, 19:30
I noticed that you had a few posts about programming in C++. Are you using C++ 6 or VC++ 7? If so, your in luck. If you put in break points on the lines you are unsure of it will help you a lot. Breaks will pause the program execution and show you what values are in all your variables. To put in a break press F9 in C++ 6. I'm not sure about VB.net but I'm sure it won't be hard to find. Look under debug and there is proably a debug toolbar. Colleges will only teach you the syntax, rarely the IDE. Breaks will help you a lot if you get stuck. What college do you go to and what are you majoring in?
I noticed that you had a few posts about programming in C++. Are you using C++ 6 or VC++ 7? If so, your in luck. If you put in break points on the lines you are unsure of it will help you a lot. Breaks will pause the program execution and show you what values are in all your variables. To put in a break press F9 in C++ 6. I'm not sure about VB.net but I'm sure it won't be hard to find. Look under debug and there is proably a debug toolbar. Colleges will only teach you the syntax, rarely the IDE. Breaks will help you a lot if you get stuck. What college do you go to and what are you majoring in?
thanks for the tip concerning breaks, i am using Microsoft Visual Studio C++ 6.0.
right now im attending a local college in Vancouver, studying under the applied science program. i hope to major in civil engineering when university starts. just realizing now that the decision to do so, is going to really kill my party life !
vBulletin® v3.8.3, Copyright ©2000-2010, Jelsoft Enterprises Ltd.