LoginSignup
1
0

More than 3 years have passed since last update.

C# 基礎プログラミング100本ノック~31-35本目~

Last updated at Posted at 2020-04-24

こんばんは

本日も基礎プログラミング100本ノックやっていきます!

前回→26-30本目
次回→36-40本目

31本目

整数値を入力させ、その個数だけ*を、5個おきに空白(スペース)を入れて表示するプログラムを作成せよ。入力値が0以下の値の場合は何も書かなくてよい。

Sample.cs
Console.Write("input num : ");
int num = int.Parse(Console.ReadLine());

string result = "";

if (num > 0)
{
    for (int i = 0; i < num; i++)
    {
        if(i % 5 == 0 && i != 0)
        {
            result += " *";
        }
        else
        {
            result += "*";
        }
    }
}

Console.WriteLine(result);

32本目

1から20まで順に表示するが、5の倍数の場合は数字の代わりにbarと表示するプログラムを作成せよ。

Sample.cs
const int NUM = 20;

for (int i = 1; i <= NUM; i++)
{
    if(i % 5 == 0)
    {
        Console.WriteLine("bar");
    }
    else
    {
        Console.WriteLine(i);
    }
}

33本目

整数値を入力させ、1から9まで、入力値以外を表示するプログラムを作成せよ。

Sample.cs
const int NUM = 9;

Console.Write("input num : ");
int inputNum = int.Parse(Console.ReadLine());

for (int i = 1; i <= NUM; i++)
{
    if(i != inputNum)
    {
        Console.WriteLine(i);
    }
}

34本目

整数値を入力させ、1から9まで、入力値と入力値+1以外を表示するプログラムを作成せよ。入力値が9の場合は9のみ表示しない。

Sample.cs
const int NUM = 9;

Console.Write("input num : ");
int inputNum = int.Parse(Console.ReadLine());

for (int i = 1; i <= NUM; i++)
{
    if(i != inputNum && i != (inputNum +1))
    {
        Console.WriteLine(i);
    }
}

35本目

{3, 7, 0, 8, 4, 1, 9, 6, 5, 2}で初期化される大きさ10の整数型配列を宣言し、整数値を入力させ、要素番号が入力値である配列要素の値を表示するプログラムを作成せよ。入力値が配列の要素の範囲外であるかどうかのチェックは省略してよい。

Sample.cs
int[] array = { 3, 7, 0, 8, 4, 1, 9, 6, 5, 2 };

Console.Write("input num : ");
int inputNum = int.Parse(Console.ReadLine());

Console.WriteLine(array[inputNum]);

配列を定数で宣言したかったけどreadonlyもconstも使い方よくわからなかった...
誰か教えて...

リンク

基礎プロI 100本ノック 基礎編

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0