GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Apr-2007, 07:59
allenfanwenyuan allenfanwenyuan is offline
Junior Member
 
Join Date: Mar 2007
Posts: 38
allenfanwenyuan is an unknown quantity at this point

Need help for simple prime number question


The question is to Write a C++ program that takes an integer n and displays the first prime number after n.
Your program should output n itself in case n is prime.
Sample 1:
Enter an integer: 17
17
Press any key to continue . . .

Sample 2:
Enter an integer: 63
67
Press any key to continue . . .

Sample 3:
Enter an integer: 12345678
12345701
Press any key to continue . . .

my code is
CPP / C++ / C Code:
void primeNo(int a)
{
    int o=0;
    for (int i=2;i<=a;i++)
    {
        if(a%i==0)
        {
                  ++o;
        }
                  }
                  if(o==1)
                  cout<<a<<endl;
                  

                  }

int main()
{
    int n;
    cout << "Enter an integer: " << flush;
    cin >> n;
    primeNo(n);
    
    system("pause");
    return 0;
}

I can test and generate if input is a prime number ,but get stuck with how to generate the first prime number after input which is not a prime number.
plz for help!!!
Last edited by LuciWiz : 04-Apr-2007 at 12:47. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 04-Apr-2007, 09:45
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: need help for simple prime number question


Quote:
Originally Posted by allenfanwenyuan
The question is to Write a C++ program that takes an integer n and displays the first prime number after n.

I can test and generate if input is a prime number ,but get stuck with how to generate the first prime number after input which is not a prime number.
plz for help!!!

You could try something like this:

Make a boolean function that tests whether its argument is a prime number. The function doesn't print anything out; it returns "true" if the argument is a prime number, and "false" if not:

CPP / C++ / C Code:
bool IsPrimeNo(int a)
{
 // test to see whether "a" is prime
 // if it is: then return "true"
 // else:  return "false"
}

Then your main program can look something like this:

Code:
BEGIN MAIN PROGRAM Get value of <n> from user. IF (IsPrimeNo(n)) THEN print message: <n> " is a prime number" ELSE print message: <n> " is not a prime number" Make a loop that increments <n> and tests to see whether it is a prime number. Exit the loop when it is. print message: <n> " is the next larger prime number" END IF END MAIN PROGRAM

As a matter of style, I made my output statements a little more verbose than your assignment. I suggest that you make your output exactly as the assignment requires.

A few runs for mine might look like:

Code:
Enter an integer: 37 37 is a prime number Enter an integer: 99 99 is not a prime number 101 is the next larger prime number Enter an integer: 129 129 is not a prime number 131 is the next larger prime number

Regards,

Dave
Last edited by davekw7x : 04-Apr-2007 at 10:38.
  #3  
Old 04-Apr-2007, 11:40
davis
 
Posts: n/a

Re: need help for simple prime number question


You may want to rethink your algorithm somewhat, but here is a very simple example of what I think you're trying to accomplish:

CPP / C++ / C Code:
#include <iostream>
#include <cmath>

using namespace std;

bool IsPrimeNumber( int number )
{
    bool bIsNotPrime = false;
    if( number > 2 )
    {
        for( int i = 2; i <= sqrt( number ); i++ )
        {
            if( number % i == 0 )
            {
                bIsNotPrime = true;
                break;
            }
        }
    }
    else
    {
        switch( number )
        {
        case 2:
            bIsNotPrime = false;
            break;
        default:
            bIsNotPrime = true;
            break;
        }
    }
    return !bIsNotPrime;
}

int main()
{
    cout << "Enter an integer: ";
    int number;
    cin >> number;

    bool bIsPrime = false;
    while( !bIsPrime )
    {
        if( (bIsPrime = IsPrimeNumber( number )) )
        {
            cout << number << endl;
        }
        else
        {
            ++number;
        }
    }
    return 0;
}

Output:

Code:
Enter an integer: 7 7 Enter an integer: 8 11 Enter an integer: 13 13 Enter an integer: 27 29


:davis:
  #4  
Old 04-Apr-2007, 18:10
allenfanwenyuan allenfanwenyuan is offline
Junior Member
 
Join Date: Mar 2007
Posts: 38
allenfanwenyuan is an unknown quantity at this point

Re: need help for simple prime number question


cheers ,guys .....i am fully understand that ....!many thanks
 
 

Recent GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
i have a question about the number of players you can have when hosting games. brandon1482 Computer Software Forum - Games 1 24-Feb-2007 13:08
Converting a number amount to text Godzilla CPP / C++ Forum 5 31-Mar-2006 11:38
Program Needed: Perfect Number Program in C language koool_kid C Programming Language 12 02-Dec-2005 14:02
Anyone can write a program code for this??? chriskan76 C Programming Language 1 19-Oct-2004 20:25
Help w/ prime # determination crystalattice C Programming Language 17 18-Apr-2004 21:43

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:22.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.