1- Write a definition of a function called product. The function receives two pa
ID: 3692861 • Letter: 1
Question
1- Write a definition of a function called product. The function receives two parameters containing integer values. You may assume that neither parameter is negative. The function returns the product of the parameters. So, product(3,5) returns 15. The function must not use a loop of any kind (for, while, do-while).
Instructor Notes:
The problem statement is certainly incomplete, though I'm not entirely sure what they were going for. It appears that the soltution must be recursive, and that you may not use the '*' operator in calculating the result.
2-The sum of the numbers 1 to n can be calculated recursively as follows:
The sum from 1 to 1 is 1.
The sum from 1 to n is n more than the sum from 1 to n-1
Write a function named sum that accepts a variable containing an integer value as its parameter and returns the sum of the numbers from 1 to to the parameter (calculated recursively).
3-Given non-negative integers x and n, x to the nth power can be defined as:
x to the 0th power is 1
x to the nth power can be obtained by multiplying x to the (n-1)th power with x
Write a function named power that accepts two parameters containing integer values (x and n, in that order) and recursively calculates and returns the value of x to the nth power.
4-The nth harmonic number is defined non-recursively as: 1+1/2+1/3+1/4+...+1/n. Come up with a recursive deinition and use it to write a function called harmonic that accepts a parameter n that contains an integer value and returns the nth harmonic number.
Explanation / Answer
Please check out aall four program according to your question with output
1:- Write a definition of a function called product
#include <iostream>
using namespace std;
int product(int a,int b);
// main function start here
int main(){
int a,b;
cout<<"Enter first number : ";
cin>>a;
cout<<" Enter second number : ";
cin>>b;
cout<<" Multiplication is : "<<product(a,b)<<endl;
return 0;
}
// main function end here
// define receives function
int product(int a , int b){
if (b==0)
return 0;
if (a==0){
return 0;
}
else{
return (a + product(a,b-1));
}
}
// receives function end here
Output:-
Enter first number : 3
Enter second number : 5
multiplication is : 15
2. The sum of the numbers 1 to n can be calculated recursively as follows:
#include<iostream>
using namespace std;
int sum(int n);
// main function start here
int main()
{
int n;
cout << "Enter a positive integer: ";
// get user input value here
cin >> n;
// call sum function and print final value here
cout << "Sum = " << sum(n);
return 0;
}
// main function end here
// sum funcation start here
int sum(int n)
{
if(n!=0)
return n+sum(n-1); /* recursive call */
}
// sum funcation end here
Output:-
Enter a positive integer: : 5
Sum : 15
3-Given non-negative integers x and n, x to the nth power can be defined as:
#include<iostream>
using namespace std;
int power(int, int);
// mina function start here
int main(void)
{
int a,b;
cout<<"Enter first number : ";
cin>>a;
cout<<" Enter second number : ";
cin>>b;
cout<<" Result is : "<<power(a,b)<<endl;
return 0;
}
// main function end here
// power function start here
int power(int x, int n)
{
int m;
if (n == 0) return 1;
if (n % 2 == 0) {
m = power(x, n / 2);
return m * m;
} else return x * power(x, n - 1);
}
// power function end here
Output:-
Enter first number : 3
Enter second number : 4
Result is : 81
4-The nth harmonic number is defined non-recursively as: 1+1/2+1/3+1/4+...+1/n
#include<iostream>
using namespace std;
double harmonic(int);
// mina function start here
int main(void)
{
int a,b;
cout<<"Enter your number : ";
cin>>a;
cout<<" Result is : "<<harmonic(a)<<endl;
return 0;
}
// main function end here
// harmonic function start here
double harmonic(int n)
{
if (n == 0) {
return 0.0;
} else {
return (1.0/n + harmonic(n-1));
}
}
// harmonic function end here
Output:-
Enter your number : 9
Result is : 2.82897
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.