Code:  #include <iostream> using namespace std; bool prime(int n) { 	 if (n<=1) 	 { 		 return false; 	 }        for (int i=2;i<=n/2;i++) 	    { 	       if (n%i==0) 		   { 		   	 return false; 		   } 	 	    } 	         return true; } int nearest(int n) { 		 for (int i=n-1;i>=2;i--) 		 { 			 if (prime(i)) 			 { 				 return i; 			 } 		 } 	 return 0; } int main () { 	 int number; 	 cout<<"Enter any number : "; 	 cin>>number; 	 if (prime(number)) 	 { 		 cout<<"The number is prime. "<<endl; 	 }     else     {     	 cout<<"The entered number is compositive."<<endl;     	 cout<<"The nearest prime number will be "<<nearest(number)<<endl; 	 } } Result:
I am Cyber Security student in Air University, Islamabad. I upload different codes and their results of C++ and Bash scripting and some other cyber projects and hacking with its proof. I am innovative of mind and dextrous of hand.
Comments
Post a Comment