-->





Find last prime number occur before enter number

These tricky kinds of c++ programs can be given in quiz.
    Tell a prime number which exist before an entered  number

    Where the entered number can be a prime number or not

For example:

Input: 11

Output: last prime number is 7

or

Input: 8

Output: Last prime number is 7


C++ CODE



#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;
}

 Sample output:



Image of Code:

 

7 comments: