이것이 C# 이다. 5장 연습문제

2020. 1. 16. 20:02C# 언어/이것이 C# 이다. 연습문제

반응형

1. 다음과 같은 결과를 출력하는 프로그램을 for 문을 이용하여 작성하세요. 

*
**
***
****
*****

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;
using static System.Console;
using System.Text;
using System.IO;
 
namespace baekjoon
{
    class MainApp
    {
        static void Main()
        {
            for(int i = 0; i<5;i++// 몇개의 줄을 담당
            {
                for(int j = 0; j <= i; j++// 출력하는 *을 담당
                {
                    Write("*");
                }
                WriteLine();
            }
        }
    }
}

2. 다음과 같은 결과를 출력하는 프로그램을 for 문을 이용하여 작성하세요.

*****
****
***
**
*

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;
using static System.Console;
using System.Text;
using System.IO;
 
namespace baekjoon
{
    class MainApp
    {
        static void Main()
        {
            for (int i = 0; i < 5; i++// 몇개의 줄을 담당
            {
                for (int j = 5; j > i; j--// 출력하는 *을 담당
                {
                    Write("*");
                }
                WriteLine();
            }
        }
    }
}

 

3. 1번과 2번을 for 문 대신 while 문과 do 문으로 바꿔서 각각 작성하세요.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System;
using static System.Console;
using System.Text;
using System.IO;
 
namespace baekjoon
{
    class MainApp
    {
        static void Main()
        {
            int i = 0;
            while(i < 5// 표시할 줄 갯수 담당
            {
                int j = 0;
                while(j <= i) // 표시할 * 갯수 담당
                {
                    Write("*");
                    j++;
                }
                WriteLine();
                ++i;
            }
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;
using static System.Console;
using System.Text;
using System.IO;
 
namespace baekjoon
{
    class MainApp
    {
        static void Main()
        {
            int i = 5;
 
            do
            {
                int j = 0;
                while (j < i)
                {
                    Write("*");
                    j++;
                }
                WriteLine();
                i--;
            }
            while (i > 0);
        }
    }
}

 

4. 다음과 같이 사용자로부터 입력받은 횟수만큼 별을 반복 출력하는 프로그램을 작성하세요.

단, 입력받은 수가 0보다 작거나 같을 경우 "0보다 같거나 작은 숫자는 사용할 수 없습니다." 라는

메세지를 띄우고 프로그램을 종료합니다.

 

반복 횟수를 입력하세요 : -10
0보다 작거나 같은 수는 입력할 수 없습니다.

반복 횟수를 입력하세요 : 5
*
**
***
****
*****
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System;
using static System.Console;
using System.Text;
using System.IO;
 
namespace baekjoon
{
    class MainApp
    {
        static void Main()
        {
            Write("반복 횟수를 입력하세요 :");
            int input = int.Parse(Console.ReadLine());
 
            if(input <= 0)
            {
                WriteLine("0보다 작거나 같은 수는 입력할 수 없습니다.");
            }
            
            for(int i = 0; i < input; i++// 표시될 줄 갯수
            {
                for(int j = 0; j <= i; j++)
                {
                    Write("*");
                }
                WriteLine();
            }
        }
    }
}
반응형