Here's the code:
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
//function is being prototyped!
bool prime(int n);
int main(){
int i;
string selection;
cout<<"This program features two different programs, a prime number checker and a next prime number finder.\n";
cout<<"What program would you like to use?";
cout<<"\nType checker for the prime number checker or finder for the next prime number finder: ";
cin>>selection;
//makes sure you typed the right thing
while((selection != "finder") || (selection != "checker")){
cout<<"Can you not read? Type checker or finder!";
cin>>selection;
}
//Checker Program
if(selection == "checker"){
cout<<"Prime Number Checker: V1.0\nWritten by Willum.";
cout<<"\n\nEnter a number to see if its prime or composite: ";
cin>>i;
if(prime(i)){
cout<<"\n"<< i <<" is prime!";
}else{
cout<<"\n"<<i<<" is composite!";
}
//Finder Program
}else{
cout<<"Next Prime Number Finder: V1.0\nWritten by Willum.";
cout<<"\n\n Enter a number to find the next prime number: ";
cin>>i;
for(i; ; i++){
if(prime(i)){
cout<< i << " is the next prime number!";
break;
}
}
}
}
//n = i in the function. what goes in it, basically.
//the actual prime number function
bool prime(int n){
int i;
//prime number formula in a loop
for (i=2; i<= sqrt(n); i++){
//if there is no remainder
if (n % i == 0){
//its composite
return false;
}
//can't find one, returns prime
}
return true;
}


Sign In
Create Account


Back to top









