2023. 10. 5. 20:03ㆍIT/C#
// 연습문제 6
{ // 1. 구구단 2단을 만들기
/* for (int i = 2; i < 10; i++)
{
Console.WriteLine("2 x " + i + " = " + 2 * i);
}
*/
}
{ // 2. 입력받은 데이터로 구구단 만들기
// 숫자를 하나 입력받아서 2 ~9 까지 곱한 결과를 나타냅니다.
// -입력받은 데이터가 숫자가 아니라면 - “`숫자가 아닙니다.`
/* Console.WriteLine("출력하고 싶은 단을 입력해주세요");
string input = Console.ReadLine();
int num;
bool isInt = int.TryParse(input, out num);
if (isInt)
{
for (int i = 2; i < 10; i++)
{
Console.WriteLine(num + " x " + i + " = " + num * i);
}
}
else
{
Console.WriteLine("숫자가 아닙니다.");
}*/
}
{ // 3.피보나치 수열 구하기
/*int a1 = 1;
int a2 = 1;
int count = 1;
while (count <= 10)
{
Console.Write(a1 + " ");
int result = a1 + a2;
a1 = a2;
a2 = result;
count++;
}
*/
}
{ // 4. 입력받은 수만큼 피보나치 수열 구하기
// 숫자를 입력하면 입력한 숫자만큼 피보나치 수열 출력하기
// -최초 메시지 출력 - “`몇개의 피보나치 수열을 출력하고 싶으신가요 ?`”
// -입력받은 데이터가 숫자가 아니라면 - “`숫자가 아닙니다.`
// -숫자가 0 이하라면 - “`양수를 입력해주세요.`”
// -숫자가 47 이상이라면 ? - “`숫자가 너무 큽니다.`”
Console.WriteLine("몇 개의 피보나치 수열을 출력하고 싶으신가요?");
int a1 = 1;
int a2 = 1;
int count = 1;
string input = Console.ReadLine();
int x;
bool isInt = int.TryParse(input, out x);
if (isInt)
{
if (x > 0 && 47 > x)
{
while (count <= x)
{
Console.Write(a1 + " ");
int result = a1 + a2;
a1 = a2;
a2 = result;
count++;
}
}
else if (0 >= x)
{
Console.WriteLine("양수를 입력해주세요");
}
else
{
Console.WriteLine("숫자가 너무 큽니다");
}
}
else
{
Console.WriteLine("숫자가 아닙니다.");
}
}