Please support our C++ advertiser: Programming Forums
Views: 55218 | Replies: 13 | Solved
![]() |
Below is my code for printing prime numbers up to a given input by user. However, my calculations are not working correctly and my output is printing twice. Any idea?
#include <iostream>
#include <cmath>
using namespace std;
void prime_num(int);
int main()
{
cout << " Enter a number and I will generate the prime numbers up to that number: ";
int num = 0;
cin >> num;
prime_num(num);
}
void prime_num( int num)
{
int check_prime = 0;
for ( int i = 0; i <= num; i++)
{
check_prime = 1;
for ( int j = 2; j <= i/2; j++)
{
if ( i % j == 0 )
check_prime = 0;
if ( check_prime != 0 )
{
cout << i << endl;
}
}
}
}
#include <iostream>
#include <cmath>
using namespace std;
void prime_num(int);
int main()
{
cout << " Enter a number and I will generate the prime numbers up to that number: ";
int num = 0;
cin >> num;
prime_num(num);
}
void prime_num( int num)
{
int check_prime = 0;
for ( int i = 0; i <= num; i++)
{
check_prime = 1;
for ( int j = 2; j <= i/2; j++)
{
if ( i % j == 0 )
check_prime = 0;
if ( check_prime != 0 )
{
cout << i << endl;
}
}
}
}
Here's what I went with. Note you want to do a break if you find out it's divisible as there is no point in checking all the numbers beyond it for divisibility..
Enter a number and I will generate the prime numbers up to that number: 100
Prime:1
Prime:2
Prime:3
Prime:5
Prime:7
Prime:11
Prime:13
Prime:17
Prime:19
Prime:23
Prime:29
Prime:31
Prime:37
Prime:41
Prime:43
Prime:47
Prime:53
Prime:59
Prime:61
Prime:67
Prime:71
Prime:73
Prime:79
Prime:83
Prime:89
Prime:97
Note you can check your results here:
http://www.utm.edu/research/primes/lists/small/1000.txt
#include <iostream>
using namespace std;
void prime_num(int);
int main()
{
cout << " Enter a number and I will generate the prime numbers up to that number: ";
int num = 0;
cin >> num;
prime_num(num);
}
void prime_num( int num)
{
bool isPrime=true;
for ( int i = 0; i <= num; i++)
{
for ( int j = 2; j <= num; j++)
{
if ( i!=j && i % j == 0 )
{
isPrime=false;
break;
}
}
if (isPrime)
{
cout <<"Prime:"<< i << endl;
}
isPrime=true;
}
}
Prime:1
Prime:2
Prime:3
Prime:5
Prime:7
Prime:11
Prime:13
Prime:17
Prime:19
Prime:23
Prime:29
Prime:31
Prime:37
Prime:41
Prime:43
Prime:47
Prime:53
Prime:59
Prime:61
Prime:67
Prime:71
Prime:73
Prime:79
Prime:83
Prime:89
Prime:97
Note you can check your results here:
http://www.utm.edu/research/primes/lists/small/1000.txt
•
•
Join Date: Jun 2005
Posts: 60
Reputation:
Rep Power: 4
Solved Threads: 5
It is printing the number multiple times, becuse of:
here you are printing out the number when check prime != 0.
but untill i%j == 0 the program are going to ouput that the number is prime, even when it aint.
this is bad design, and I think you need to rewrite your function.
check out: http://www.daniweb.com/techtalkforum...064-prime.html
for ( int j = 2; j <= i/2; j++)
{
if ( i % j == 0 )
{
check_prime = 0;
}
if ( check_prime != 0 )
{
cout << i << endl;
}
}but untill i%j == 0 the program are going to ouput that the number is prime, even when it aint.
this is bad design, and I think you need to rewrite your function.
check out: http://www.daniweb.com/techtalkforum...064-prime.html
•
•
•
•
Originally Posted by winbatch
Here's what I went with. Note you want to do a break if you find out it's divisible as there is no point in checking all the numbers beyond it for divisibility..
Enter a number and I will generate the prime numbers up to that number: 100#include <iostream> using namespace std; void prime_num(int); int main() { cout << " Enter a number and I will generate the prime numbers up to that number: "; int num = 0; cin >> num; prime_num(num); } void prime_num( int num) { bool isPrime=true; for ( int i = 0; i <= num; i++) { for ( int j = 2; j <= num; j++) { if ( i!=j && i % j == 0 ) { isPrime=false; break; } } if (isPrime) { cout <<"Prime:"<< i << endl; } isPrime=true; } }
Prime:1
Prime:2
Prime:3
Prime:5
Prime:7
Prime:11
Prime:13
Prime:17
Prime:19
Prime:23
Prime:29
Prime:31
Prime:37
Prime:41
Prime:43
Prime:47
Prime:53
Prime:59
Prime:61
Prime:67
Prime:71
Prime:73
Prime:79
Prime:83
Prime:89
Prime:97
Note you can check your results here:
http://www.utm.edu/research/primes/lists/small/1000.txt
So Picky
. As you wish:
Enter a number and I will generate the prime numbers up to that number: 150
Prime:2
Prime:3
Prime:5
Prime:7
Prime:11
Prime:13
Prime:17
Prime:19
Prime:23
Prime:29
Prime:31
Prime:37
Prime:41
Prime:43
Prime:47
Prime:53
Prime:59
Prime:61
Prime:67
Prime:71
Prime:73
Prime:79
Prime:83
Prime:89
Prime:97
Prime:101
Prime:103
Prime:107
Prime:109
Prime:113
Prime:127
Prime:131
Prime:137
Prime:139
Prime:149
. As you wish:#include <iostream>
using namespace std;
void prime_num(int);
int main()
{
cout << " Enter a number and I will generate the prime numbers up to that number: ";
int num = 0;
cin >> num;
prime_num(num);
}
void prime_num( int num)
{
bool isPrime=true;
for ( int i = 2; i <= num; i++)
{
for ( int j = 2; j <i; j++)
{
if ( i % j == 0 )
{
isPrime=false;
break;
}
}
if (isPrime)
{
cout <<"Prime:"<< i << endl;
}
isPrime=true;
}
}
Prime:2
Prime:3
Prime:5
Prime:7
Prime:11
Prime:13
Prime:17
Prime:19
Prime:23
Prime:29
Prime:31
Prime:37
Prime:41
Prime:43
Prime:47
Prime:53
Prime:59
Prime:61
Prime:67
Prime:71
Prime:73
Prime:79
Prime:83
Prime:89
Prime:97
Prime:101
Prime:103
Prime:107
Prime:109
Prime:113
Prime:127
Prime:131
Prime:137
Prime:139
Prime:149
•
•
•
•
Originally Posted by winbatch
So Picky. As you wish:
Enter a number and I will generate the prime numbers up to that number: 150#include <iostream> using namespace std; void prime_num(int); int main() { cout << " Enter a number and I will generate the prime numbers up to that number: "; int num = 0; cin >> num; prime_num(num); } void prime_num( int num) { bool isPrime=true; for ( int i = 2; i <= num; i++) { for ( int j = 2; j <i; j++) { if ( i % j == 0 ) { isPrime=false; break; } } if (isPrime) { cout <<"Prime:"<< i << endl; } isPrime=true; } }
Prime:2
Prime:3
Prime:5
Prime:7
Prime:11
Prime:13
Prime:17
Prime:19
Prime:23
Prime:29
Prime:31
Prime:37
Prime:41
Prime:43
Prime:47
Prime:53
Prime:59
Prime:61
Prime:67
Prime:71
Prime:73
Prime:79
Prime:83
Prime:89
Prime:97
Prime:101
Prime:103
Prime:107
Prime:109
Prime:113
Prime:127
Prime:131
Prime:137
Prime:139
Prime:149
its funny you can sit in front of this screen and try to debug for hours and once you figure it out it was something small to begin with. Very frustrating.
•
•
Join Date: Sep 2005
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 1
Hello My friend, I do not know if my response will be in time for you but here is a master code for finding all prime numbers up to any number 'x' that you input...
// In you need any explanation for any parts of this program send me a message and i will reply as soon as i can...
# include <cmath> // This library enable the use of sqrt.
# include <iostream>
using namespace std;
void primenum(long double); // Prototype...
int c = 0;
int main()
{
long double x = 0;
cout<<"\n This program will generate all prime numbers up to the"
<<"\n number you have entered below...\n";
cout<<"\n Please enter a number: ";
cin>> x;
cout<<"\n Here are all the prime numbers up to "<<x<<".\n";
primenum(x); //function invocation...
cout<<endl<<"\nThere are "<<c
<<" prime numbers less than or equal to "<<x<<".\n\n";
return 0;
}
// This function will determine the primenumbers up to num.
void primenum(long double x)
{
bool prime = true; // Calculates the square-root of 'x'
int number2;
number2 =(int) floor (sqrt (x));
for (int i = 1; i <= x; i++){
for ( int j = 2; j <= number2; j++){
if ( i!=j && i % j == 0 ){
prime = false;
break;
}
}
if (prime){
cout <<" "<<i<<" ";
c += 1;
}
prime = true;
}
}// In you need any explanation for any parts of this program send me a message and i will reply as soon as i can...
Last edited by Dave Sinkula : Sep 4th, 2005 at 12:07 pm. Reason: Added [code][/code] tags.
•
•
Join Date: Mar 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 1
•
•
•
•
Hello My friend, I do not know if my response will be in time for you but here is a master code for finding all prime numbers up to any number 'x' that you input...
# include <cmath> // This library enable the use of sqrt. # include <iostream> using namespace std; void primenum(long double); // Prototype... int c = 0; int main() { long double x = 0; cout<<"\n This program will generate all prime numbers up to the" <<"\n number you have entered below...\n"; cout<<"\n Please enter a number: "; cin>> x; cout<<"\n Here are all the prime numbers up to "<<x<<".\n"; primenum(x); //function invocation... cout<<endl<<"\nThere are "<<c <<" prime numbers less than or equal to "<<x<<".\n\n"; return 0; } // This function will determine the primenumbers up to num. void primenum(long double x) { bool prime = true; // Calculates the square-root of 'x' int number2; number2 =(int) floor (sqrt (x)); for (int i = 1; i <= x; i++){ for ( int j = 2; j <= number2; j++){ if ( i!=j && i % j == 0 ){ prime = false; break; } } if (prime){ cout <<" "<<i<<" "; c += 1; } prime = true; } }
// In you need any explanation for any parts of this program send me a message and i will reply as soon as i can...
very very very nice programmmm......
and thanks for this .....
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 4 (0 members and 4 guests)





Linear Mode