C#/수업 내용

[C#] 2021-08-20 오후 for문 연습 문제 11~17, challenge

JSH1 2021. 8. 20. 16:04

문제11

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace hello_world
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("숫자를 입력하세요: ");
            int number = Convert.ToInt32(Console.ReadLine());
            int check = number;
            Console.WriteLine();

            for (int i = 0; i < check; i++)
            {
                if (number > 1)
                {
                    number--;
                }

                else if (number == 1)
                {
                    Console.WriteLine("{0}보다 작은 숫자는 {1}개 입니다.", check, i);
                }

            }
        }
    }
}


문제12

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace hello_world
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(10 - i);
            }
        }
    }
}


문제13

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace hello_world
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 8; i++)
            {
                if (0 == i % 2)
                {
                    Console.WriteLine("*****");
                }
                else if (0 != i % 2)
                {
                    Console.WriteLine("=====");
                }
            }

        }
    }
}


문제14

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace hello_world
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("구구단의 단수를 입력하세요: ");
            int number = Convert.ToInt32(Console.ReadLine());

            for (int i = 0; i < 9; i++)
            {
                Console.WriteLine("{0} x {1} = {2}", number, i + 1, number * (i + 1));
            }

        }
    }
}


문제15

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace hello_world
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("숫자를 입력하세요: ");
            int number = Convert.ToInt32(Console.ReadLine());
            int count = 0;

            for (int i = 0; i < number; i++)
            {
                count += i + 1;
            }
            Console.WriteLine("1 ~ {0}까지의 합은 {1}입니다.", number, count);

        }
    }
}


challenge

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace hello_world
{
    class Program
    {
        static void Main(string[] args)
        {
            Random dice = new Random();

            for (int i = 0; i < 6; i++)
            {
                Console.Write("\n추첨하시려면 엔터를 누르세요: ");
                Console.ReadLine();
                Console.WriteLine("당첨번호: {0}번", dice.Next(1, 45 + 1));
            }
            Console.Write("\n추첨하시려면 엔터를 누르세요: ");
            Console.ReadLine();
            Console.WriteLine("보너스번호: {0}번", dice.Next(1, 45 + 1));

        }
    }
}


문제16

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace hello_world
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("x의 y승을 계산합니다.");
            Console.Write("x를 입력하세요: ");
            int x = int.Parse(Console.ReadLine());
            Console.Write("y를 입력하세요: ");
            int y = int.Parse(Console.ReadLine());

            int count = 1;
            for(int i=0; i<y; i++)
            {
                count *= x;
            }

            Console.WriteLine("{0}의 {1}승은 {2}입니다.", x, y, count);
        }
    }
}


문제17

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace hello_world
{
    class Program
    {
        static void Main(string[] args)
        {
            float max = 0;
            float min = 1000f;

            int population = 5;
            float height = 0;
            float sum = 0;

            for (int i = 0; i < population; i++)
            {
                Console.Write("키를 입력하세요 (단위:cm): ");
                height = float.Parse(Console.ReadLine());

                if(max < height)
                {
                    max = height;
                }

                if (min > height)
                {
                    min = height;
                }

                sum += height;
            }

            Console.WriteLine("평균: {0}cm, 최대: {1}cm, 최소: {2}cm", sum / population, max, min);
        }
    }
}


 

'C# > 수업 내용' 카테고리의 다른 글

[C#] switch case문  (0) 2021.08.23
[C#] 2021-8-20 challenge 문제, Random 중복값 방지  (0) 2021.08.20
[C#] 2021-08-20 오후 for문 연습 문제 1~10  (0) 2021.08.20
[C#] for, foreach  (0) 2021.08.20
2021-08-20 오전  (0) 2021.08.20