Prime or not prime (i need help)

This is a discussion on Prime or not prime (i need help) within the C++ Programming forums, part of the General Programming Boards category; Code: int main() { int N, C; cout<<"Enter an integer "; cin>>N; C=2; while(N>C*C&&N%C!=0) { C=C+1; } if(N>=C*C) cout<<"NOT PRIME"<<endl; ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    4

    Prime or not prime (i need help)

    Code:
    int main()
    {
        int N, C;
        cout<<"Enter an integer ";
        cin>>N;
        C=2;
        while(N>C*C&&N%C!=0)
        {
         C=C+1;
        }
        if(N>=C*C)
        cout<<"NOT PRIME"<<endl;
        else
        cout<<"PRIME"<<endl;
    
      system("PAUSE");
      return 0;
    }
    This is the code which finds the integer if its prime or not. But i need to do something different. I will enter 15 integers and the program will print only the prime ones.

    I have coded this but didn't work.

    Code:
    int main()
    {
        int A[15];
        int N, C;
        for(N=0;N<15;N=N+1)
           {
            cout<<"Enter "<<N+1<<". value: ";
            cin>>A[N];
           }
        cout<<"--------"<<endl;
        C=2;
        while(N>C*C&&N%C!=0)
        {
         C=C+1;
        }
        if(N>=C*C)
        {
         cout<<A[N]<<endl;
        }
    
      system("PAUSE");
      return 0;
    }
    Can anyone help me how can i do this program.

  2. #2
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    What about something like this:
    Code:
    for i=0 to 14
      enter number i and store in array[]
    
    for(i = 0 to 14)
      if(isPrime(array[i]))
        print out the number
    Basically I think you should make the code that determines if the number is prime a function that returns a boolean value. Read in your array of values and then call the function to dtermine if it's prime. If it is, then print it out. Otherwise, ignore it.

    Hope this helps.

    Also you shouldn't use system("pause") change it to

    cin.ignore();
    cin.get();

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    4
    thanks for your help but i didnt understand : (
    can you write down the full code with correction

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    All you need to do is put this loop
    while(N>C*C&&N%C!=0)

    Inside this loop
    for(N=0;N<15;N=N+1)


    Or as an alternative, put this loop
    while(N>C*C&&N%C!=0)
    inside another function, called say isPrime() which you pass a single integer to, and it returns a true/false value.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    4
    I can't do
    Please, please, please put the loop inside another you said and give me the full code

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    While some of us may be code machines or code monkies, we aren't here to do your work for you. Read your book, ask your teacher, use google.

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Or search the forums! I'm sure there was something about this on the boards. Look through here

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,264
    Quote Originally Posted by toggie
    I can't do
    Please, please, please put the loop inside another you said and give me the full code
    NO, we will NOT do this for you! This programming task is obviously too far above your level of understanding.

    You should fail this otherwise it will give the teacher the false impression that you have the slightest idea what you are doing, when it is very clear that you don't!

  9. #9
    Registered User
    Join Date
    Jul 2006
    Posts
    4
    ok i did it. thanks.

  10. #10
    Registered User
    Join Date
    Jul 2006
    Posts
    1
    In case you want to see a program which outputs first 1000 prime numbers (using prime test function) -- here is an example:
    http://www.anyexample.com/programmin...lity_test).xml

    There is also a little more efficent function for prime testing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating prime factor of a huge number.
    By Bakster in forum C Programming
    Replies: 15
    Last Post: 02-20-2009, 11:06 AM
  2. prime number program with function
    By mackieinva in forum C Programming
    Replies: 17
    Last Post: 09-20-2007, 08:36 AM
  3. prime numbers, counters, help!
    By vege^ in forum C++ Programming
    Replies: 1
    Last Post: 03-10-2003, 03:32 PM
  4. Prime Wonder
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-07-2002, 11:49 PM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 04:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21