본문 바로가기
공부/C#

C# 공부(4) 41~50

by 라이티아 2024. 8. 24.

41. swich문

using System;

namespace A041
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("입력 : ");
            int value = int.Parse(Console.ReadLine());
            string grade = default(string);

            switch (value / 10)
            {
                case 10:
                case 9:
                    grade = "A";
                    break;
                case 8:
                    grade = "B";
                    break;
                case 7:
                    grade = "C";
                    break;
                case 6:
                    grade = "D";
                    break;
                default :
                    grade = "0";
                    break;
            }
            Console.WriteLine(grade);
        }
    }
}

 

결과

입력 : 72
C

 

평범한 swicth문이다. 항상 볼때마다 잘 쓰면 좋을 것 같은데 잘 쓰기가 어려운 문법중 1개라고 생각한다.

 

42. BMI계산기

using System;

namespace A042
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("키 입력 : ");
            double h = double.Parse(Console.ReadLine());
            h /= 100;

            Console.Write("체중 입력 : ");
            double w = double.Parse(Console.ReadLine());
            double bmi = w / (h * h);

            string comment = default(string);
            if (bmi < 20)
                comment = "저체중";
            else if (bmi < 25)
                comment = "정상";
            else if (bmi < 30)
                comment = "경도 비만";
            else if (bmi < 35)
                comment = "비만";

            Console.WriteLine(comment);
        }
    }
}

 

결과

키 입력 : 170
체중 입력 : 50
저체중

 

그냥 if문 사용한 bmi 계산기다. 

 

43. 반복문

using System;

namespace A043
{
    class Program
    {
        static void Main(string[] args)
        {
            // 1부터 100까지 합
            int sum = 0;
            for(int i = 1; i <= 100; i++)
                sum += i;
            Console.WriteLine(sum);

            // 1부터 100까지 홀수 합
            sum = 0;
            for (int i = 1;i <= 100; i++)
                if((i % 2 ) == 1) sum += i;
            Console.WriteLine(sum);

            // 1+1/ 2+1/ 3+ 2+ 1/ .....
            double sub = 0.0;
            for (int i = 1; i <= 100; i++)
                sub += 1.0 / i;
            Console.WriteLine(sub);
        }
    }
}

 

결과

5050
2500
5.187377517639621

 

그냥 반복문 사용 코드이다. 근데 3번에 사용한 역수는 처음보는 수학 개념은데 뭘까...

 

사사. 반복문으로 2진, 8진, 16진수 출력

using System;

namespace A04_
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine($"{"10진수" : 5} {"2진수" : 8} {"8진수" : 3} {"16진수" : 4}");

            for (int i = 1; i <= 128; i++)
            {
                Console.WriteLine($"{i , 7} {Convert.ToString(i, 2).PadLeft(8,'0') : 10} {Convert.ToString(i, 8) : 5} {Convert.ToString(i, 16) : 16}");
            }
        }
    }
}

 

결과

10진수 2진수 8진수 16진수
      1 00000001 1 1
      2 00000010 2 2
      3 00000011 3 3
      4 00000100 4 4
      5 00000101 5 5
      6 00000110 6 6
      7 00000111 7 7
      8 00001000 10 8
      9 00001001 11 9
     10 00001010 12 a
     11 00001011 13 b
     12 00001100 14 c
     13 00001101 15 d
     14 00001110 16 e

 

for루프로 사용된 10진수 i값을 각 진수들로 출력해보는 코드이다.

이때 Convert.Tostring(변수, 원하는 진수); 가 사용된다

PadLeft는 score를 제작할때 사용하면 좋을 것 같다

 

45. 반복문으로 구구단 출력

using System;

namespace A045
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("원하는 구구단 : ");
            int value = int.Parse(Console.ReadLine());

            for (int i = 1; i <= 9; i++)
            {
                Console.WriteLine($"{value} * {i} = {value*i}");
            }
        }
    }
}

 

결과

원하는 구구단 : 5
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45

 

반복문 그만!!! 그만!!!

 

46. 평균, 최소, 최대값 구하기

using System;
namespace A046
{
    class Program
    {
        static void Main(string[] args)
        {
            double max = double.MinValue;
            double min = double.MaxValue;
            double sum = 0;

            for (int i = 0; i < 5; i++)
            {
                Console.Write("키 입력 : ");
                double h= double.Parse(Console.ReadLine());

                if( h > max ) max = h;
                if( h < min ) min = h;
                sum += h;
            }
            Console.WriteLine("평균 {0} 최대 {1} 최소 {2}", sum / 5, max, min);
        }
    }
}

 

결과

키 입력 : 60
키 입력 : 70
키 입력 : 80
키 입력 : 90
키 입력 : 100
평균 80 최대 100 최소 60

 

그냥 평균값 구하는 코드

 

47. x의 y승 구하기

using System;

namespace A047
{
    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 sum = 1;
            for (int i = 0; i < y; i++)
            {
                sum *= x;
            }
            Console.WriteLine($"{sum}");
        }
    }
}

 

결과

x의 y승 계산
x 입력 : 2
y 입력 : 6
64

 

그냥 간단한 x y승 구하기 코드

 

48. 팩토리얼 구하기

using System;
namespace A048
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("n!계산");
            Console.Write("n입력 : ");
            int n = int.Parse(Console.ReadLine());

            int sum = 1;
            for(int i = 1; i <= n; i++)
            {
                sum *= i;
            }
            Console.WriteLine("팩토리얼 값 {0}", sum);
        }
    }
}

 

결과

n!계산
n입력 : 5
팩토리얼 값 120

 

간단 코딩 그만!!!!

 

49. 소수 판단하기

using System;

namespace A049
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("소수 판별 하고싶은 숫자 : ");
            int n = int.Parse(Console.ReadLine());
            int i;
            for (i = 2; i < n; i++)
            {
                if (n % i == 0)
                {
                    Console.WriteLine($"{n}은 소수가 아님"); break;
                }
            }
            if (n ==i) Console.WriteLine($"{n}은 소수");
        }
    }
}

 

결과

소수 판별 하고싶은 숫자 : 7
7은 소수

 

for루프의 i++구조를 이용하는 코드

 

50. 원주율의 계산

using System;

namespace A050
{
    class Program
    {
        static void Main(string[] args)
        {
            bool sign = false;
            double pi = 0;

            for (int i = 1; i <= 10000; i+= 2)
            {
                if(sign == false)
                {
                    pi += 1.0 / i;
                    sign = true;
                }
                else
                {
                    pi -= 1.0 / i;
                    sign = false;
                }
                Console.WriteLine("i = {0}, PI = {1}", i, 4 * pi);
            }
        }
    }
}

 

결과

i = 9981, PI = 3.1417930142369483
i = 9983, PI = 3.1413923330789797
i = 9985, PI = 3.141792933980332
i = 9987, PI = 3.141392413303452
i = 9989, PI = 3.141792853787985
i = 9991, PI = 3.141392493463693
i = 9993, PI = 3.1417927736598306
i = 9995, PI = 3.1413925735597807
i = 9997, PI = 3.1417926935957916
i = 9999, PI = 3.141392653591791

 

for 반복문을 활용해서 원주율을 계산하는 코드이다.

for 안쪽에서 변화를 줄수 있음을 알려준다

 

 

C-sharp-Studying/Part1 at main · NoNamed02/C-sharp-Studying

C#언어 공부 정리. Contribute to NoNamed02/C-sharp-Studying development by creating an account on GitHub.

github.com

 

'공부 > C#' 카테고리의 다른 글

C# 공부(6) 57~60  (0) 2024.08.25
C# 공부(5) 51~56 (Part 1 End)  (0) 2024.08.24
C# 공부(3) 31~40  (0) 2024.08.23
C# 공부(2) 21~30  (0) 2024.08.22
C# 공부(2) 11~20  (0) 2024.08.21