반응형
뇌를 자극하는 C# 5.0 프로그래밍 Chapter5 연습문제
1. 다음과 같은 결과를 출력하는 프로그램을 for 문을 이용하여 작성하세요. 규칙은 첫 번째 줄에 별 하나, 두 번째 줄에 별 둘, 세 번째 줄에 별 셋… 이런 식으로 5개의 별이 찍힐 때까지 반복합니다(힌트:for문 블록 안에 for 문 블록을 넣으세요)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | using System; namespace Csharp_practice { class Program { static void Main(string[] args) { for (int i = 0; i < 5; i++) //총 다섯 개의 줄을 출력한다. { for (int j = 0; j < i + 1; j++) { Console.Write("*"); } Console.WriteLine(""); //한 줄이 끝나면 개행한다(줄바꿈) } } } } | cs |
출력결과:
2. 다음과 같은 결과를 출력하는 프로그램을 for 문을 이용하여 작성하세요.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | using System; namespace Csharp_practice { class Program { static void Main(string[] args) { for (int i = 0; i < 5; i++) //총 다섯 개의 줄을 출력한다. { for (int j = 5; j > i; j--) { Console.Write("*"); } Console.WriteLine(""); //한 줄이 끝나면 개행한다(줄바꿈) } } } } | cs |
출력결과:
3. 1번과 2번을 for문 대신 while 문과 do 문으로 바꿔서 각각 작성하세요.
- while문
1번
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | using System; namespace Csharp_practice { class Program { static void Main(string[] args) { int i = 0; int j = 0; while (i < 5) //총 다섯 개의 줄을 출력한다. { while (j < i + 1) { Console.Write("*"); j++; } Console.WriteLine(""); //한 줄이 끝나면 개행한다(줄바꿈) i++; j = 0; //j를 0으로 초기화한다. } } } } | cs |
2번
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | using System; namespace Csharp_practice { class Program { static void Main(string[] args) { int i = 0; int j = 5; while (i < 5) //총 다섯 개의 줄을 출력한다. { while (j > i) { Console.Write("*"); j--; } Console.WriteLine(""); //한 줄이 끝나면 개행한다(줄바꿈) i++; j = 5; //j를 5으로 초기화한다. } } } } | cs |
- do while문
1번
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; namespace Csharp_practice { class Program { static void Main(string[] args) { int i = 0; int j = 0; do { do { Console.Write("*"); j++; } while (j < i+1); Console.WriteLine(""); //한 줄이 끝나면 개행한다(줄바꿈) i++; j = 0; //j를 0으로 초기화 한다. } while (i < 5); //총 다섯 개의 줄을 출력한다. } } } | cs |
2번
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 | using System; namespace Csharp_practice { class Program { static void Main(string[] args) { int i = 0; int j = 5; do { do { Console.Write("*"); j--; } while (j > i); Console.WriteLine(""); //한 줄이 끝나면 개행한다(줄바꿈) i++; j = 5; //j를 5로 초기화 한다. } while (i < 5); //총 다섯 개의 줄을 출력한다. } } } | cs |
4. 다음과 같이 사용자로부터 입력받은 횟수만큼 별을 반복 출력하는 프로그램을 작성하세요. 단, 입력받은 수가 0보다 작거나 같을 경우 "0보다 같거나 작은 숫자는 사용할 수 없습니다."라는 메시지를 띄우고 프로그램을 종료합니다.
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 31 | using System; namespace Csharp_practice { class Program { static void Main(string[] args) { Console.Write("반복 횟수를 입력하세요 : "); string input = Console.ReadLine(); //숫자를ㄹ 입력 받는다. int number = Convert.ToInt32(input); //입력받은 문자열을 int형으로 바꿔준다. if(number <= 0) //0보다 작거나 같을 경우 메시지 출력 후, 프로그램 종료 { Console.WriteLine("0보다 작거나 같은 수는 입력할 수 없습니다."); } else { for(int i = 0; i < number; i++) { for(int j = 0; j < i+1; j++) { Console.Write("*"); } Console.WriteLine(); } } } } } | cs |
출력결과:
- 0보다 작거나 같은 수를 입력받은 경우
- 0보다 큰 수를 입력받은 경우
반응형
'programming' 카테고리의 다른 글
뇌를 자극하는 C# 5.0 프로그래밍 Chapter6 연습문제 풀이 (0) | 2016.01.04 |
---|---|
프로젝트 오일러(Project Euler) 22번 문제 (0) | 2016.01.04 |
뇌를 자극하는 C# 5.0 프로그래밍 Chapter4 연습문제 풀이 (0) | 2015.12.30 |
[안드로이드]안드로이드 4대 컴포넌트 (3) | 2015.11.06 |
[안드로이드]Android SDK version (0) | 2015.11.05 |