By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,415 Members | 1,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,415 IT Pros & Developers. It's quick & easy.

C++ assistance

P: 1
Prime Numbers Between 1-7000, and the sub intervals.(1-1000,1001-2000,2001-3000.....)
Printing of the Histogram showing the number of primes
1-1000 ****************
1002 - 2000 **********



This is how I started but I cant continue...pleease..


#include<stdio.h>
#include<conio.h>
#include <iostream>
#include <cmath>

void prime_num(int);

int main()
{



printf(" 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 )
{
printf(i endl);
}
}
}

I am getting errors ...still correcting them....I need help on this
Oct 4 '06 #1
Share this Question
Share on Google+
1 Reply


Banfa
Expert Mod 5K+
P: 8,916
basically you are suffering from a few minor syntax errors

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include <iostream>
  4. #include <cmath>
  5.  
  6. void prime_num(int);
  7. /* No using namespace std so cin and cout don't work */
  8.  
  9. int main()
  10. {
  11.  
  12.  
  13.  
  14. printf(" Enter a number and I will generate the prime numbers up to that number: ");
  15. int num = 0;
  16. /* ) instead of >> */
  17. cin ) num;
  18.  
  19. prime_num(num);
  20. }
  21.  
  22.  
  23.  
  24. void prime_num( int num)
  25. {
  26. int check_prime = 0;
  27.  
  28. for ( int i = 0; i <= num; i++)
  29. {
  30. check_prime = 1;
  31.  
  32. /* i/2 is a reasonable limit to start with but you can procude a 
  33.    faster algorithm, especially for larger numbers, if you use a 
  34.    dynamic upper limit of i/j */
  35. for ( int j = 2; j <= i/2; j++)
  36. {
  37. /* As soon as this condition is true and you set check_prime to 0 
  38.    you can end the inner loop with a break statement.  If you can't use break
  39.    put an extra condition in the for loop based on check_prime. */
  40. if ( i % j == 0 )
  41.  
  42. check_prime = 0;
  43.  
  44. /* This if statement should be outside the inner for loop */
  45. if ( check_prime != 0 )
  46. {
  47. /* you have mixed printf and cout formats here */
  48. printf(i  endl);
  49. }
  50. }
  51. }
  52.  
Oct 4 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.