Saturday, January 12, 2019

program to find prime number using procedural pattern in c plus plus

Question: Program to find prime number.


Solution:
#include"pch.h"
#include<iostream>
#include<conio.h>

  using namespace std;

int main() {
int n,i;
int flag = 1;
cout << "Enter number which is to check : ";
cin >> n;
if (n==2)
{
cout << "Entered number is prime ";
}
else if (n >2 ) 
{
for (i = 2; i <= (n/2); i++) {
if (n%i==0) {
flag = 0;
break;
}

}

if (flag)
{
cout << "Entered number is prime .";

}
else
{

cout << "Entered number is not prime";
}

}
else
{
cout << " Enter valid positive number greater then 1 !!!";
}

return(0);

}



Output:





No comments:

Post a Comment