Question

1. Write a recursive function that computes the sum of all numbers from 1 to n,...

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){...}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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));
}
}

XUsers sagarjavac RecurSun.jaua Users sagarja Recur Sun ter n Ualue he Sun if First 15 Numbcrs is 120

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));
}
}

S. CAWin CUsersagajavac FindMin-java Users sagariava FindMin Enter How Many Elements Enter 1 Element Enter 2 Element Enter 3

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);
}
}

S. CAWin CNUsersagajavac ReverseArray.java Users sagarjava ReverseArray Enter How Many Elements Enter 1 Element Enter 2 Eleme

Add a comment
Know the answer?
Add Answer to:
1. Write a recursive function that computes the sum of all numbers from 1 to n,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. (Sum the digits in an integer) Write a method that computes the sum of the...

    1. (Sum the digits in an integer) Write a method that computes the sum of the digits in an integer. Use the following method header: public static int sumDigits(long n) For example, sumDigits (234) returns 9 (2 + 3 + 4). (Hint: Use the % operator to extract digits, and the / operator to remove the extracted digit. For instance, to extract 4 from 234, use 234 % 10(= 4). To remove 4 from 234, use 234 / 10(= 23)....

  • C++ Write a recursive function that reverses the given input string. No loops allowed, only use...

    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...

    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

  • C++: Write a recursive function that displays the contents of an array in reverse order (from...

    C++: Write a recursive function that displays the contents of an array in reverse order (from the bottom up). The function declaration may look something like this: void displayReverse(char list[], int size, int startingIndex); The parameters are described as follows: list: The array to be displayed. size: The number of elements in the array. startingIndex: The element of the array currently being examined by the algorithm. The call to the function from main should look something like this, assuming that...

  • In Java Write a recursive method called sumUpto(n) which will return the sum of the first...

    In Java Write a recursive method called sumUpto(n) which will return the sum of the first n integers. For example, sumUpto(4) will return 10 because 1+2+3+4 = 10. sumUpto(5) will return 15 because 1+2+3+4+5=15. So you can see that sumUpto(5) = sumUpto(4) + 5. Make this more general for n : sumUpto(n) = sumUpto(??) + n Figure out the base case and write the method……….. import java.util.Scanner; public class Lab12Num1 { public static int sumUpto(int num) {    } public...

  • Write a method called printReverse() that takes a string and uses recursion to print the contents...

    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...

  • Assignment: Write a program with each of the following methods. You can assume this program is...

    Assignment: Write a program with each of the following methods. You can assume this program is saved in a file called multiArrays.java. A sample main method and sample output is provided at the end of this section. All arrays created are of integer-type. Write a method to declare, initialize, and populate a two-dimensional array. Using the following header:                         public static int[ ][ ] declare2D(Scanner scnr) The user will indicate the size of the array. Write a method to declare,...

  • The following recursive method factRecursive computes the factorial of positive integer n. Demonstrate that this method...

    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...

    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

  • Write a method that will display numbers from 1 to n with 10 numbers per line....

    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)

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT