[C#] 연산자

 C# 또한 C와 동일한 연산자를 사용한다.

일반적인 사칙연산과 나머지 연산, 논리 연산자와 그에 대한 연산자 우선 순위 또한 동일하다

참고 : https://learn.microsoft.com/ko-kr/dotnet/csharp/language-reference/operators/


예제-1) n의 배수

출처 : 프로그래머스

- 제한 사항은 생략
- 배수에 해당하는 경우, n으로 num을 나누었을 때 나머지 값은 0

풀이)
1
2
3
4
5
6
7
8
9
10
11
using System;
 
public class Solution {
    public int solution(int num, int n) {
        int answer = 0;
        
        if(num%n == 0) answer = 1;
        
        return answer;
    }
}
cs



예제-2) n의 배수-공배수
출처 : 프로그래머스
- 제한 사항은 생략
- number에 대해 n과 m의 나머지 연산(%)의 값이 0 이 되어야 공배수임이 증명됨

풀이)

1
2
3
4
5
6
7
8
9
10
11
using System;
 
public class Solution {
    public int solution(int number, int n, int m) {
        int answer = 0;
        
        if((number%n == 0&& (number%m == 0)) answer = 1;
        
        return answer;
    }
}
cs



댓글