Saturday, 20 December 2014

Find last prime number occur before enter number


#include<iostream>
using namespace std;
int main()
{
 int num,count=0;
 cout<<"Enter number to find last prime number occurs before it: ";
 cin>>num;
  for( int a=num-1;a>=1;a--)
  {
   for(int b=2;b<a;b++)
      {
       if(a%b==0)
       count++;
       }
       if(count==0)
       {
      if(a==1)
       {
       cout<<"no prime number less than 2";
       break;
     }
      cout<<a<<" is the last prime number before entered number";
       break;
       }
       count=0;
}
return 0;
}

No comments:

Post a Comment