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