CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

Thread: can someone explain whats going on

1. Junior Member
Join Date
Nov 2011
Posts
3

can someone explain whats going on

here is a full working program to check a number for primeness

i need an expert who can explain it for me particularly the section //print results

what is if(is_prime) mean.

#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...
// Assume true for now.
// Get a number from the keyboard.
cout << "Enter a number and press ENTER: ";
cin >> n;
// Test for prime by checking for divisibility
// by all whole numbers from 2 to sqrt(n).
i = 2;
while (i <= sqrt(n)) { // While i is <= sqrt(n),
if (n % i == 0) // If i divides n,
is_prime = false; // n is not prime.
i++; // Add 1 to i.
}
// Print results
if (is_prime)
cout << "Number is prime." << endl;
else
cout << "Number is not prime." << endl;
system("PAUSE");
return 0;
}

2. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

Re: can someone explain whats going on

Originally Posted by TalkTime
here is a full working program to check a number for primeness
Next time, please use code tags when posting code. Your code is almost unreadable without using them.
i need an expert who can explain it for me particularly the section //print results

what is if(is_prime) mean.
What part of that statement is confusing you? The if()? The is_prime? The is_prime is either true or false, and the if() statement executes if it is true.

Regards,

Paul McKenzie

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•