Thread: upper prime - lower prime

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    6

    upper prime - lower prime

    I wrote something but ı have to enter 2 times. also ı want to add cod next prime number - previous prime number I tried a lot, but I couldn't.


    Code:
    #include <iostream>
     
    using namespace std;
     
    int main()
    {
        int i,j=2,number,count,num,x;
      
        cout<<"Enter number ";
        cin>>number;
        cin>>num;
        for(i=number+1;i<3000;i++)
        {
            for(j=2;j <i;j++)
            {
                if(i %j==0)
                {
                    break;
                } 
            } 
            if(i==j || i==1)
            {
                cout<<"\t"<<i;
                break;
            } 
        } 
        for( int a=num-1;a>=1;a--)
      {
       for(int b=2;b<a;b++)
          {
           if(a%b==0)
           count++;
           }
           if(count==0)
           {
          if(a==1)
           {
           cout<<"no prime number less than 2";
           break;
         }
          cout<<a<<" last prime and next prime number before entered number"<<endl;
           break;
           }
           count=0;
    }
    return 0 ;
    }
    

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    "I wrote something" is terribly vague.

    You have to enter two times because you request for input twice. If you don't want that, you need to figure out why you are doing that: maybe you can copy the value to another variable instead.

    Having variables named num and number is confusing. Choose one, make the other more descriptive.

    You need to comment your code, or at least move parts of it into functions that have descriptive names.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-02-2014, 10:11 AM
  2. convert from upper to lower
    By tost in forum C Programming
    Replies: 18
    Last Post: 02-05-2013, 01:56 AM
  3. Replies: 1
    Last Post: 03-16-2012, 02:07 AM
  4. Lower to Upper
    By Krush in forum C Programming
    Replies: 13
    Last Post: 11-19-2002, 10:14 PM
  5. lower to upper
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 07-29-2002, 07:50 PM

Tags for this Thread