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;
}
}
}
}
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..
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;
}
}
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
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.
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..
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;
}
}
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
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;
}
}
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
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;
}
}
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
thanks, that is correct.
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.
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...
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...
Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.
This thread is more than three months old
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.