Home Programming How to Calculate 2+2 in 30 Different Programming Languages

# How to Calculate 2+2 in 30 Different Programming Languages

## Let's create a simple program in Java, C#, Python, JavaScript, Assembler etc.

Here is how you can calculate the expression “2+2” in various (30+) programming languages:

### 2+2 in C++

```#include <iostream>

int main() {
std::cout << 2+2 << std::endl;
return 0;
}```

### 2+2 in C#

```using System;

namespace ConsoleApp1 {
class Program {
static void Main(string[] args) {
Console.WriteLine(2+2);
}
}
}```

### 2+2 in C

```#include <stdio.h>

int main(void) {
printf("%d\n", 2+2);
return 0;
}```

### 2+2 in Java

```public class Main {
public static void main(String[] args) {
System.out.println(2+2);
}
}```

### 2+2 in Python

`print(2+2)`

### 2+2 in Ruby

`puts 2+2`

### 2+2 in JavaScript

`console.log(2+2);`

```<?php
echo 2+2;
?>```

### 2+2 in Swift

```import Foundation

print(2+2)```

### 2+2 in Kotlin

```fun main(args: Array<String>) {
println(2+2)
}```

### 2+2 in Go

```package main

import "fmt"

func main() {
fmt.Println(2+2)
}```

### 2+2 in R

`print(2+2)`

### 2+2 in Julia

`println(2+2)`

`main = putStrLn (show (2+2))`

### 2+2 in Lisp

`(print (+ 2 2))`

### 2+2 in Prolog

```:- initialization(main).

main :-
write(2+2), nl.```

### 2+2 in Bash

```#!/bin/bash

echo \$((2+2))```

### 2+2 in PowerShell

`Write-Output (2+2)`

### 2+2 in Tcl

`puts [expr 2+2]`

### 2+2 in Matlab

`disp(2+2)`

### 2+2 in Fortran

```program main
print *, 2+2
end program```

### 2+2 in Pascal

```program Main;
begin
writeln(2+2);
end.```

### 2+2 in Basic

`PRINT 2+2`

```with Ada.Text_IO; use Ada.Text_IO;

procedure Main is
begin
Put(2+2);
end Main;```

### 2+2 in Assembler (x86)

```section .data
section .text

global _start

_start:
mov eax, 2
mov ebx, eax
mov eax, 1
int 0x80```

### 2+2 in Erlang

```main() ->
io:fwrite("~p~n", [2+2]).```

### 2+2 in OCaml

```let () =
print_int (2+2);
print_newline ()```

### 2+2 in Scala

```object Main extends App {
println(2+2)
}```

### 2+2 in F#

`printfn "%d" (2+2)`

### 2+2 in Groovy

`println 2+2`

### 2+2 in Elixir

`IO.puts(2+2)`

### 2+2 in Rust

```fn main() {
println!("{}", 2+2);
}```

As you can see, the syntax for performing arithmetic operations and printing output varies somewhat between programming languages, but the basic principles are usually the same. In most languages, you can use the `+` operator to add two numbers together, and then use a function or method to print the result.

Rate this post