Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, Will you please provide an example of how I can implement the following requirement into the existing code I have? The function I am

Hello, Will you please provide an example of how I can implement the following requirement into the existing code I have?

The function I am trying to create is:

In C++, develop a function to format the output for a list of prime numbers. The function must accept two whole numbers: a sequence number and the prime number. The prototype for this function should be string formatPrime(int sequence, int prime);. The function must return a string formatted as Prime #6 = 13.

This is the code I have so far:

#include "pch.h"

#include

#include

using namespace std;

bool isPrime(int number);

int main()

{

int number, n, count, primenum = 0, num= 2;

bool flag;

do

{

cout << "Enter a number between 30 and 2000" << endl;

cin >> n;

cout << endl;

}

while (n < 30 || n > 2000);

cout << "2" << " ";

while (primenum < n - 2)

{

num++;

count = 0;

for (int j = 2; j < num; j++)

{

if (num % j == 0)

{

count++;

break;

}

}

if (count == 0)

{

cout << num << " ";

primenum++;

}

}

cout << "Test any number to see if it is prime. If it is, the output will show true." << endl;

cout << "If it is not a prime number the output will show false " << endl;

cin >> number;

flag = isPrime(number);

if (flag == true)

cout << number << " is a prime number. " << endl;

else

cout << number << " is not a prime number. " << endl;

return 0;

}

bool isPrime(int number)

{

bool flag = true;

for (int i = 2; i <= number / 2; ++i) {

if (number % i == 0) {

flag = false;

break;

}

}

return flag;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Successful Keyword Searching Initiating Research On Popular Topics Using Electronic Databases

Authors: Randall MacDonald, Susan MacDonald

1st Edition

0313306761, 978-0313306761

More Books

Students also viewed these Databases questions