The result of adding two or more quantities together.
The sum or total refers to the result of a summation operation. To refer to the operation yields a sum, use the [tag: addition] tag. A summation involves adding two or more numbers or other quantities. It is primarily an arithmetic operation which means it deals with numbers and also other mathematical objects. However, sum can be used in other contexts which may not necessarily be a result of a mathematical operation. Almost all programming languages support addition of numbers using the +
operator. Most programming languages support the summation operation through a built-in language construct or as a utility function part of the standard library.
Examples of sum in various programming languages
C++
#include <iostream>
#include <vector>
#include <numeric>
int main() {
std::vector<int> array{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sum = std::accumulate(array.begin(), array.end(), 0, std::plus<int>());
std::cout << sum;
return 0;
}
Python:
sum_of_items = sum([1, 2, 3, 4, 5, 6])
# sum_of_items will be equal to 21
SQL
SELECT SUM(column1) FROM MyTable
WHERE column1 = "condition"
C#
List<int> array = new List<int>() {1, 2, 3, 4, 5};
int sum = array.Sum(); # sum == 21
Java
import java.util.Arrays;
public class MyClass {
public static void main(String args[]) {
var numbers = Arrays.asList(1, 2, 3, 4, 5);
var sum = numbers.stream().mapToInt(Integer::intValue).sum();
System.out.println(sum);
}
}
R
x <- c(1.1, 2.5, -0.4)
y <- c(NA, 1.7)
sum(x)
sum(y)
sum(y, na.rm = TRUE)
sum(x, y) ## as same as sum(c(x, y))
sum(x, y, na.rm = TRUE) ## as same as sum(c(x, y), na.rm = TRUE)