Please support our C++ advertiser: Programming Forums
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;
}
}
}
}
Similar Threads
Other Threads in the C++ Forum
Other Threads in the C++ Forum
- Previous Thread: Car dealer system
- Next Thread: Yes/No program
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Threaded Mode