please help

Hello,I'am having trouble following this code can you explain this code step by step starting at the while loop Thank you

#include <iostream>
#include <cmath>
using namespace std;

int main() {
int n; // Number to test for prime-ness
int i; // Loop counter.
int is_prime = true; // Boolean flag = true

// Get a number from keyboard.
cout << "Enter a number and press Enter: ";
cin >> n;

// Test for prime numbers by checking for divisibility
// by all whole number from 2 to sqrt(n).

i = 2;
while ( i <= sqrt(n)) { // While i is <= sqrt(n).
if ( n % i == 0) {
is_prime = false;
break;
}

i++;
}
// Print the results
if ( is_prime)
cout << "Number is prime." << endl;
else
cout << "Number is not prime." << endl;
return 0;
}
Last edited on
see comments in code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    int n; // Number to test for prime-ness
    int i; // Loop counter.
    int is_prime = true; // Boolean flag = true

    // Get a number from keyboard.
    cout << "Enter a number and press Enter: ";
    cin >> n;

    // Test for prime numbers by checking for divisibility
    // by all whole number from 2 to sqrt(n).

    i = 2;
    
    // while i is less than or equal to the square root of n
    while ( i <= sqrt(n))
    {
        if ( n % i == 0) { // if the remainder of n divided by i is equal to 0
            is_prime = false; // set prime flag to false
            break;  // break out of while statement since n is not prime
        }
        i++; // increment i by 1
    }
    
    // Print the results
    if ( is_prime) // if is_prime is true
    {
        cout << "Number is prime." << endl;
    } else {
        cout << "Number is not prime." << endl;
    }
    
    return 0;
}
Last edited on
Thank you for the reply,but could you use an example with numbers and go though it step by step starting with the while loop so I could see how it works
1
2
3
4
5
6
7
8
9
i = 0;

while(i<=20)
{
std::cout<<i<<" ";
i=i+5;
}

std::cout<<endl;


output:
0 5 10 15 20

in line 1, the variable (i'll assume you know this one) is set to zero.
in line 3, is then evaluated since the expression i<=20 evaluates to true, hence, it will execute the next value.
in line 5, it will output the current value of i which is zero.
in line 6, since i=0 the statement will add five to zero, hence, the output will be 5 and so on until it hits i<=20.
Last edited on
Suppose n is 21. Then look what happens in the loop:
1
2
3
4
5
6
7
8
    while ( i <= sqrt(n))
    {
        if ( n % i == 0) {
            is_prime = false; 
            break;  
        }
        i++;
    }

Now sqrt(n) is 4.58. Starting with i=2, since 2 < 4.58 we enter the loop.
Then n%i is (21 mod 2) which is 1 (the remainder of 21/2), Since that is not zero we skip the body of the if, then increment I, which becomes 3.
Now 3 < 4.58, so we again enter the loop body and calculate 21%3 which IS zero, so we enter the if, set is_prime to false and break out of the loop, having found the answer (21 is not prime.

Now suppose n=17. Let's do the same thing.
The sqrt of 17 is 4.123. Starting with i = 2, since 2 < 4.123 we enter the loop and compute 17%2 which is 1 so we skip the body of the if-statement and increment i to be 3 and repeat the loop check. Since 3 < 4.123 we check 17%3, which is 2.

Since that is not zero, we again skip the body of the if-statement and increment i again, to become , and repeat the loop check. Since 4 < 4.123 we enter the loop body and check 17%4, which is 1. Since that is not zero, we again skip the body of the if-statement and increment i, which becomes 5.

We again perform the loop check. Since 5 is not <= 4.123, the loop terminates, having never set is_prime to false, so it is still true - our answer: 17 is a prime.
Topic archived. No new replies allowed.