Algorithm
Input Any integer number (up to 32767)
Output Is it a prime number or Not
Complexity O(n)
Prime(num)
1 Set i=2
2 while i<=num/2
3 if num mod i = 0
4 print "Not a Prime number" and exit;
5 i=i+1
6 if (i==(num/2)+1)
7 Print "Prime number"
FLOWCHART
C++ program to Check Prime Number
/* C++ program to check whether a number is prime or not. */
#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;
}
No comments:
Post a Comment