c++
Write a function that uses recursion to raise a number to a power. The function should accept two arguments: the number to be raised and the exponent. Assume that the exponent is a nonnegative integer. Demonstrate the function in a program.
Write a function that accepts an integer argument and returns the sum of all the integers from 1 up to the number passed as an argument. For example, if 50 is passed as an argument, the function will return the sum of 1,2,3,...,50. Use recursion to calculate the sum. Demonstrate the function in a program.
Write a recursive function that accepts a string object as its argument and prints the string in reverse order. Demonstrate the function in a driver program.
Program 1:
#include<iostream>
using namespace std;
//function prototype
int power(int, int);
int main()
{
int n, exp;
//prompt and read a number
cout << "Enter a number: ";
cin >> n;
//prompt and read the exponent
cout << "Enter the exponent: ";
cin >> exp;
//display result
cout << "The value after raising " << n <<" to exponent " << exp <<" is: " << power(n,exp);
return 0;
}
//function that returns the value of the number raised to an exponent
int power(int n, int exponent)
{
//if exponent is not 0
if (exponent != 0)
//call the function recursively by passing n and exponent-1 as parameters multiply the result to n
return (n*power(n, exponent-1));
//if exponent is 0 return 1
else
return 1;
}
Output:
Program 2:
#include<iostream>
using namespace std;
//function prototype
int sum(int);
int main()
{
int n;
//prompt and read a number
cout << "Enter a number: ";
cin >> n;
//display result
cout << "The sum of numbers from 1 to " << n << " is: " << sum(n);
return 0;
}
//function that takes a n as parameter and returns the sum of numbers from 1 to n
int sum(int n)
{
//if n is not 0
if (n != 0)
//call the function recursively by passing n-1 as parameters adding the result to n
return (n + sum(n-1));
//if n is 0 return n
else
return n;
}
Output:
Program 3:
#include<iostream>
using namespace std;
//function prototype
void reverse(string);
int main()
{
string str;
//prompt and read a string
cout << "Enter a string: ";
getline(cin, str);
//print reverse of string using reverse method
reverse(str);
return 0;
}
//function that takes a string as parameter and prints its reverse
void reverse(string str)
{
//get the length of the string
int strLength = str.size();
//if length is 1 print the string
if(strLength != 1)
{
//print the last character in the string
cout << str[strLength - 1];
//call the function recursively by removing the last character in the string
reverse(str.substr(0, strLength - 1));
}
else
cout << str << endl;
}
Output:
c++ Write a function that uses recursion to raise a number to a power. The function...
Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...
C++ Create three recursive functions that accomplish the following: Recursive Power Function this function will raise a number to a power. The function should accept two arguments, the number to be raised and the exponent. Assume that the exponent is a non-negative integer. String Reverser function that accepts a string object as its argument and prints the string in reverse order. Sum of Numbers this function accepts an integer argument and returns the sum of all the integers from 1...
I need help writing this program in C++. Thanks so much for your help in advance Function 1: Write a function that accepts an integer argument and returns the sum of all the integers from 1 up to the number passed as an argument. For example, if 50 is passed as an argument, the function will return the sum of 1, 2, 3, 4, ... 50. Use recursion to calculate the sum. Demonstrate the function in a program. A sample...
python programming Design a function that uses recursion to raise a number to a power. The function should accept two arguments: the number to be raised and the exponent. Assume that the exponent is a nonnegative integer.
Write a python program (recursive.py) that contains a main() function. The main() should test the following functions by calling each one with several different values. Here are the functions: 1) rprint - Recursive Printing: Design a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n. 2) rmult - Recursive Multiplication: Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times...
Create a new NetBeans project from scratch named PowerDemo. Write a method that uses recursion to raise a number to a power. The method should accept two arguments: the number to be raised and the exponent. Assume that the exponent is a nonnegative integer. Demonstrate the method in a program. The main method will provide the testing code for using user input for the recursive method.
To be written in C++ write a recursive function that accepts a string object as its argument and prints the string in reverse order. demonstrate the function in a driver program.
Sum of Numbers Problem: Write a method that accepts an integer argument and returns the sum of all the integers from 1 up to the number passed as an argument. For example, if 50 is passed as an argument, the method will return the sum of 1, 2, 3, 4, . . . 50. Use recursion to calculate the sum. Demonstrate the method in a program. The program should be called SumOfNumbers.java.
How would you answer this in visual studios c++ and text book were using is Data Structures Using C++ Author: D.S. Malik. Please help. It has to be written in .cpp 1) Sum of numbers Write a recursive function that accepts an integer argument and returns the sum of the number as an argument. For example, i 50 is passed as an argument, the function will return the sum of 1,2, 3, 4, 50. 2) Counting of times a character...
(C++)Write a function that accepts an int array and the array’s size as arguments.The function should create a new array that is twice the size of the argument array.The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0.The function should return a pointer to the new array.Demonstrate the function by using it in a main program that reads an integer N (that is not more...