Programming Language/C#

[C#] TQC+ 문제 CS_204 :

공구일 2025. 5. 8. 11:09
728x90

🔍C#: CS_204 .

  입력된 두 값 사이 값들 중 소수(1와 자기자신만을 인수로 갖는 수)만을 출력하시오. 입력된 두 값이 1미만 100 초과일 경우에는 error를 출력하시오.

 

 

1. 문제 정답

using System;

namespace CS_204 {
    class CS_204 {
        static void Main(string[] args){
            int start = int.TryParse(Console.ReadLine(), out int num1) && num1 > 0 && num1 < 101 ? num1 : 0;
            int end = int.TryParse(Console.ReadLine(), out int num2) && num2 > 0 && num2 < 101 ? num2 : 0;
            
            if(start == 0 || end == 0){
                Console.Write("error");
            } else{
                for(int n = start; n <= end; n++){
                    int count = 0;
                    for(int i = 1; i <= n; i++){
                        if(n % i == 0) count++;
                        if(count > 2) break;
                    }
                    if(count == 2) Console.WriteLine(n);
                }
            }
        }
    }
}

 

2. 정리

- 자기 자신과 1만 나머지가 0인 경우를 구하면 되므로 이중 포문을 이용해줍니다.

728x90