Saturday, August 18, 2018

RECURSION-7:SUM OF ELEMENTS IN ARRAY


  • Problem Description

    Write a program to compute the sum of elements in an array using recursion.

    Input and Output Format:

    Input consists of n+1 integers.

    Refer sample input and output for formatting specifications.

    All text in bold corresponds to input and the rest corresponds to output.
  • CODING ARENA
  • #include <stdio.h>
    int main()
    {
      int arr[100];
      int n,i,sum=0;
      scanf("%d",&n);
      for(i=0;i<n;i++)
      {
        scanf("%d",&arr[i]);
      }
      for(i=0;i<n;i++)
      {
        sum=sum+arr[i];
      }
      printf("%d",sum);
      return 0;
    }
        
        
      
      
      

  • Test Case 1

    Input (stdin)
    5
    
    5 4 3 2 1
    
    
    Expected Output
    15
  • Test Case 2

    Input (stdin)
    7
    
    1 2 3 0 0 0 0
    
    
    Expected Output
    6

No comments:

Post a Comment