How to add two numbers in all supported programming languages?   C Accept two integers then add and print their sum. #include<stdio.h> #include <stdlib.h> int main() { int num1, num2; scanf("%d %d", &num1, &num2); printf("%d", num1 + num2); }   C++/C++17 Accept two integers then add and print their sum. #include<iostream> using namespace std; int main() { int num1, num2; cin>>num1>>num2; cout<<num1+num2; }   Java Accept two integers then add and print their sum. import java.util.*; public class Hello { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num1 = sc.nextInt(); int num2 = sc.nextInt(); System.out.println(num1 + num2); } }   Python 2 Read and convert the first line to two integers. Then print their sum. num1, num2 = map(int, raw_input().split()) print num1 + num2   Python 3 Read and convert the first line to two integers. Then print their sum. num1, num2 = map(int, input().split()) print(num1 + num2)   C# Read and convert the first line to two integers. Then print their sum. using System; namespace Hello { class Hello { public static void Main (string[] args) { char[] splitchar = { ' ' }; String[] stringValues = Console.ReadLine().Split(splitchar); Int32 num1 = Convert.ToInt32(s[0]); Int32 num2 = Convert.ToInt32(s[1]); Console.WriteLine(num1+num2); } } }   Javascript Read and convert the first line to two integers. Then print their sum. var readline = require('readline'); var reader = readline.createInterface({ input: process.stdin, terminal: true }); var lines = []; reader.on('line', function (line) { lines.push(line); }); reader.on('close', function () { var stringValues = lines[0].split(" "); var num1 = parseInt(stringValues[0]) var num2 = parseInt(stringValues[1]) console.log(num1 + num2) });   Go Accept two integers then add and print their sum. package main import "fmt" func main() { var a int var b int fmt.Scanf("%d %d", &a, &b) fmt.Println(a+b) }   PHP Accept two integers then add and print their sum. <?php fscanf(STDIN, "%d %d\n", $num1, $num2); echo $num1 + $num2; ?>   Scala Accept two integers then add and print their sum. import java.util.Scanner; object Hello { def main(args: Array[String]) { var scanner = new Scanner(System.in); var num1 = scanner.nextInt(); var num2 = scanner.nextInt(); println(num1+num2); } }   Ruby Read and convert the first line to two integers. Then print their sum. stringValues = gets.chomp.split(" ") num1 = stringValues[0].to_i num2 = stringValues[1].to_i puts num1+num2   Kotlin Accept two integers then add and print their sum. import java.util.Scanner fun main(vararg args: String) { val input = Scanner(System.`in`) val num1 = input.nextInt() val num2 = input.nextInt() println(num1 + num2) }   Perl Read and convert the first line to two integers. Then print their sum. while (<STDIN>) { ($x, $y) = split; print ($x+$y); }   R Read and convert the first line to two integers. Then print their sum. f <- file("stdin") on.exit(close(f)) T <- readLines(f) T <- strsplit(T, " ") num1 <- as.numeric(T[[1]][1]) num2 <- as.numeric(T[[1]][2]) write(num1+num2, stdout())   Groovy Read and convert the first line to two integers. Then print their sum. def str = System.in.newReader().readLine() def (num1, num2) = str.tokenize( ' ' ) println num1.toInteger()+num2.toInteger()   D Read and convert the first line to two integers. Then print their sum. import std.stdio; import std.string; import std.conv; void main() { auto input = strip(stdin.readln()); auto arr = input.split(" "); int a = to!int(arr[0]); int b = to!int(arr[1]); writeln(a+b); }   Lua Read and convert the first line to two integers. Then print their sum. local str = io.read() local sum = 0 for token in string.gmatch(str, "[^%s]+") do sum = sum + tonumber(token) end print(sum)  

Add Two Numbers in All Supported Languages2 months ago

How to add two numbers in all supported programming languages?

 

C
Accept two integers then add and print their sum.

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

int main()
{
    int num1, num2;
    scanf("%d %d", &num1, &num2);
    printf("%d", num1 + num2);
}

 

C++/C++17
Accept two integers then add and print their sum.

#include<iostream>
using namespace std;

int main()
{
    int num1, num2;
    cin>>num1>>num2;
    cout<<num1+num2;
}

 

Java
Accept two integers then add and print their sum.

import java.util.*;
public class Hello {

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int num1 = sc.nextInt();
        int num2 = sc.nextInt();
        System.out.println(num1 + num2);
    }
}

 

Python 2
Read and convert the first line to two integers. Then print their sum.

num1, num2 = map(int, raw_input().split())
print num1 + num2

 

Python 3
Read and convert the first line to two integers. Then print their sum.

num1, num2 = map(int, input().split())
print(num1 + num2)

 

C#
Read and convert the first line to two integers. Then print their sum.

using System;
namespace Hello
{
    class Hello
    {
        public static void Main (string[] args)
        {
            char[] splitchar = { ' ' };
            String[] stringValues = Console.ReadLine().Split(splitchar);
            Int32 num1 = Convert.ToInt32(s[0]);
            Int32 num2 = Convert.ToInt32(s[1]);
            Console.WriteLine(num1+num2);
        }
    }
}

 

Javascript
Read and convert the first line to two integers. Then print their sum.

var readline = require('readline');
var reader = readline.createInterface({
  input: process.stdin,
  terminal: true
});

var lines = [];

reader.on('line', function (line) {
    lines.push(line);
});

reader.on('close', function () {
    var stringValues = lines[0].split(" ");
    var num1 = parseInt(stringValues[0])
    var num2 = parseInt(stringValues[1])
    console.log(num1 + num2)
});

 

Go
Accept two integers then add and print their sum.

package main
import "fmt"
func main() {
    var a int
    var b int
    fmt.Scanf("%d %d", &a, &b)
    fmt.Println(a+b)
}

 

PHP
Accept two integers then add and print their sum.

<?php
    fscanf(STDIN, "%d %d\n", $num1, $num2);
    echo $num1 + $num2;
?>

 

Scala
Accept two integers then add and print their sum.

import java.util.Scanner;
object Hello {
    def main(args: Array[String])
    {
        var scanner = new Scanner(System.in);
        var num1 = scanner.nextInt();
        var num2 = scanner.nextInt();
        println(num1+num2);
    }
}

 

Ruby
Read and convert the first line to two integers. Then print their sum.

stringValues = gets.chomp.split(" ")
num1 = stringValues[0].to_i
num2 = stringValues[1].to_i
puts num1+num2

 

Kotlin
Accept two integers then add and print their sum.

import java.util.Scanner

fun main(vararg args: String) {
  val input = Scanner(System.`in`)
  val num1 = input.nextInt()
  val num2 = input.nextInt()
  println(num1 + num2)
}

 

Perl
Read and convert the first line to two integers. Then print their sum.

while (<STDIN>) 
{
    ($x, $y) = split;
    print ($x+$y);
}

 

R
Read and convert the first line to two integers. Then print their sum.

f <- file("stdin")
on.exit(close(f))
T <- readLines(f)
T <- strsplit(T, " ")
num1 <- as.numeric(T[[1]][1])
num2 <- as.numeric(T[[1]][2])
write(num1+num2, stdout())

 

Groovy
Read and convert the first line to two integers. Then print their sum.

def str = System.in.newReader().readLine()
def (num1, num2) = str.tokenize( ' ' )
println num1.toInteger()+num2.toInteger()

 

D
Read and convert the first line to two integers. Then print their sum.

import std.stdio;
import std.string;
import std.conv;

void main()
{
    auto input = strip(stdin.readln());
    auto arr = input.split(" ");
    int a = to!int(arr[0]);
    int b = to!int(arr[1]);
    writeln(a+b);
}

 

Lua
Read and convert the first line to two integers. Then print their sum.

local str = io.read()
local sum = 0
for token in string.gmatch(str, "[^%s]+") do
   sum = sum + tonumber(token)
end
print(sum)