


CODE
class Main {
public static int factorial(int n)
{
if(n == 1) // base case
return 1;
// calling recursively with 1 less and multiplying n
return n*factorial(n-1);
}
public static double pow(double base, int exponent)
{
if(exponent == 1) // base case
return base;
// calling recursively with 1 less and multiplying base
return base*pow(base, exponent-1);
}
public static void main(String[] args) {
System.out.println(factorial(5));
System.out.println(pow(3.0, 4));
}
}
OUTPUT
120
81.0
// We are allowed to answer 2 coding questions only. Please post accordingly.
You will implement and test several short recursive methods below. With the proper use of recursi...
implement and test several recursive methods below. Test all these in the same program. Keep adding methods and testing until all are working.(java) 1.A recursive method to find the sum of all elements of an integer array. The method should have 2 parameters. The array and where to start in the array. Example of initial call: int sum = sumOfValues(array, 0); 2.A recursive method to find the largest element in an integer array. The method should have 2 parameters as...
In Jgrasp(Java) Thanks!
This solution must use methods. It is recommended to use methods for this solution. Write a program that reads an integer and displays, using asterisks, a filled and hollow square, placed next to each other. For example, if the side length is 5. the program should display: ***** ***** You can assume that the user will enter a side length that is at least 2. If user enters a number smaller than 2, and your program has...
Write a program that uses a recursive function to determine whether a string is a character-unit palindrome. Moreover, the options for doing case sensitive comparisons and not ignoring spaces are indicated with flags. For example "A nut for a jar of tuna" is a palindrome if spaces are ignored and not otherwise. "Step on no pets" is a palindrome whether spaces are ignored or not, but is not a palindrome if it is case sensitive. Background Palindromes are character sequences...
I keep getting an error code in my C++ program. It says "[Error] 'strlen' was not declared in this scope" in my main.cpp. Here are my codes. main.cpp #include <iostream> #include <string> #include "functions.h" using namespace std; //definition of the main function. //takes arguments from the command-line. int main(int argc, char *argv[]) { //Determine if you have enough arguments. //If not, output a usage message and exit program if (argc<2 || (argc == 2 && argv[1][0] == '-')) { //call...
Can you help me make these two methods listed below work? I have code written but it is not working. I would appreciate any advice or help. Function "isAPalidrome" accepts an array of characters and returns an integer. You must use pointer operations only, no arrays. This function should return 1 (true) if the parameter 'string' is a palindrome, or 0 (false) if 'string' is not a palindrome. A palindrome is a sequence of characters which when reversed, is the...
In java, please complete the methods in the class below. Add comments to identify your base case or base cases and the recursive steps in each of the recursive implementations. a. public String starsRecursive(int numOfLinesOfStars) takes the number of lines of stars to be in the returned string. The first line will have as many stars as numOfLinesOfStars. Each line after the first line will have one less star. The last line will have only one star. If the value...
Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement some list functionalities on an ArrrayList of generic type using type parameters in method definition Our goal for this lab is to continue using the divide and conquer approach of splitting a list into its head and tail and keep recursing on the tail (which happens to be smaller). However, instead of trying the approach on a string (a list of characters) we would...
***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName) { cout << "Usage: " << executableName << " [-c] [-s] string ... " << endl; cout << " -c: turn on case sensitivity" << endl; cout << " -s: turn off ignoring spaces" << endl; exit(1); //prints program usage message in case no strings were found at command line } string tolower(string str) { for(unsigned int i = 0; i < str.length(); i++)...
JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using recursion. The method signatures are included in the starter code below, with a more detailed explanation of what function the method should perform. You will be writing the following methods: stringClean() palindromeChecker() reverseString() totalWord() permutation() You will be using tools in the String class like .substring(), .charAt(), and .length() in all of these methods, so be careful with indices. If you get stuck, think...
Hint: User will enter certain number of random words, you need to read the input as string and evaluate based on the following instructions. Write a void method called palindromeCheck that takes NO argument. The method should have functionality to check whether or not the word is a palindrome and print to the screen all of the palindromes, one per line. Also, the last line of the output should have the message: “There are x palindromes out of y words...