Answer in Java please: 1: Write a java program that can compute sum of an array
ID: 3783325 • Letter: A
Question
Answer in Java please:
1: Write a java program that can compute sum of an array using recursive calls
Test the program uses the array of {4,3,6,2,5,10}
2: Using throws statement to handle run-time error to write a java program that uses two recursive power function objects we addressed in class. Compare the execution time (in nanoseconds) of the result.
3:
The “odd/even factorial” of a positive integer is represented as n!! and is defined non-recursively as: (n)(n-2)(n-4)…(4)(2) if n is even and is (n)(n-2)(n-4)…(5)(3)(1) if n is odd. For example 7!! Equals 7*5*3*1 or 105 and 6!! Is 6*4*2 or 48. Write a method called oddevenfact that recursively calculates the odd/even factorial value of its single int parameter. The value returned by this method may be a long data type.
Explanation / Answer
------------/*Sum of Array using Recursion*/----------------------------
import java.util.Scanner;
Class RecursiveSum
{
public static void main(String[] args)
{
int n;
Scanner s = new Scanner(System.in);//read input from user
System.out.print("Enter the no. of elements in array:");
n = s.nextInt();
int a[] = new int[n];//store elements into array
System.out.print("Enter elements in array:");//Display elements in array
for(int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
Recursive_ArraySum arr = new Recursive_ArraySum();
int sum_value = arr.RecrsiveAdd(a, a.length, 0);
System.out.println("Sum of array elements in array is :"+sum_value);
public class Recursive_ArraySum //Recursive function to add the elements
{
int RecrsiveAdd(int a[], int n, int i)
{
if(i < n)//Check tilll the last element of array
{
return a[i] + RecrsiveAdd(a, n, ++i);
}
else
{
return 0;
}
}
}
}
}
/* OddEvenFact Method */
long OddEvenFact(int x){ //Function which inputs integer and returns long data type
if (x>2)
return(oddevenfact(x-2) * (long) x); //Call recursivly
else
return((long) x);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.