Friday, 29 September 2017

write a program to find the prime number or not in c++

C++ Program to check whether number is prime or not.

You must have basic knowledge of  C++ programming language and variable  , while loop and if..ealse and for loop

What is prime number?



The prime number mean which postive integer number is divided by 1 or itself.
The prime number such as 13 that is division itself and 1.


In this example we show the prime number is [1,2,3,5,7,11,13,17,19,22]





Example 1: Program to Check Prime Number given by user.


#include<iosteam>
#include <conio.h>
int main ( )
 {
  int m,j,flag=0;
cout<<"please enter the any positive number";
   cin>>m;
for(j=2;j <=n/2;i++)
  {
   //this condition is use for non prime
    if (m%j==0)
{
      flag=1;
       break;
}
}

if(flag==0)
cout <<m <<"This number is prime";
else
cout <<m <<"This number is not prime";
return 0;
getch ();
}
}

Result
------------------------
Please enter the any positive numer
8
8 This number is not prime



Example2: Program to Check Prime Number



#include<iosteam>
#include <conio.h>
int main ( )
 {
  int m=7,j,flag=0;

for(j=2;j <=n/2;i++)
  {
    if (m%j==0)
{
      flag=1;
       break;
}
}

if(flag==0)
cout <<m <<"This number is prime";
else
cout <<m <<"This number is not prime";
return 0;
getch ();
}
}

Output
------------------------

7 This number is prime


No comments:

Post a Comment