C++ Help
-
i'm trying to design a program that prompts the user for an integer adn then prints out all prime numbers up to that integer. i'm a beginner and i'm kind of stumped can anyone help j pena
-
i'm trying to design a program that prompts the user for an integer adn then prints out all prime numbers up to that integer. i'm a beginner and i'm kind of stumped can anyone help j pena
#include <iostream>
bool IsPrime (unsigned int); // I don't know how you are going to check that, there are several ways to do that.
int main(int argc, char* argv[])
{
unsigned int max;
cout << "Give an integer number: "; cout.flush;
cin >> max;
cout << endl;for (unsigned int n = 1; n <= max; n++) if (IsPrime(n)) { cout << n << ", "; cout.flush; } cout << endl; return 0;
}
-
i'm trying to design a program that prompts the user for an integer adn then prints out all prime numbers up to that integer. i'm a beginner and i'm kind of stumped can anyone help j pena
herez one way of doing it.....;) #include #include void main(void) { int number; BOOL flag=0; clrscr(); cout<<"Enter the number : "; cin>>number; cout<<"The numbers are :"\n"; for(int j=1;j<=number;j++) { flag=0;//reset the flag..... for(int i=1;i
-
i'm trying to design a program that prompts the user for an integer adn then prints out all prime numbers up to that integer. i'm a beginner and i'm kind of stumped can anyone help j pena
Looks like homework to me, so I won't give you answers. I don't like the ones others have given, though they will work. (I even have a book someplace that tells how to check if a number is prime, but it fails on Newton primes - I can't recall exactly what they are called) What I would do, and what I suspect your professor wants: create a linked list of all known primes, up to the current number. This list starts empty. You start by checking the lowest unknown number against the list. If anything in the list divides your number it is not prime. If the number is prime place it in the list. Either way move to the next highest number. If this isn't enough, google will reveal several C programs that do the same thing. Don't bother handing them in though. Professors are fairly good at spotting those programs even after students hide the origions. Besides, I might get a job with you latter, and I'll want you to have done the work then.
-
#include <iostream>
bool IsPrime (unsigned int); // I don't know how you are going to check that, there are several ways to do that.
int main(int argc, char* argv[])
{
unsigned int max;
cout << "Give an integer number: "; cout.flush;
cin >> max;
cout << endl;for (unsigned int n = 1; n <= max; n++) if (IsPrime(n)) { cout << n << ", "; cout.flush; } cout << endl; return 0;
}
-
DaFrawg wrote: I don't know how you are going to check that, there are several ways to do that then why did u posted this ? redindian