Answer to Question #243503 in C++ for Frida

Question #243503

But how do I ask user to choose a number and previous prime before that and after is displayed. And how many integers are in there depending on user input number?


1
Expert's answer
2021-09-28T17:13:45-0400
#include <iostream>
using namespace std;
  
bool isPrimeNumber(int number)
{
    
    if (number <= 1)
        return false;
  
    
    for (int i = 2; i < number; i++)
        if (number % i == 0)
            return false;
  
    return true;
}
  


int main()
{
    int prev, next;
	cout<<"Enter a number\n";
	int number;
	cin>>number;
	for(int i=number-1; i>=0; i--){
		if(isPrimeNumber(i)==true){
			prev=i;
			break;
		}
	}
	for(int i=number +1; i<number * 2; i++){
		if(isPrimeNumber(i)==true){
			next = i;
			break;
		}
	}
	cout<<"Prevous Prime:  "<<prev<<endl;
	cout<<"Next Prime:  "<<next<<endl;
	
	
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS