home > 写経編 > 柴田望洋『明解C言語 入門編』 > 2. 演算と型 >

ForNext

Only Do What Only You Can Do

014. 読み込んだ3つの整数値の合計値と平均値を表示 (キャスト)

VBScript

JScript

Perl

PHP

Python

Ruby

PowerShell

Scala

F#

C

更新日 : 2010.10.08
#include <stdio.h>
int main(int argc, char* argv[])
{
    int     na, nb, nc;
    int     sum;
    double  avg;

    puts("3つの整数を入力してください。");

    printf("整数A:");
    scanf("%d", &na);

    printf("整数B:");
    scanf("%d", &nb);

    printf("整数C:");
    scanf("%d", &nc);

    sum = na + nb + nc;
    avg = (double)sum / 3;

    printf("それらの合計は%dです。\n", sum);
    printf("それらの平均は%fです。\n", avg);
    printf("それらの平均は%.1fです。\n", avg);

    return 0;
}
R:\>lesson014\project1.exe
3つの整数を入力してください。
整数A:87
整数B:45
整数C:59
それらの合計は191です。
それらの平均は63.666667です。
それらの平均は63.7です。

C++

更新日 : 2010.10.13
#include <iostream.h>
#include <iomanip.h>

int main(int argc, char* argv[])
{
    int     na, nb, nc;
    int     sum;
    double  avg;

    cout << "3つの整数を入力してください。" << endl;

    cout << "整数A:";
    cin >> na;

    cout << "整数B:";
    cin >> nb;

    cout << "整数C:";
    cin >> nc;

    sum = na + nb + nc;
    avg = double(sum) / 3;

    cout << "それらの合計は" << sum << "です。" << endl;
    cout << "それらの平均は" << avg << "です。" << endl;

    cout << fixed << setprecision(1); // 精度を 1桁に指定します。
    cout << "それらの平均は" << avg << "です。" << endl;

    return 0;
}
T:\>lesson014\Project1.exe
3つの整数を入力してください。
整数A:87
整数B:45
整数C:59
それらの合計は191です。
それらの平均は63.6667です。
それらの平均は63.7です。

C++Builder

VC++

C#

Java

更新日 : 2010.11.05
import java.util.Scanner;

class Lesson014 {
    public static void main(String[] args) {
        Scanner stdIn = new Scanner(System.in);
        System.out.println("3つの整数を入力してください。");

        System.out.printf("整数A:");
        int na = stdIn.nextInt();

        System.out.printf("整数B:");
        int nb = stdIn.nextInt();

        System.out.printf("整数C:");
        int nc = stdIn.nextInt();

        int sum = na + nb + nc;
        double avg = (double)sum / 3;

        System.out.printf("それらの合計は%dです。\n", sum);
        System.out.printf("それらの平均は%fです。\n", avg);
        System.out.printf("それらの平均は%.1fです。\n", avg);
    }
}
L:\>java Lesson014
3つの整数を入力してください。
整数A:87
整数B:45
整数C:59
それらの合計は191です。
それらの平均は63.666667です。
それらの平均は63.7です。

Objective-C

D

VB

VB.NET

Delphi

更新日 : 2010.09.24
program Project1;

{$APPTYPE CONSOLE}

uses
    SysUtils;
var
    na, nb, nc: Integer;
    sum:        Integer;
    avg:        Double;
begin
    writeln('3つの整数を入力してください。');

    write('整数A:');
    read(na);

    write('整数B:');
    read(nb);

    write('整数C:');
    read(nc);

    sum := na + nb + nc;
    avg := sum / 3.0;

    write(format('それらの合計は%dです。'#13#10, [sum]));
    write(format('それらの平均は%fです。'#13#10, [avg]));
    write(format('それらの平均は%.1fです。'#13#10, [avg]));
end.
S:\>lesson014\project1.exe
3つの整数を入力してください。
整数A:87
整数B:45
整数C:59
それらの合計は191です。
それらの平均は63.67です。
それらの平均は63.7です。

Ada

PL/SQL

T-SQL

関数型

inserted by FC2 system