ONLY DO WHAT ONLY YOU CAN DO

こけたら立ちなはれ 立ったら歩きなはれ

さまざまな言語で四則演算と数値の出力

VBScript

WScript.Echo 3 + 5
WScript.Echo 3 - 5
WScript.Echo 3 * 5
WScript.Echo 3 ^ 5
WScript.Echo 5 / 3
WScript.Echo 5 \ 3
WScript.Echo 5 Mod 3

WScript.StdOut.Write     3 * 5 & vbNewLine
WScript.StdOut.WriteLine 3 * 5
Z:\>cscript //nologo 0101.vbs
8
-2
15
243
1.66666666666667
1
2
15
15

JavaScript

WScript.Echo(3 + 5)
WScript.Echo(3 - 5)
WScript.Echo(3 * 5)
WScript.Echo(Math.pow(3, 5))
WScript.Echo(5 / 3)
WScript.Echo(parseInt(5 / 3))
WScript.Echo(5 % 3)

WScript.StdOut.Write(3 * 5 + "\n")
WScript.StdOut.WriteLine(3 * 5)
Z:\>cscript //nologo 0101.js
8
-2
15
243
1.66666666666667
1
2
15
15

Windows PowerShell

Echo (3 + 5)
Echo (3 - 5)
Echo (3 * 5)
Echo ([Math]::Pow(3, 5))
Echo (5 / 3)
Echo ([Math]::Floor(5 / 3))
Echo (5 % 3)

Write-Output (3 * 5)
Write-Host   (3 * 5)

Write-Host ([string]::format("{0,3:D}`n",  3 * 5)) -nonewline
Write-Host ([string]::format("{0,23:F20}", 5 / 3))
Z:\>powershell -file 0101.ps1
8
-2
15
243
1.66666666666667
1
2
15
15
 15
 1.66666666666667000000

Perl

print 3 + 5, "\n";
print 3 - 5, "\n";
print 3 * 5, "\n";
print 3 ** 5, "\n";
print 5 / 3, "\n";
print int(5 / 3), "\n";
print 5 % 3, "\n";

printf "%3d\n",     3 * 5;
printf "%23.20f\n", 5 / 3;
Z:\>perl 0101.pl
8
-2
15
243
1.66666666666667
1
2
 15
 1.66666666666666670000

PHP

<?php
print (3 + 5)."\n";
print (3 + 5)."\n";
print (3 - 5)."\n";
print (3 * 5)."\n";
print pow(3, 5)."\n";
print (5 / 3)."\n";
print (int)(5 / 3)."\n";
print (5 % 3)."\n";

echo  3 * 5, "\n";

printf("%3d\n",     3 * 5);
printf("%23.20f\n", 5 / 3);
?>
Z:\>php 0101.php
8 
-2 
15 
243 
1.6666666666667 
1 
2 
15
 15
 1.66666666666666674068

Python

import sys

print 3 + 5
print 3 - 5
print 3 * 5
print 3 ** 5
print 5 / 3.0
print 5.0 / 3
print 5 / 3
print 5 % 3

sys.stdout.write(str(3 * 5) + "\n")

print "%3d"     % (3 * 5)
print "%23.20f" % (5 / 3.0)
Z:\>python 0101.py
8
-2
15
243
1.66666666667
1.66666666667
1
2
15
 15
 1.66666666666666674068

Ruby

puts 3 + 5
puts 3 - 5
puts 3 * 5
puts 3 ** 5
puts 5 / 3.0
puts 5.0 / 3
puts 5 / 3
puts 5 % 3

print 3 * 5, "\n"

printf "%3d\n",     3 * 5
printf "%23.20f\n", 5 / 3.0
Z:\>ruby 0101.rb
8
-2
15
243
1.6666666666666667
1.6666666666666667
1
2
15
 15
 1.66666666666666674068

Pascal

Program Pas0101(arg);
uses
    SysUtils, Math;
begin
    writeln(3 + 5);
    writeln(3 - 5);
    writeln(3 * 5);
    writeln(power(3, 5));
    writeln(5 / 3);
    writeln(5 div 3);
    writeln(5 mod 3);

    write(IntToStr(3 * 5) + #13#10);

    writeln(format('%3d',     [3 * 5]));
    writeln(format('%23.20f', [5 / 3]));
end.
Z:\>fpc Z:\Pas0101.pp
Free Pascal Compiler version 2.6.2 [2013/02/12] for i386
Copyright (c) 1993-2012 by Florian Klaempfl and others
Target OS: Win32 for i386
Compiling Pas0101.pp
Linking Pas0101.exe
17 lines compiled, 0.1 sec , 60800 bytes code, 13036 bytes data

Z:\>Pas0101
8
-2
15
 2.4300000000000000E+0002
 1.6666666666666667E+0000
1
2
15
 15
    1.66666666666666670

VB.NET

Module VB0101
    Public Sub Main()
        Console.WriteLine(3 + 5)
        Console.WriteLine(3 - 5)
        Console.WriteLine(3 * 5)
        Console.WriteLine(3 ^ 5)
        Console.WriteLine(5 / 3)
        Console.WriteLine(5 \ 3)
        Console.WriteLine(5 Mod 3)

        Console.Write(3 * 5 & vbNewLine)

        Console.WriteLine(String.Format("{0,3:D}",    3 * 5))
        Console.WriteLine(String.Format("{0,23:F20}", 5 / 3))
    End Sub
End Module
Z:\>vbc VB0101.vb
Microsoft (R) Visual Basic Compiler version 10.0.30319.233
Copyright (c) Microsoft Corporation.  All rights reserved.

Z:\>VB0101
8
-2
15
243
1.66666666666667
1
2
15
 15
 1.66666666666667000000

C#

public class CS0101
{
    public static void Main()
    {
        System.Console.WriteLine(3 + 5);
        System.Console.WriteLine(3 - 5);
        System.Console.WriteLine(3 * 5);
        System.Console.WriteLine(System.Math.Pow(3, 5));
        System.Console.WriteLine(5 / 3);
        System.Console.WriteLine(5.0 / 3);
        System.Console.WriteLine(5 / 3.0);
        System.Console.WriteLine(5 % 3);

        System.Console.Write(3 * 5 + "\n");

        System.Console.WriteLine(string.Format("{0,3:D}",    3 * 5));
        System.Console.WriteLine(string.Format("{0,23:F20}", 5 / 3.0));
    }
}
Z:\>csc CS0101.cs
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.

Z:\>CS0101
8
-2
15
243
1
1.66666666666667
1.66666666666667
2
15
 15
 1.66666666666667000000

Java

public class Java0101 {
     public static void main(String []args) {
        System.out.println(3 + 5);
        System.out.println(3 - 5);
        System.out.println(3 * 5);
        System.out.println(Math.pow(3, 5));
        System.out.println(5 / 3);
        System.out.println(5.0 / 3);
        System.out.println(5 / 3.0);
        System.out.println(5 % 3);

        System.out.print(3 * 5 + "\n");

        System.out.println(String.format("%3d",     3 * 5));
        System.out.println(String.format("%23.20f", 5 / 3.0));
    }
}
Z:\>javac Java0101.java

Z:\>java Java0101
8
-2
15
243.0
1
1.6666666666666667
1.6666666666666667
2
15
 15
 1.66666666666666670000

C++

#include <iostream.h>
#include <iomanip>
#include <math.h>
using namespace std;

int main()
{
    cout << 3 + 5     << endl;
    cout << 3 - 5     << endl;
    cout << 3 * 5     << endl;
    cout << pow(3, 5) << endl;
    cout << 5 / 3     << endl;
    cout << 5.0 / 3   << endl;
    cout << 5 / 3.0   << endl;
    cout << 5 % 3     << endl;

    cout << setw(3)  <<                              3 * 5   << endl;
    cout << setw(23) << fixed << setprecision(20) << 5 / 3.0 << endl;

    return 0;
}
Z:\>bcc32 CP0101.cpp
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
CP0101.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

Z:\>CP0101
8
-2
15
243
1
1.66667
1.66667
2
 15
 1.66666666666666674100

Objective-C

#import <Foundation/Foundation.h>

int main()
{
    printf("%d\n", 3 + 5);
    printf("%d\n", 3 - 5);
    printf("%d\n", 3 * 5);
    printf("%d\n", (int)pow(3, 5));
    printf("%d\n", 5 / 3);
    printf("%f\n", 5.0 / 3);
    printf("%f\n", 5 / 3.0);
    printf("%d\n", 5 % 3);

    printf("%3d\n",     3 * 5);
    printf("%23.20f\n", 5 / 3.0);

    return 0;
}
xxxxxx@yyyyyy ~/objc
$ gcc -o OC0101 OC0101.m -lobjc -lgnustep-base -I $INCLUDE -L $LIB $CFLAGS

xxxxxx@yyyyyy ~/objc
$ OC0101
8
-2
15
243
1
1.666667
1.666667
2
 15
 1.66666666666666670000

D

import std.stdio;
import std.math;

void main(string[] args)
{
    writefln("%d",     3 + 5);
    writefln("%d",     3 - 5);
    writefln("%d",     3 * 5);
    writefln("%d",     pow(3, 5));
    writefln("%d",     5 / 3);
    writefln("%f",     5.0 / 3);
    writefln("%f",     5 / 3.0);
    writefln("%d",     5 % 3);

    writef("%d\n",     5 * 3);

    writefln("%3d",     3 * 5);
    writefln("%23.20f", 5 / 3.0);
}
Z:\>dmd D0101.d

Z:\>D0101
8
-2
15
243
1
1.666667
1.666667
2
15
 15
 1.66666666666666674060

Go

package main

import "fmt"
import "math"

func main() {
    fmt.Printf("%d\n", 3 + 5)
    fmt.Printf("%d\n", 3 - 5)
    fmt.Printf("%d\n", 3 * 5)
    fmt.Printf("%f\n", math.Pow(3, 5))
    fmt.Printf("%d\n", 5 / 3)
    fmt.Printf("%f\n", 5.0 / 3)
    fmt.Printf("%f\n", 5 / 3.0)
    fmt.Printf("%d\n", 5 % 3)

    fmt.Printf("%3d\n",     3 * 5)
    fmt.Printf("%23.20f\n", 5 / 3.0)
}
Z:\>8g GO0101.go

Z:\>8l -o GO0101.exe GO0101.8

Z:\>GO0101
8
-2
15
243.000000
1
1.666667
1.666667
2
 15
 1.66666666666666674068