Sample – check if a number is prime

by Nideesh C on February 8, 2011 · 0 comments

in C++




#include <iostream.h>

int smalldiv (int n) {
int count;
count = 2;

while (count < n && n % count != 0) {
count = count + 1;
}
return(count);
}

int main () {
int n;
cout << “Enter a natural number: “;
cin >> n;
if (n == smalldiv(n)) {
cout << n << ” is a prime number” << endl;
}
else {
cout << n << ” is not a prime number” << endl;
}
return(0);
}

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. Sample – count number of characters and lines in a file
  2. Sample – factorial
  3. Sample program (multiplication using addition)
  4. Sample – exponentiation using multiplication
  5. Sample – Fibonacci numbers

Leave a Comment

Previous post:

Next post: