読み込んだ2つの整数値の和を表示
明解C言語 入門編 > 1. まずは慣れよう >
読み込んだ2つの整数値の和を表示
C
#include <stdio.h>
int main(int argc, char* argv[])
{
int n1, n2;puts("2つの整数を入力してください。");
printf("整数1:");
scanf("%d", &n1);printf("整数2:");
scanf("%d", &n2);printf("それらの和は%dです。\n", n1 + n2);
return 0;
}
実行結果
R:\>lesson005\project1.exe
2つの整数を入力してください。
整数1:27
整数2:35
それらの和は62です。
C++
#include <iostream.h>int main(int argc, char* argv[])
{
int n1, n2;cout << "2つの整数を入力してください。" << endl;
cout << "整数1:";
cin >> n1;cout << "整数2:";
cin >> n2;cout << "それらの和は" << n1 + n2 << "です。" << endl;
return 0;
}
実行結果
T:\>lesson005\project1.exe
2つの整数を入力してください。
整数1:27
整数2:35
それらの和は62です。
Delphi
program Project1;{$APPTYPE CONSOLE}
uses
SysUtils;
var
n1, n2: Integer;
begin
writeln('2つの整数を入力してください。');write('整数1:');
read(n1);write('整数2:');
read(n2);write(format('それらの和は%dです。'#13#10, [n1 + n2]));
end.
実行結果
S:\>lesson005\project1.exe
2つの整数を入力してください。
整数1:27
整数2:35
それらの和は62です。