MUST BE IN C++ LANGUAGE. You may not use any header files other than iostream or
ID: 3816201 • Letter: M
Question
MUST BE IN C++ LANGUAGE. 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>
#include <cstdlib>
using namespace std;
bool isprime(int num)
{
int i,flag = 0;
for(i=2;i<num/2;i++)
{
if(num/i == 0) //if num is divisible by any number between 2 and num/2 it is not prime
flag = 1;
}
if(flag == 0)
return true;
else
return false;
}
int prime(int n)
{
int i,j,count=0;
if (n == 1)
return 2;;
for(i=3;i<=10000;i+=2) // n can be from 3 to 10000
{
int isPrime=1;
int jMax = i/2;
for(j=3;j<=jMax;j+=2)
{
if(i%j==0) //divisible
{
isPrime=0; //not prime
break;
}
}
if(isPrime)
{
if(++count==n)
{
return i;
}
}
}
}
int main()
{
char option;
int number,primeno;
do
{
cout<<" Menu";
cout<<" F: Find the nth prime number";
cout<<" T: Test a value to see if it is prime";
cout<<" Q: Terminate the program";
cout<<" Enter your option : ";
cin>>option;
switch(option)
{
case 'F':
case 'f': cout<<" Enter a positive (greater than 0) value";
cin>>number;
if(number > 0)
primeno = prime(number);
else
cout<<" Invalid number. Try again";
cout<<" The "<<number<<" prime number = "<<primeno;
break;
case 't':
case 'T':cout<<" Enter a non-negative value";
cin>>number;
if(number > 0)
{
if(isprime(number))
cout<<" The number "<<number<<" is a prime number.";
else
cout<<" The number "<<number <<" is not a prime number";
}
else
cout<<" Invalid number";
break;
case 'Q':
case 'q':
exit(0);
default: cout<<" Invalid option ";
cout<<" Enter your option : ";
cin>>option;
break;
}
}while(option != 'Q' || option != 'q');
return 0;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.