2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

paizaラーニングレベルアップ問題集の「足し算」を色々な言語でやってみた

Last updated at Posted at 2024-08-11

paiza×Qiita記事投稿キャンペーンということで、paizaラーニングレベルアップ問題集の足し算を色々な言語でやってみました。対象はコードモンスター大図鑑で対応している9言語です。

2024/08/24追記
Perl, Scala, Go, Swiftの4言語を追加いたしました。


PHP
<?php
	[$a, $b] = explode(" ", fgets(STDIN));
	echo $a + $b;
?>

Ruby
a, b = gets.split.map(&:to_i)
p a + b

Java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		sc.close();
		System.out.println(a + b);
	}
}

Python
a, b = map(int, input().split())
print(a + b)

C言語
#include <stdio.h>
int main(void){
	int a, b;
	scanf("%d %d", &a, &b);
	printf("%d\n", a + b);
	return 0;
}

C#
using System;

class Program
{
	static void Main()
	{
		int[] ab = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		Console.WriteLine(ab[0] + ab[1]);
	}
}

Javascript
const lines = require("fs").readFileSync("/dev/stdin", "utf8").split("\n");
// 2024-08-25 parseIntからNumberに修正
// const [a, b] = lines[0].split(" ").map(s => parseInt(s, 10));
const [a, b] = lines[0].split(" ").map(Number);
console.log(a + b);

C++
#include <iostream>
using namespace std;

int main(void){
	int a, b;
	cin >> a >> b;
	cout << a + b << endl;
	return 0;
}

Kotlin
import java.util.*

fun main() {
	val sc = Scanner(System.`in`)
	val a = sc.nextInt()
	val b = sc.nextInt()
	sc.close()
	println(a + b)
}

Perl
my ($a, $b) = split ' ', <STDIN>;
print $a + $b, "\n"

Scala
import scala.io.StdIn._

object Main extends App {
	val Array(a, b) = readLine().split(" ").map(_.toInt)
	println(a + b)
}

Go
package main
import "fmt"
func main() {
	var a, b int
	fmt.Scan(&a, &b)
	fmt.Println(a + b)
}

Swift
import Foundation

let ab = readLine()!.split(separator: " ").map { Int($0)! }
let a = ab[0]
let b = ab[1]
print(a + b)
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?