Top >C# 練習問題集

2. 入力

プログラムの実行には、以下のようなコードが必要です

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercise
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

または、

using System;

class Program
{
    static void Main(string[] args)
    {
    }
}

以降、解答例としては static void Main( ...の部分のみ記述します

練習問題 2 - 1

string 型の変数 s に文字列を入力し、s の値を表示するプログラムを作成しなさい


static void Main(string[] args)
{
    string  s = Console.ReadLine();

    Console.WriteLine(s);
}

練習問題 2 - 2

int 型の変数 x に数値を入力し、x の値を表示するプログラムを作成しなさい


static void Main(string[] args)
{
    int x = int.Parse(Console.ReadLine());

    Console.WriteLine(x);
}

練習問題 2 - 3

int 型の変数 x に数値を入力し、x を 1 乗、2 乗、3 乗した結果を表示するプログラムを作成しなさい


static void Main(string[] args)
{
    int x = int.Parse(Console.ReadLine());

    Console.WriteLine(x);
    Console.WriteLine(x * x);
    Console.WriteLine(x * x * x);
}

練習問題 2 - 4

int 型の変数 x、y にそれぞれ数値を入力し、x と y の和、差(x-y)、積、商と余り (x÷y)、を表示するプログラムを作成しなさい


static void Main(string[] args)
{
    int x = int.Parse(Console.ReadLine());
    int y = int.Parse(Console.ReadLine());

    Console.WriteLine("和 " + ( x + y ));
    Console.WriteLine("差 " + ( x - y ));
    Console.WriteLine("積 " + ( x * y ));
    Console.WriteLine("商 " + ( x / y ));
    Console.WriteLine("余り " + ( x % y ));
}

練習問題 2 - 5

2つの整数値を入力し、平均値を求めるプログラムを作成しなさい

※ 計算は整数で行い、小数点以下は切り捨ててよい

static void Main(string[] args)
{
    int x = int.Parse(Console.ReadLine());
    int y = int.Parse(Console.ReadLine());

    Console.WriteLine("平均値=" + ( ( x + y ) / 2 ));
}

練習問題 2 - 6

年齢を訊ね、生まれてから現在までの、おおよその日数を表示するプログラムを作成しなさい


static void Main(string[] args)
{
    Console.WriteLine("あなたの年齢は?");
    int age = int.Parse(Console.ReadLine());

    Console.WriteLine("生まれてから今まで、およそ {0}日です", age * 365);
}
PAPER BOWL
NEZEN