C 또는 C++에서는 모듈러스 연산자(모듈러스 연산자라고도 함) %로 표시되는 은 산술 연산자 . 모듈로 나눗셈 연산자는 다음을 생성합니다. 나머지 연산의 모듈러스라고도 불리는 정수 나누기.
모듈러스 연산자의 구문
x와 y가 정수이면 표현식은 다음과 같습니다.
x % y;>
다음과 같이 발음된다 x 모드 y. 예를 들어 10 % 2는 Ten mod Two로 발음됩니다.
모듈로 연산자의 반환 값
- y가 x를 완전히 나누면 표현식의 결과는 0입니다.
- x가 y로 완전히 나누어지지 않으면 결과는 [0, y-1] 범위의 나머지가 됩니다.
- (엑스 % 그리고) < (x / 2) ………if (x>= y)
- (엑스 % y) = x ……… 만약 (x
- y가 0이면 0으로 나누기 는 컴파일 타임 오류 .
모듈로 연산자의 예
다음은 모듈로 연산자의 작동을 보여주는 C/C++ 프로그램입니다.
C++
// C++ Program to demonstrate the working of modulo operator> #include> using> namespace> std;> // Driver code> int> main(>void>)> {> >int> x, y;> >int> result;> >x = 3;> >y = 4;> >// using modulo operator> >result = x % y;> >cout << result << endl;> >result = y % x;> >cout << result << endl;> >// for different values> >x = 4;> >y = 2;> >result = x % y;> >cout << result;> >return> 0;> }> // This code is contributed by Mayank Tyagi> |
>
>
씨
// C Program to illustrate the working of modulo operator> #include> int> main(>void>)> {> >int> x, y;> >int> result;> >x = 3;> >y = 4;> >// using modulo operator> >result = x % y;> >printf>(>'%d'>, result);> >result = y % x;> >printf>(>'
%d'>, result);> >// for different values> >x = 4;> >y = 2;> >result = x % y;> >printf>(>'
%d'>, result);> >return> 0;> }> |
>
>
모듈로 연산자에 대한 제한 사항
모듈로 연산자에는 제한 사항이 거의 없습니다. 그만큼 % 모듈러스 연산자 에 적용할 수 없습니다. 부동 소수점 숫자 즉, float 또는 double입니다. 부동 소수점 상수나 변수에 모듈로 연산자를 사용하려고 하면 컴파일러에서 오류가 발생합니다.
예제 1: 모듈로 연산자의 제한 사항을 보여주는 C/C++ 프로그램.
C++
// C++ Program to demonstrate the restrictions of modulo> // operator> #include> using> namespace> std;> // Driver code> int> main()> {> >float> x, y;> >x = 2.3;> >y = 1.5;> >// modulo for floating point values> >result = x % y;> >cout << result;> >return> 0;> }> // This code is contributed by Harshit Srivastava> |
>
>
씨
// C Program to illustrate the working of modulo operator> #include> int> main(>void>)> {> >float> x, y;> >float> result;> >x = 2.3;> >y = 1.5;> >// modulo for floating point values> >result = x % y;> >printf>(>'%f'>, result);> >return> 0;> }> |
>
>
산출
Compilation Error in C code :- prog.c: In function 'main': prog.c:19:16: error: invalid operands to binary % (have 'float' and 'float') result = x % y; ^>
음수 피연산자에 대한 모듈로 연산자
모듈로 연산자의 결과 부호는 언더플로 또는 오버플로의 결과로 작업이 수행되므로 음수 피연산자에 대해 기계에 따라 다릅니다.
예제 2: 음수 피연산자에 대한 모듈로 연산자를 시연하는 C/C++ 프로그램.
C++
자바 xor
// C++ Program to demonstrate the working of the modulo> // operator for negative operands> #include> using> namespace> std;> // Driver code> int> main(>void>)> {> >int> x, y;> >int> result;> >x = -3;> >y = 4;> >// modulo for negative operands> >result = x % y;> >cout << result << endl;> >x = 4;> >y = -2;> >result = x % y;> >cout << result << endl;> >x = -3;> >y = -4;> >result = x % y;> >cout << result;> >return> 0;> }> // This code is contributed by Harshit Srivastava> |
>
>
씨
// C Program to illustrate the working of the modulo> // operator with negative operands> #include> int> main(>void>)> {> >int> x, y;> >int> result;> >x = -3;> >y = 4;> >// modulo for negative operands> >result = x % y;> >printf>(>'%d'>, result);> >x = 4;> >y = -2;> >result = x % y;> >printf>(>'
%d'>, result);> >x = -3;> >y = -4;> >result = x % y;> >printf>(>'
%d'>, result);> >return> 0;> }> |
>
>산출
-3 0 -3>
메모: 이 경우 반환 값은 컴파일러에 따라 다릅니다.
모듈로 연산자에 대한 FAQ
Q1. 모드를 정의합니다.
답변:
C/C++ 프로그래밍 언어에서 mod는 한 숫자를 다른 숫자로 나누고 나머지가 반환되는 수학 연산을 나타냅니다.
다음을 사용하여 수행할 수 있습니다. 연산자 모듈(%) .
Q2. 모드 산술이란 무엇입니까?
답변:
Mod 산술은 숫자가 특정 지점보다 항상 작아지도록 특정 지점 주위를 계속해서 감싸는 프로세스를 나타냅니다. 예를 들어,
숫자를 고려하세요 n = 10 그리고 포인트 p = 20.
n을 10배 증가시키면 n = 20이 되지만 모듈러 연산에서는 지정된 점보다 훨씬 작아야 합니다.이를 수행하는 한 가지 방법은 모듈로 연산자를 다음과 같이 사용하는 것입니다.
n++; n = n % p;>모듈러 산술에 대한 자세한 내용은 다음 기사를 참조하십시오. 모듈식 산술
Q3. 모듈로와 나누기 연산자의 차이점은 무엇입니까?
답변:
모듈로와 나눗셈 연산자의 주요 차이점은 다음과 같습니다.
- 오퍼레이터 모듈 (%) 한 숫자를 다른 숫자로 나눈 나머지를 반환합니다.
- 나누기 연산자 (/) 한 숫자를 다른 숫자로 나눈 후 몫을 반환합니다.