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:
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:
Hmm..Interesting One.
ReplyDeleteIndeed, interesting article!
ReplyDeletecontent is looks good and it would be helpful if you put the code in more readable format like using the syntax highlighting.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteinteresting
ReplyDeleteCarrot
ReplyDeleteNice help of new learners
ReplyDelete