Showing posts with label sum of elements in an array. Show all posts
Showing posts with label sum of elements in an array. Show all posts

Saturday, 16 May 2015

Python program to calculate the sum of elements in a list


This is a Python program which calculates the sum of all the elements in a list.

Here we use a function called sum() to calculate the sum of all the items in a list.




PROGRAM : 
lst = []
num = int(input('How many numbers: '))
for n in range(num):
    numbers = int(input('Enter number '))
    lst.append(numbers)
print("Sum of elements in given list is :", sum(lst))


OUTPUT : 
Python  - Sum of all items in a list



Similar Programs : 
C program to calculate the sum of elements in a array
Cpp program to calculate the sum of elements in a array
Java program to calculate the sum of elements in a array

Thursday, 8 January 2015

Java Program to calculate the Sum of Elements in an Array


This is a Java Program which calculates the sum of all elements in an array.

Here initially first element in array and  0 are added the obtained sum is added to 2nd element and so on until the last element in the array.

PROGRAM :
package codingcorner.in;

import java.util.Scanner;

public class SumOfElementsArray {
 public static void main(String[] args) {
  int i, n, sum = 0;
  Scanner scan = new Scanner(System.in);
  System.out.print("How many elements do u want to add ?\t");
  n = scan.nextInt();
  int array[] = new int[n];
  for (i = 0; i < n; i++) {
   System.out.print("\nEnter number" + (i + 1));
   array[i] = scan.nextInt();
  }
  scan.close();
  for (i = 0; i < n; i++) {
   sum = sum + array[i];
  }
  System.out.print("\nThe sum of " + n + " numbers is " + sum);
 }
}

OUTPUT :
Java- Sum of elements in an array

Related Programs:
Sum Of Elements in an array ( Cpp )

C Program to calculate Sum Of Elements in an array


This is a C Program which calculates the sum of all elements in an array.

Here initially first element in array and  0 are added the obtained sum is added to 2nd element and so on until the last element in the array.

PROGRAM :
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,n,sum=0;
    printf("How many elements do u want to add ?\t");
    scanf("%d",&n);
    int array[n];
    for(i=0;i<n;i++)
    {
        printf("\nEnter number %d :\t",i+1);
        scanf("%d",&array[i]);
    }

    for(i=0;i<n;i++)
    {
        sum = sum+array[i];
    }
    printf("\nThe sum of %d numbers is %d\n",n,sum);
    return 0;
}

OUTPUT :
C - Sum Of Elements in an Array


Related Programs:
Sum Of Elements in an array ( Cpp )
Sum Of Elements in an array ( Java)
Largest Element in an array( C )
Largest Element in an array ( Cpp )
Largest Element in an array( Java )

Sunday, 28 December 2014

Cpp Program to calculate Sum of elements in an array


This is a Cpp Program which calculates the sum of all elements in an array.

Here initially first element in array and  0 are added the obtained sum is added to 2nd element and so on until the last element in the array.

PROGRAM :
#include <iostream>

using namespace std;

int main()
{
    int n, array[100],i,sum=0;
    cout << "Enter how many numbers ? :\t" ;
    cin >> n;
    cout << endl;
    for(i=0;i<n;i++)
    {
        cout << "Enter number " << i+1 << " : \t";
        cin >> array[i];
    }
    for(i=0;i<n;i++)
    {
        sum = sum + array[i];
    }

    cout << " \nSum of " << n << " numbers is : "<< sum;
    return 0;
}
OUTPUT :

Cpp - Sum of elements in an array