| | |
(Noob) need some help w/ Prime Numbers
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2006
Posts: 3
Reputation:
Solved Threads: 0
I know there are tons of posts on this, I think I'm on the right track with this, but I can't quite get it to work. I can't figure out how to get the range of primes to print. I'd appreciate any input I can get, sorry about the indenting.
main() keeps doing the following, until user enters a 0 or a negative number:
• Ask user to enter a number;
• If it is positive, print all the prime numbers between 1 and it; when there’s no prime number, print “none�; ( A prime number is a whole number that can only be divided by 1 and itself, i.e., 2, 3, 5, 7, 11, 13, 17, 23, ….)
• Print the max of these prime numbers; or print “none� when there’s no prime.
The calling relation should be: main() calls printPrimes() and getMaxPrime();
printPrimes() and getMaxPrime() calls isPrime() .
My Code:
main() keeps doing the following, until user enters a 0 or a negative number:
• Ask user to enter a number;
• If it is positive, print all the prime numbers between 1 and it; when there’s no prime number, print “none�; ( A prime number is a whole number that can only be divided by 1 and itself, i.e., 2, 3, 5, 7, 11, 13, 17, 23, ….)
• Print the max of these prime numbers; or print “none� when there’s no prime.
The calling relation should be: main() calls printPrimes() and getMaxPrime();
printPrimes() and getMaxPrime() calls isPrime() .
My Code:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <math.h> using namespace std; /* function isPrime (int num) finds out whether the input integer num is a prime number or not */ bool isPrime (int num) { for (int fac = 2; fac <= num; fac++) if (num % fac == 0) return 0; return 1; } /* function printPrimes (int num) prints all the prime numbers between 1 and num */ void printPrimes (int num) { int number = 1; while(number <= num) { ++number; if (isPrime(number)) cout << number<<" "; } } /* function getMaxPrime(int num) returns the greatest prime number between 1 and num. if there is no prime number between 1 and num, getMaxPrime() should return a 0. */ int getMaxPrime (int num) { int maxCount = 0; for (int count = 1; count <= num; count++) { if (isPrime(count)) maxCount = count; } return int (maxCount); } int main () { int number; do { cout <<endl<<endl <<"Please enter a number, 0 or negative to stop :"; cin >> number; if (number > 0) { cout << "All the prime numbers between 1 and "<< number<<" are: "<< endl; printPrimes(number); cout << "The max prime is :" ; int maxPrime = getMaxPrime(number); if (maxPrime==0) cout <<"none."; else cout << maxPrime; } } while (number>0); return 0; }
i'm not sure if I understood the problem but try to replace with
and use this
instead your printPrimes
I didn't had the time to test it
C++ Syntax (Toggle Plain Text)
if (num % fac == 0)
C++ Syntax (Toggle Plain Text)
if ((num % fac == 0) && (num != fac))
and use this
C++ Syntax (Toggle Plain Text)
void printPrimes (int num) { int number = 2; while(number <= num) { if (isPrime(number)) cout << number<<" "; ++number; } }
I didn't had the time to test it
if you want between 1 and num (num not included) than
C++ Syntax (Toggle Plain Text)
/* lab3.cpp Working with Functions */ #include <iostream> #include <math.h> using namespace std; /* function isPrime (int num) finds out whether the input integer num is a prime number or not. If num is a prime, return true; else return false; A prime number is a number whose only factors are 1 and itself. Note: 0 and 1 are not prime numbers. The minimal prime number is 2. */ bool isPrime (int num) { for (int fac = 2; fac <= num; fac++) if ((num % fac == 0) && (num != fac)) return 0; return 1; } /* function printPrimes (int num) prints all the prime numbers between 1 and num */ void printPrimes (int num) { int number = 2; while(number < num) { if (isPrime(number)) cout << number<<" "; ++number; } } /* function getMaxPrime(int num) returns the greatest prime number between 1 and num. if there is no prime number between 1 and num, getMaxPrime() should return a 0. */ int getMaxPrime (int num) { int maxCount = 0; for (int count = 1; count < num; count++) { if (isPrime(count)) maxCount = count; } return int (maxCount); } int main () { int number; do { cout <<endl<<endl <<"Please enter a number, 0 or negative to stop :"; cin >> number; if (number > 0) { cout << "All the prime numbers between 1 and "<< number<<" are: "<< endl; printPrimes(number); cout << "The max prime is :" ; int maxPrime = getMaxPrime(number); if (maxPrime==0) cout <<"none."; else cout << maxPrime; } } while (number>0); return 0; }
•
•
Join Date: Mar 2006
Posts: 3
Reputation:
Solved Threads: 0
Finally got it working. I thought I'd post the code in case someone else has the same problem
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; /* function isPrime (int num) finds out whether the input integer num is a prime number or not. If num is a prime, return true; else return false; A prime number is a number whose only factors are 1 and itself. Note: 0 and 1 are not prime numbers. The minimal prime number is 2. */ bool isPrime (int num) { bool primeCheck = true; for (int fac = 2; fac < num; fac++ )//runs through factors between 2 and num { if (num % fac == 0) //checks to see if a number is divisible primeCheck = false; } return (primeCheck); //returns true or false to calling functions printPrimes and getMaxPrime. } /* function printPrimes (int num) prints all the prime numbers between 1 and num */ void printPrimes (int num) { bool print; if (num <= 1) cout << "none!"; // Determines if their are any primes in the range. for (int count = 2; count <= num; count++ ) { print = isPrime(count); //Creates "print" function to determine if a number is prime. if (print == true) { cout << count << ", "; // outputs prime number } } } /* function getMaxPrime(int num) returns the greatest prime number between 1 and num. if there is no prime number between 1 and num, getMaxPrime() should return a 0. */ int getMaxPrime (int num) { int maxPrime; int prime = 2; if (num <= 1) //sets maxPrime to "0" (none) { maxPrime = 0; prime = 0; } do //checks if a number is prime, sets maxPrime equal to found primes. { bool check = isPrime(prime); if (check == true) maxPrime = prime; //sets maxPrime to last prime number found in range. prime ++; }while (prime <= num); return (maxPrime); // returns the value of maxPrime to main function } int main () { int number; do { cout <<endl<<endl <<"Please enter a number, 0 or negative to stop :"; cin >> number; if (number > 0) { cout << "All the prime numbers between 1 and "<< number<<" are:"<< endl; printPrimes(number); cout << endl; cout << "The max prime is :" ; int maxPrime = getMaxPrime( number ); if (maxPrime<=1) cout <<"none."; else cout << maxPrime; } } while (number>0); return 0; }
Do you know what is the definition of the prime number? It is a number that has no other diveders other than one and itself. You tested the number to itself (every number devides it self, doesn't it?) so that is why the function always return false.
If the number isn't prime it must have deviders to (inclusively) its square root. Think about it, and say why.
If the number isn't prime it must have deviders to (inclusively) its square root. Think about it, and say why.
Revenage is a dish best served cold.
50|2|2Y 4 |34|) 3|\|6|_|5|-| ![]() |
Similar Threads
- Prime Numbers (C)
- prime numbers homework trouble (C++)
- help with array that determines prime numbers (C)
- prime numbers (C++)
- prime numbers (C++)
Other Threads in the C++ Forum
- Previous Thread: Installing the SDK for VC++ 2005???
- Next Thread: Why program works in Dev-C++ and not in VC++ 2005?
Views: 3256 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






