HRECURS - Hello Recursion in java

HRECURS – Hello Recursion

You will be given an array of N integers. Write a recursive function to calculate its summation.


Input starts with an integer T (T ≤ 100), the number of test cases.

Each of the next T lines will start with an integer N (1 <= N <= 100), number of integers followed by N space separated. Each of these N integers will be between -1000 and 1000 (inclusive).


For each test case, output one line in the format “Case x: a” (quotes for clarity), where x is the case number and a is the summation of the integers.

Sample Input

5 10 5 -2 3 0
3 100 -10 34

Sample Output

Case 1: 16
Case 2: 124


You just have to get the sum, which is an easy in task iteratively. Try it recursively.

Solution of HRECURS – Hello Recursion :–

import java.util.*;
import java.lang.*;

public class Main
	public static void main (String[] args) throws java.lang.Exception
		//your code here
      Scanner sc=new Scanner(;
      int t=sc.nextInt();
      for(int j=1;j<=t;j++)
          int n=sc.nextInt();
          int[] arr=new int[n];
          for(int i=0;i<n;i++)
          System.out.print("Case "+j+": "+sum(arr,n)+"\n");
  public static int sum(int[] arr,int n)
      return 0;
    return (sum(arr,n-1)+arr[n-1]);

Add a Comment

Your email address will not be published. Required fields are marked *