Find last prime number occur before enter number

Find last prime number occur before enter number
Di Posting Oleh : Simple Learning
Kategori : find last prime number in c++ prime number tricky question Tricky C++ questions

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:

 

0 Response to "Find last prime number occur before enter number"

Post a Comment