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...
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 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...
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...
In C++ Write a program that will conduct a quiz consists of 10 arithmetic questions and...
In C++ Write a program that will conduct a quiz consists of 10 arithmetic questions and display the final score as ‘Your score in the quiz is X’ with a response message in the next line. A response message will be as follows based on the student’s performance: a. Number of correct answers >= 9: Excellent, your are passed with ‘A’ grade! b. Number of correct answers >= 7, but less than 9: Very Good, you are passed with ‘B’...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT