Question

In: Computer Science

Write a complete C++ program that at least consists of the main() function and at least...

Write a complete C++ program that at least consists of the main() function and at least two recursive functions. The first function has no return value and can be named printPrime(). It prints first n prime numbers with proper prompt. Note that number 1 is not regarded as a prime number. We assume the first prime number is 2. The printout should start from 2. The prototype of the recursive function should be

void printPrime(int n);

The algorithm of printPrime() should be using a for loop and inside the loop calling another recursive function getPrime(), which returns the nth prime number. Again we assume the first prime number, i.e. getPrime(1), is 2. The function getPrime() has following prototype.

int getPrime(int n);

getPrime() can be implemented by first calling getPrime(n-1) to get the n-1th prime number then using a while loop to find the nth prime number.

The main() function reads a positive integer with proper prompt. Then it calls the printPrime(). You can add other functions if needed.

Solutions

Expert Solution

Code:

#include<iostream>
using namespace std;
void printprime(int n);
int getprime(int n);
int s=1;
int main(){
   int n;
   cout<<"Enter value of n:";
   cin>>n;
   cout<<"prime numbers are:"<<endl;
   printprime(n);
}
void printprime(int n){
   if(s<=n){
       cout<<(getprime(s))<<endl;
       s++;
       printprime(n);
   }
}
int getprime(int n){
   int i=2,j=2,c=0,k=0;
   if(n==0){
       return 0;
   }
   else{
       if(k<=n){
           while(1){
               j=2;
               c=0;
               while(j<i){
                   if(i%j==0){
                       c=1;
                   }
                   j++;
               }
               if(c==0){
                   k++;
               }
               if(k==n){
                   break;
               }
               i++;
           }
           getprime(n-1);
           return i;  
       }
   }  
}

Output:


Related Solutions

Write a C++ program which consists of several functions besides the main() function. The main() function,...
Write a C++ program which consists of several functions besides the main() function. The main() function, which shall ask for input from the user (ProcessCommand() does this) to compute the following: SumProductDifference and Power. There should be a well designed user interface. A void function called SumProductDifference(int, int, int&, int&, int&), that computes the sum, product, and difference of it two input arguments, and passes the sum, product, and difference by-reference. A value-returning function called Power(int a, int b) that...
C++ Write a program that has two functions. The 1st function is the main function. The...
C++ Write a program that has two functions. The 1st function is the main function. The main function should prompt the user for three inputs: number 1, number 2, and an operator. The main function should call a 2nd function called calculate. The 2nd function should offer the choices of calculating addition, subtraction, multiplication, and division. Use a switch statement to evaluate the operator, then choose the appropriate calculation and return the result to the main function.
Write a program in c++, with at least four functions, including main, which must do the...
Write a program in c++, with at least four functions, including main, which must do the following: Ask user whether they want to encode or decode a message – if no, then terminate Take the input string from the user, store it in dynamic memory (use new) As appropriate, encode or decode the message using Rot13. Output the encoded/decoded message Delete the input string from dynamic memory (use delete) Input will be a string of no more than 25 characters....
Write a program that utilizes a function called paymentCalc. In the main body of the program,...
Write a program that utilizes a function called paymentCalc. In the main body of the program, ask the user what is their principal and how many months they want the loan. If it is easier, ask the user for number of years, but don’t forget to change it to months for the calculation. Use a function to calculate their monthly mortgage payment. In the main body of the program, print out what their monthly mortgage payment will be. For simplicity,...
Write the following function and provide a program to test it (main and function in one...
Write the following function and provide a program to test it (main and function in one .py) def repeat(string, n, delim) that returns the string string repeated n times, separated by the string delim. For example repeat(“ho”, 3, “,”) returns “ho, ho, ho”. keep it simple.Python
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three integer values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three int||eger values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
In C Write a main function with a function call to a function called GetLetter which...
In C Write a main function with a function call to a function called GetLetter which has two double arguments/parameters. The function returns a character. Declare and initialize the necessary data variables and assign values needed to make it executable and to prevent a loss of information
Write a complete Java program, including comments in both the main program and in each method,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
Write a complete Java program, including comments in both the main program and in each method,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT