RECURSION
16. A class SeriesSum is designed to calculate the sum of the following series:-
Sum= x2/1! + x4/3!+x6/5!+….xn/(n-1)!
Some of the members of the class are given below:
Class name : SeriesSum
Data members
x : to store an integer number
n : to store number of terms
sum : double variable to store the sum of the series
Member functions :
SeriesSum(intxx,intnn) : constructor to assign x=xx, n=nn
double findfact(int m) : to return the factorial of m using recursive technique
void calculate() : to calculate the sum of the series by invoking the recursive
functions repeatedly
void display() : to display the sum of the series
Specify the class SeriesSum, giving details of the constructor(int,int), double findfact(int),
double findpower(int,int), void calculate() and void display(). Define the main() function to
create an object and call the functions accordingly to enable the task.
17. A class RecFact defines a recursive function to find the factorial of a number. The
details of the class are given below:
Class Name : RecFact
Data Members :
n : stores the number whose factorial is required.
r : stores an integer
Member functions :
RecFact() : default constructor
void readnum() : to enter values for n and r.
int factorial(int) : returns the factorial of the number using the recursive
technique.
voidfactseries() : to calculate and display the value of
n!
-----------------
r! * (n-r)!
Specify the class RecFact giving the details of the constructor and member functions void
readnum(), int factorial(int) and void factseries(). Also define the main function to create an
object and call methods accordingly to enable the task.
18. An Emirp number is a number which is prime backwards and forwards. Example: 13
and 31 are both prime numbers. Thus 13 is an emirp number.
Design a class Emirp to check if a given number is Emirp number or not. Some of the
members of the class are given below:
Class Name : Emirp
Data Members
n : stores the number
rev : stores the reverse of the number
f : stores the divisor
Member functions
Emirp(int nn) : to assign n=nn, rev=0, and f=2
int isprime(int x) : check if the number is prime using the recursive technique
and
return 1 if prime otherwise return 0.
void isEmirp() : reverse the given number and check if both the original
number ad the reverse number are prime, by invoking the
function isprime(int) and display the result with an appropriate
message.
Specify the class Emirp giving details of the constructor(int), int isprime(int) and void
isEmirp(). Define the main function to create an object and call the methods to check for
Emirp number.
19. Design a class VowelWord to accept a sentence and calculate the frequency of words that
begin with a vowel. The words in the input string are separated by a single blank space and
terminated by a full stop.
The description of the class is given below:
Class Name : VowelWord
Data members
str : to store a sentence
freq : to store the frequency of words beginning with a vowel.
Member functions
VowelWord() : constructor to initialize data members to legal initial values.
Void readstr() : to accept a sentence.
void freq_vowel( ) : counts the frequency of the words beginning with a vowel.
void display() : to display the original string and the frequency of the
words that begin with a vowel.
Specify the class VowelWord giving details of the constructor( ), void readstr(), void
freq_vowel() and void display(). Also defing the main function to create an object and call
the methods accordingly to enable the task.
20. Input a sentence from the user and count the number of times, the words “an” and “and”
are present in the sentence. Design a class Frequency using the description given below:
Class name : Frequency
Data Members
text : stores the sentence
countand : to store the frequency of the word and.
countan : to store the frequency of the word an.
len : stores the length of the string.
Member functions
Frequency() : constructor to initialize the data variables.
void accept(String n) : to assign n to text where the value of the parameter should be
in lowercase.
void checkandfreq(int) : to count the frequency of and using recursive technique.
void checkanfreq(int) : to count the frequency of an using recursive technique.
: to display the frequency of “an” and “and” with suitable
messages.
Specify class Frequency giving details of the constructor(), void accept(String), void
checkandfreq(), checkanfreq() and void display(). Also define the main function to create an
object and call methods accordingly to enable the task.