1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Wanted help in c++

  1. Sep 11, 2011 #1
    Wanted help in c++ !!

    Program to print the number of prime numbers between two user given numbers.

    MY ATTEMPT TO SOLUTION :
    I hv found how to print the prime numbers between two given numbers but couldn't print the number of prime numbers between two given numbers.

    #include<iostream.h>
    #include<conio.h>
    void main()
    {
    int n,x,i,j;
    cout<<"Enter the lower range";
    cin>>n;
    cout<<"Enter the upper range";
    cin>>x;
    for(i = n;i<=x;i++)
    {
    for(j = 2;j<=i-1;j++)
    {
    if(i % j !== 0)
    {
    cout<<i;
    cout<<"\n";
    }
    }
    }
    getch();
    }
     
  2. jcsd
  3. Sep 11, 2011 #2
    Re: Wanted help in c++ !!

    The code you posted just prints a number every time i is found to not have a specific factor.
    You still need to do something such as check that i % j !== 0 for all j. This could be done with a boolean external to the innermost loop.

    Once you have done this, you may use an integer external to the outer loop to count the number of primes.

    Also, iostream.h is not part of standard C++. Instead, please use

    Code (Text):
    #include <iostream>
     
  4. Sep 13, 2011 #3
    Re: Wanted help in c++ !!

    I doubt that this code you presented will even compile. A few comments on the coding (since the problem with the algorithm has already been mentioned):
    - I don't think I have ever seen the operator "!==". Are you sure you don't mean "!=" ?
    - cout<<"\n" can (and should) in many cases be replaced with cout<<endl. "cout<<i; cout<<"\n";" can be combined in a single line "cout<<i<<endl;".
    - You should add the line "using namespace std;" before the start of the main routine.
    - I made it a habit to give my variables sensible names, a habit that I could also convince my students of (by simply not giving points for parts of the code that I don't understand :P). Sensible variable names would be n->lower, x->upper, i->candidate, j->divisor. That should make your code much more readable.
    - Another related good habit is to define variables as local as possible. In c++ you do not need to specify all variables being used at the start of the routine (you had to do that in C). Rather than writing
    Code (Text):

    int i;
    ...
    for(i=lower; i<=upper; i+=1) { ...
     
    you can write
    Code (Text):

    for(int i=lower; i<=upper; i+=1) { ...
     
    Apart from being shorter it also improves the readability of code overall.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Wanted help in c++
  1. C++ help (Replies: 10)

  2. C\C++ help needed (Replies: 2)

  3. Help with C++ strings (Replies: 3)

Loading...