Question

C++ Recursion Code a function which displays the first n prime numbers. The prototype is: void...

C++ Recursion

Code a function which displays the first n prime numbers.

The prototype is: void prime (int)

The number of primes to display is passed as the parameter. The function prime itself is not recursive. However, it should call a separate recursive helper function which determines if a given number is prime

#include <iostream>
using namespace std;

void prime(int )

{

}

int main()
{

   prime (21);
  
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOURCE CODE:

#include <iostream>

using namespace std;
// recursive helper function to determine n is prime or not
bool isprime(int num,int j)
{
    if(num==2)
       return 2;
    if(num< 2)
        return false;
    if(num%j == 0)
        return false;
    if (num<j*j)
        return true;
    return isprime(num,j+1);
}  

void prime(int n)
{
   int i=1;
   for(i=1;i<=n;i++)
   {
       //calling seperate recursive helper function
       if(isprime(i,2)) //if prime printing prime number
       {  
           cout << i << endl;
       }
      
   }
      
}

int main()
{
   //calling prime function
   prime (21);

return 0;
}



CODE SCREENSHOT:



OUTPUT:

Add a comment
Know the answer?
Add Answer to:
C++ Recursion Code a function which displays the first n prime numbers. The prototype is: void...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which...

    LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which has six working functions that use looping (for, while, or do loops) to repeat the same set of statements multiple times. You will create six equivalent functions that use recursion instead of looping. Although looping and recursion can be interchanged, for many problems, recursion is easier and more elegant. Like loops, recursion must ALWAYS contain a condition; otherwise, you have an infinite recursion (or...

  • 81. The following function call doesn’t agree with its prototype:              cout << BoxVolume( 10, 5...

    81. The following function call doesn’t agree with its prototype:              cout << BoxVolume( 10, 5 );                    int BoxVolume(int length = {1}, int width = {1}, int height = {1});                                                     T__   F__                                                                     82. The following function is implemented to swap in memory the          argument-values passed to it:         void swap(int a, int b)                   {           int temp;             temp = a;             a = b;             b = temp;        ...

  • The prime factorization of a number is the unique list of prime numbers that, when multiplied,...

    The prime factorization of a number is the unique list of prime numbers that, when multiplied, gives the number. For example, the prime factorization of 60 is 2 ∗ 2 ∗ 3 ∗ 5. In this problem you must write code to recursively find and return the prime factorization of the given number. You must print these in ascending sorted order with spaces in between. For example, if your input is: 120 then you should print the following output: 2...

  • answer in c++ Using the table below, complete the C++ code to write the function prototype...

    answer in c++ Using the table below, complete the C++ code to write the function prototype statement, and the void function header. The void function should receive three double variables: the first two by value and the last one by reference. Name the formal parameters num1, num2 and answer. The function should divide the num1 variable by the num2 variable and then store the result in the answer variable. Name the function calcQuotient. Also write an appropriate function prototype for...

  • What's wrong with my code? : I'm trying to use recursive functions to display and count...

    What's wrong with my code? : I'm trying to use recursive functions to display and count all the even numbers of an array. However, I can't seem get the program output the number of even integers . Here's the output I want: Here are the 5 even numbers: // Luckily, however, my code does output all the even numbers. But, I also want the program to tell me how may even integers there are. 2 8 14 18 22 MY...

  • C++ Write a recursive function that reverses the given input string. No loops allowed, only use...

    C++ Write a recursive function that reverses the given input string. No loops allowed, only use recursive functions. Do not add more or change the parameters to the original function. Do not change the main program. #include <iostream> #include <string> using namespace std; //Write a recursive function 'void reverse(string &str)' that reverses the given input string. void reverse(string &str) { /*Code needed*/ } int main() {    string name = "sherry";    reverse(name);    cout << name << endl; //should...

  • C++ // This program prints the proverb // "Now is the time for all good men...

    C++ // This program prints the proverb // "Now is the time for all good men to come to the aid of their party" // in a function (procedure) called writeProverb that is called by the main function // PLACE YOUR NAME HERE #include <iostream> using namespace std; void writeProverb(); // This is the prototype for the writeProverb function int main() { // Fill in the code to call the writeProverb function return 0; } //********************************************************************* // writeProverb // //...

  • Modify the program below by replacing the function prototype "void getScore(int& score);" with "int getScore();" You...

    Modify the program below by replacing the function prototype "void getScore(int& score);" with "int getScore();" You need to modify other part of program according to this change. //This program reads a course score and prints the //associated course grade #include <iostream> using namespace std; void getScore (int& score); void printGrade (int score); int main () int courseScore; cout << "Line 1 Based on the course score, n" <<"this program computes the" <"course grade." << endl; getScore (courseScore); printGrade (courseScore); return...

  • Can you help me to create this program, please? Prompt the user for the user for...

    Can you help me to create this program, please? Prompt the user for the user for a number of degrees in Fahrenheit as an int. That number should be passed to a function named ffocREFO. This function will do the necessary calculation to convert the passed temperature to degrees Celsius as a double. The function MUST have a void return type and have the following prototype: void ffccREF(double& celsius, int Faren); In your main, display both the entered temperature and...

  • This is a c++ code /**    Write a function sort that sorts the elements of...

    This is a c++ code /**    Write a function sort that sorts the elements of a std::list without using std::list::sort or std::vector. Instead use list<int>::iterator(s) */ using namespace std; #include <iostream> #include <iomanip> #include <string> #include <list> void print_forward (list<int>& l) { // Print forward for (auto value : l) { cout << value << ' '; } cout << endl; } void sort(list<int>& l) { write code here -- do not use the built-in list sort function }...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT