Wednesday, May 8, 2019

Sum of array elements using Functions

  • Problem Description

    Write a C program to find the sum of the elements in an array.

    Input Format:

    Input consists of n+1 integers. The first integer corresponds to n , the size of the array. The next n integers correspond to the elements in the array. Assume that the maximum value of n is 15.

    Output Format:

    Refer sample output for details.
  • Test Case 1

    Input (stdin)
    5

    2 3 6 8 1

    Expected Output
    20
  • Test Case 2

    Input (stdin)
    5

    2 3 4 5 1

    Expected Output
    15
Solution

#include<stdio.h>
int main(){
  int n,i,a[20],sum=0;
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
    sum=sum+a[i];
  }
  printf("%d",sum);
  return 0;
}

No comments:

Post a Comment

Parity

Problem Description Ram and Sita playing the parity game. Two types of parity are there. One is odd parity and next is even parity. Ram will...