Saturday, February 6, 2016

Checking Prime Number in C++

Here is a sample program that I wrote a long time ago in C++ to check for prime numbers. I have a very busy schedule at work that why I was not able to update my blog. Anyway I hope you will find my work useful in your programming assignments in C++ programming.

If you have some questions please send me an email at jakerpomperada@gmail.comand jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.


Program Listing

#include <iostream.h>


 int check_prime(int x)
 {
 int var1=0,var2=0,flag=0;
 cout << "\n\n";
 cout << "\t List of Prime Numbers";
 cout << "\n\n";
 for(var1 = 2; var1 <= x; var1++){
flag = 1;
for(var2 = 2; var2 < var1; var2++){
if((int)var1 % (int)var2 == 0){
flag = 0;
}
}

if(flag == 1){
cout << var2;
cout << " ";
}
}
 }
 main(){


   int values=0;
   cout << "\t\tPrime Number Generator Version 1.0";
   cout << "\n\n";
   cout << "Enter a Number : ";
   cin >> values;

   check_prime(values);
cout << "\n\n";
system("pause");
}

No comments:

Post a Comment