Top >C# 練習問題集

1. 表示、変数、演算

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

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( ...の部分のみ記述します。

練習問題 1 - 1

“Hello World”と表示するプログラムを作成しなさい。


static void Main(string[] args)
{
    Console.WriteLine("Hello World");
}

練習問題 1 - 2

int 型の変数 x に数値 11 を代入し、xの値を“x=11”のように表示するプログラムを作成しなさい。


static void Main(string[] args)
{
    int x;

    x = 11;

    Console.WriteLine("x=" + x);
}

または、

    Console.WriteLine("x={0}", x);

練習問題 1 - 3

int 型の変数 x、y に数値 13、17 を代入し、x、y の値を“x=13,y=17”のように表示するプログラムを作成しなさい。


static void Main(string[] args)
{
    int x;
    int y;

    x = 13;
    y = 17;

    Console.WriteLine("x=" + x + ",y=" + y);
}

または、

    Console.WriteLine("x={0},y={1}", x, y);

練習問題 1 - 4

int 型の変数 x に数値 13 と 17 の和を代入し、x の値を表示するプログラムを作成しなさい。


static void Main(string[] args)
{
    int x;

    x = 13 + 17;

    Console.WriteLine("x=" + x);
}

練習問題 1 - 5

数値 13 と 17 の積を表示するプログラムを作成しなさい。ただし、変数を使用しないこと。


static void Main(string[] args)
{
    Console.WriteLine( 13 * 17 );
}

練習問題 1 - 6

次のプログラムを作成しなさい。

  • int 型の変数 x に数値 7 を代入する。
  • 変数 x の値を 3 倍にする。
  • 変数 x の値を表示する。
  • 変数 x の値を半分(1/2)にする。
  • 変数 x の値を表示する。

static void Main(string[] args)
{
    int x;

    x = 7;
    x *= 3;
    Console.WriteLine("x=" + x);
    x /= 2;
    Console.WriteLine("x=" + x);
}

練習問題 1 - 7

int 型の変数 x、y に任意の数値を代入し、x の値を y に、y の値を x に入れ替えて x と y の値を表示するプログラムを作成しなさい。


static void Main(string[] args)
{
    int x, y, t;

    x = 3;
    y = 7;

    t = x;
    x = y;
    y = t;

    Console.WriteLine("x={0},y={1}", x, y);
}

練習問題 1 - 8

int 型の変数 x、y に数値 19、23 を代入し、その積を変数 z に代入して z の値を表示するプログラムを作成しなさい。


static void Main(string[] args)
{
    int x, y, z;

    x = 19;
    y = 23;
    z = x * y;

    Console.WriteLine("z=" + z);
}

練習問題 1 - 9

int 型の変数 x に任意の数値を代入し、x を 2 倍、3 倍、4 倍した結果を表示するプログラムを作成しなさい。


static void Main(string[] args)
{
    int x = 12;

    Console.WriteLine( x * 2 );
    Console.WriteLine( x * 3 );
    Console.WriteLine( x * 4 );
}

練習問題 1 - 10

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


static void Main(string[] args)
{
    int x = 3;

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

練習問題 1 - 11

int 型の変数 x に任意の数値を代入し、x を x より小さい任意の数値で割った商と余りを表示するプログラムを作成しなさい。


static void Main(string[] args)
{
    int x = 13;

    Console.WriteLine("商={0}, 余り={1}", x / 5, x % 5);
}

練習問題 1 - 12

int 型の変数 x に任意の数値を代入し、インクリメント演算子、デクリメント演算子を適用して結果を表示することにより演算子の効果を確認するプログラムを作成しなさい。


static void Main(string[] args)
{
    int x = 10;

    x++;
    Console.WriteLine(x);
    x--;
    x--;
    Console.WriteLine(x);
}
PAPER BOWL
NEZEN