Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MUST BE IN C++ LANGUAGE. NO BUILT IN FUNCTIONS. CAN ONLY USE <IOSTREAM>!!!!*****

ID: 3816267 • Letter: M

Question

MUST BE IN C++ LANGUAGE. NO BUILT IN FUNCTIONS. CAN ONLY USE <IOSTREAM>!!!!******* You may not use any header files other than iostream or any built-in functions.**

For this assignment you are to implement two prime number functions that are then used to implement a larger program. One of the prime number function isprime() tests integer values to determine if they are prime. The prototype for the function is:

            bool isprime(int num);

This function tests the value of num to determine if it is a prime number. If num is prime, then the function returns true, otherwise it returns the value false.

The second prime number function, having the prototype

            int prime(int n);

returns the nth prime number, where 2 is the first prime, 3, the second, 5 the third, and so on.

Using the two prime number functions you are to then implement a program that repeatedly offers the user the following three menu options:

F:        Find the nth prime number

T:         Test a value to see if it is prime

Q:        Terminate the program

The user selects an option by entering the associated letter, in uppercase or lowercase. Any other input should lead to a prompting of the user for a correct input. Depending on the option selected by the user program should perform the action described in the following.

In response to an input of F the program prompts the user to enter a positive (greater than 0) value. If a valid value is input, then the program, treating that value as its ordinal number equivalent, should output the prime number corresponding to that value. For example, an input value of one should result in the output of 2, the first prime number; an input of 2 results in the output of 3; and so on. If the user inputs an invalid number value (i.e., zero or a negative number) the program should output an appropriate error message, but take no additional action for the F option

For an input value of T the program prompts the user to enter a non-negative value (i.e., a value that is greater than or equal to zero), after which the program outputs a message indicating whether or not the input value is prime. The program responds to an invalid value with an appropriate message, and no additional action for the T option.

An input of Q causes the program to terminate.

NO BUILT IN FUNCTIONS. CAN ONLY USE <IOSTREAM>!!!!*******

Explanation / Answer

#include <iostream>
using namespace std;

bool isprime(int n)
{
// Corner cases
if (n <= 1) return false;
if (n <= 3) return true;

// This is checked so that we can skip
// middle five numbers in below loop
if (n%2 == 0 || n%3 == 0) return false;

for (int i=5; i*i<=n; i=i+6)
if (n%i == 0 || n%(i+2) == 0)
return false;

return true;
}

int prime(int n)
{
if (n <= 0)
{
cout << "Number should be atleast 1" << endl;
return 0;
}
  
if (n == 1)
{
return 2;
}
  
int count = 1;
  
int lastPrime = 2;
for(int i = 3; count != n; i = i+2)
{
if (isprime(i))
{
lastPrime = i;
count++;
}
}
return lastPrime;
}


// Driver Program to test above function
int main()
{
char choice;
  
while(true)
{
while(true)
{
cout << "Please choose from following option: " << endl;
cout << "F: Find the nth prime number" << endl;
cout << "T: Test a value to see if it is prime" << endl;
cout << "Q: Terminate the program" << endl;
cin >> choice;
if (choice != 'F' && choice != 'f' &&
choice != 'T' && choice != 't' &&
choice != 'Q' && choice != 'q')
{
cout << "Choose from given menu" << endl;
}
else
{
break;
}
}
  
switch(choice)
{
int n;
case 'F':
case 'f':
cout << "Enter n: ";
cin >> n;
cout << prime(n) << endl;
break;
case 'T':
case 't':
cout << "Enter n: ";
cin >> n;
cout << n << " is a prime? " << isprime(n) << endl;
break;
case 'q':
case 'Q':
break;
}
if (choice == 'q' || choice == 'Q')
{
break;
}
}
  
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote