1. Write a recursive function that computes the sum of all numbers from 1 to n, where n is given as parameter.
Here is the method header: public static int sum (int n){...}
2. Write a recursive function that finds and returns the minimum
value in an array, where the array and its size are given as
parameters.
Here is the method header: public static int minValue (int [] data, int size){...}
3. Write a recursive function that reverses the elements in an array, where the array, indices of the first and the last value in the array are given as parameters.
Here is the method header: public static void reverse (int [] data, int start, int end){...}
import java.util.*;
class RecurSum
{
public static int sum(int n)
{
if(n==0)
return 0;
else
return n+sum(n-1);
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter n Value");
int n=sc.nextInt();
System.out.println("The Sum if First "+n+" Numbers is
"+sum(n));
}
}
import java.util.*;
class FindMin
{
private static int findMin(int[] data, int index) {
if(index==0)
return data[0];
if(data[index-1] < findMin(data , index -1))
return data[index -1];
else
return (findMin(data , index -1));
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter How Many Elements");
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
System.out.println("Enter "+(i+1)+" Element");
a[i]=sc.nextInt();
}
System.out.println("The Minimum Element is "+findMin
(a,n));
}
}
import java.util.*;
class ReverseArray
{
public static void reverse(int[] data,int start,int end)
{
int temp;
if (start >= end)
return;
temp = data[start];
data[start] = data[end];
data[end] = temp;
reverse(data, start+1, end-1);
}
static void print(int[] data,int size)
{
int i;
for (i=0; i < size; i++)
System.out.print(data[i] + " ");
System.out.println("");
}
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter How Many Elements");
int n=sc.nextInt();
int data[] = new int[n];
for(int i=0;i<n;i++)
{
System.out.println("Enter "+(i+1)+" Element");
data[i]=sc.nextInt();
}
print(data, n);
reverse(data, 0, n-1);
System.out.println("Reversed array is ");
print(data, n);
}
}
1. Write a recursive function that computes the sum of all numbers from 1 to n,...
C++ Write a recursive function that reverses the given input string. No loops allowed, only use recursive functions. Do not add more or change the parameters to the original function. Do not change the main program. #include <iostream> #include <string> using namespace std; //Write a recursive function 'void reverse(string &str)' that reverses the given input string. void reverse(string &str) { /*Code needed*/ } int main() { string name = "sherry"; reverse(name); cout << name << endl; //should...
PYTHON: (Sum the digits in an integer using recursion) Write a recursive function that computes the sum of the digits in an integer. Use the following function header: def sumDigits(n): For example, sumDigits(234) returns 9. Write a test program that prompts the user to enter an integer and displays the sum of its digits. Sample Run Enter an integer: 231498 The sum of digits in 231498 is 27
Write a method called printReverse() that takes a string and uses recursion to print the contents of the string in reverse order. The string itself should not be reversed; it must be left in its original form. The method has the following header: void printReverse(String s, int i) where s is a reference to the string, and i is an integer parameter that you may use as you see fit. You do not need to code up this method as...
The following recursive method factRecursive computes the factorial of positive integer n. Demonstrate that this method is recursive. public static int factRecursive(int n) { int result = 0; if (n == 0) { result = 1; } else { result = n * factRecursive(n - 1); } return result; }
Write a recursive method: public static int raise2ToN(int n) //computes and returns the value of 2n using //recursion. Note: 2^3 = 2 * 2^2 . 2^2 = 2 * 2^1 2^1 = 2 What value/values of n will stop the recursion (base case)? Sample output: 5 2 to 5 is 32
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...
Create a class called Reverse that reverses an array of integers. The class should have two method: - public static void reverse(int [] array) – Which calls the private recursive method, passing the array parameter and which two array elements should be swapped. - private static void reverseRecursive(int [] array, int startIndex, int endIndex) – Which swaps the two elements of the array parameter pointed to by the startIndex and endIndex parameters. The method should be called again, this time,...
Write a method that will display numbers from 1 to n with 10 numbers per line. The numbers are separated by one space. The method header is as follows: public static void displayNumbers(int n)
c++ program Write a recursive function that computes and returns the product of the first n >=1 real numbers in an array.
17. For the following program, draw a stack to find the output. public class RecApp public static void main(String[] args) System.out.println(Sum(7)); public static int Sum(int n) if (n < 1) return 5; else return n + Sum (n-2); Output: (13) Stack: (4%) Sum (_ ) Sum(_) Sum(_) Function Value Parameter Function Value Parameter Function Value Parameter Function Value Parameter Function Value Parameter Sum( ) 7 + Sum (5) Sum (7) Write the recursive equation (Tn)) for the run time of...