TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to print the sum of all the elements of an array

Program to print the sum of all the elements of an array on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

<< Back to PROGRAM

Q. Program to print the sum of all the elements of an array.

Explanation

In this program, we need to calculate the sum of all the elements of an array. This can be solved by looping through the array and add the value of the element in each iteration to variable sum.

Program to print the sum of all the elements of an array

Sum of all elements of an array is 1 + 2 + 3 + 4 + 5 = 15.

Algorithm

  1. Declare and initialize an array.
  2. The variable sum will be used to calculate the sum of the elements. Initialize it to 0.
  3. Loop through the array and add each element of array to variable sum as sum = sum + arr[i].

Solution

Python

#Initialize array 
arr = [1, 2, 3, 4, 5]; 
sum = 0;
 
#Loop through the array to calculate sum of elements
for i in range(0, len(arr)):
   sum = sum + arr[i];
 
print("Sum of all the elements of an array: " + str(sum));

Output:

Sum of all the elements of an array: 15

C

#include 
int main()
{
    //Initialize array 
    int arr[] = {1, 2, 3, 4, 5}; 
    int sum = 0;
    
    //Calculate length of array arr
    int length = sizeof(arr)/sizeof(arr[0]);
    
    //Loop through the array to calculate sum of elements
    for (int i = 0; i < length; i++) { 
       sum = sum + arr[i];
    }  
    printf("Sum of all the elements of an array: %d", sum);
    return 0;
}

Output:

Sum of all the elements of an array: 15

JAVA

public class SumOfArray {
    public static void main(String[] args) {    
        
        //Initialize array 
        int [] arr = new int [] {1, 2, 3, 4, 5}; 
        int sum = 0;
        
        //Loop through the array to calculate sum of elements
        for (int i = 0; i < arr.length; i++) { 
           sum = sum + arr[i];
        }  
        System.out.println("Sum of all the elements of an array: " + sum);
    }
}

Output:

Sum of all the elements of an array: 15

C#

using System;
                    
public class SumOfArray
{
    public static void Main()
    {
        //Initialize array 
        int [] arr = new int [] {1, 2, 3, 4, 5}; 
        int sum = 0;
        
        //Loop through the array to calculate sum of elements
        for (int i = 0; i < arr.Length; i++) { 
           sum = sum + arr[i];
        }  
        Console.WriteLine("Sum of all the elements of an array: " + sum);
    }
}

Output:

Sum of all the elements of an array: 15

PHP

<!DOCTYPE html>
<html>
<body>
<?php
//Initialize array 
$arr = array(1, 2, 3, 4, 5); 
$sum = 0;
 
//Loop through the array to calculate sum of elements
for ($i = 0; $i < count($arr); $i++) { 
   $sum = $sum + $arr[$i];
}  
print("Sum of all the elements of an array: " . $sum);
?>
</body>
</html>

Output:

Sum of all the elements of an array: 15

Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf