1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Where is the logic in flag in finding prime number

Discussion in 'Programming' started by Liz Lazu, Feb 10, 2016.

  1. #1
    #include <iostream>
    using namespace std;
    int main()
    {
    int n, i, flag=0;
    cout << "Enter a positive integer: "; cin >> n;
    for(i=2;i<=n/2;++i)
    {
    if(n%i==0) {
    flag=1;
    break;
       }
    }
    if (flag==0)
    cout << "This is a prime number"; else cout << "This is not a prime number";
    return 0;
    }
    Code (markup):

     
    Liz Lazu, Feb 10, 2016 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #2
    More homework?
    How about you tell us what you understand and what you don't and we can fill in the gaps
     
    sarahk, Feb 11, 2016 IP
  3. Liz Lazu

    Liz Lazu Member

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    33
    #3
    Everything I post here is not a home work. I'm just learning programming and I don't want to cram. What I know about prime is the number which can be divided by one or itself. The first condition I have already understood even number can't be prime number when reminder is zero.
    More I want to know when flag is set equal to one. How does the program detect that number can be divided with one or itself
     
    Liz Lazu, Feb 11, 2016 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #4
    It doesn't need to because that is always true
     
    sarahk, Feb 11, 2016 IP