Sum of Natural Numbers
Find the sum of the first n natural numbers (1 + 2 + ... + n).
Sample input
10
Sample output
55
Solution
n = 10
print(n * (n + 1) // 2)
const n = 10;
console.log((n * (n + 1)) / 2);
public class Main {
public static void main(String[] args) {
int n = 10;
System.out.println(n * (n + 1) / 2);
}
}
fun main() {
val n = 10
println(n * (n + 1) / 2)
}
let n = 10
print(n * (n + 1) / 2)
void main() {
int n = 10;
print(n * (n + 1) ~/ 2);
}
#include <iostream>
using namespace std;
int main() {
int n = 10;
cout << n * (n + 1) / 2 << endl;
return 0;
}
#include <stdio.h>
int main() {
int n = 10;
printf("%d\n", n * (n + 1) / 2);
return 0;
}