Monday, February 15, 2016

C Program to calculate sum of all elements in an Array

/*=================================================================================
 **
 **  File Name     : sumArray.c
 **  Creation Date : Tue 16 Feb 2016 02:47:54 AM IST
 **  Last Modified : Tue 16 Feb 2016 02:57:53 AM IST
 **  Compoler      : gcc
 **  Author        : Manoj Kumar Patra, manojpatra.sit@gmail.com
 **  Organization  : SCIS, University of Hyderabad, India.
 **
 **===============================================================================*/

#include<stdio.h>
int main()
{
    printf("\n= = = = = = Sum of Array elements = = = = =\n");
    int arr[100], n, i, sum=0;
    printf("\nHow many elements you wanna Store : ");
    scanf("%d", &n);
    printf("\nEnter the values into array :\n");
    for (i=0; i<n; i++)
    {
        scanf("%d", &arr[i]);
    }
    for (i=0; i<n; i++)
    {
        sum = sum + arr[i];
    }
    printf("\nSum of all the numbers in the array = %d\n", sum);
    printf("\n= = = = = = = = = = = = = = = = = = = = = = =\n\n");
    return (0);
}

Output :

max@ubuntu:~/cprog$ gcc sumArray.c -o sumArray
max@ubuntu:~/cprog$ ./sumArray

= = = = = = Sum of Array elements = = = = =

How many elements you wanna Store : 5

Enter the values into array :
1
2
3
4
5

Sum of all the numbers in the array = 15

= = = = = = = = = = = = = = = = = = = = = = =

No comments:

Post a Comment