C vai C++ valodā modulo operators (pazīstams arī kā moduļa operators) , kas apzīmēts ar %, ir an aritmētiskais operators . Moduļu nodaļas operators ražo atlikumu vesela skaitļa dalījuma, ko sauc arī par operācijas moduli.
Modulus Operatora sintakse
Ja x un y ir veseli skaitļi, tad izteiksme:
x % y;>
izrunā kā x mod y. Piemēram, 10 % 2 tiks izrunāts kā Ten mod Two.
Modulo operatora atdeves vērtība
- Ja y pilnībā sadala x, izteiksmes rezultāts ir 0.
- Ja x nav pilnībā dalāms ar y, tad rezultāts būs atlikums diapazonā [0, y-1]
- (x % un) < (x / 2) ………ja (x>= y)
- (x % y) = x ……… ja (x
- Ja y ir 0, tad dalīšana ar nulli ir kompilēšanas laika kļūda .
Modulo operatora piemērs
Zemāk ir C/C++ programma, lai demonstrētu modulo operatora darbību:
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
sql datu tipi
// 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;> }> |
>
>
Modulo operatora ierobežojumi
Moduļu operatoram ir daži ierobežojumi vai ierobežojumi. The % moduļa operators nevar piemērot peldošā komata skaitļi t.i., pludiņš vai dubultā. Ja mēģināt izmantot modulo operatoru ar peldošā komata konstantēm vai mainīgajiem, kompilators radīs kļūdu.
1. piemērs: C/C++ programma, lai demonstrētu moduļu operatora ierobežojumus.
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
// 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;> }> |
>
>
Izvade
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; ^>
Modulo operators negatīvajiem operandiem
Rezultāta zīme modulo operatoram ir atkarīga no mašīnas negatīviem operandiem, jo darbība notiek nepietiekamas vai pārpildes rezultātā.
2. piemērs: C/C++ programma, lai demonstrētu moduļu operatoru negatīviem operandiem.
C++
apakšvirkne java
// 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
// 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;> }> |
>
>Izvade
-3 0 -3>
Piezīme: Atgriešanas vērtība šajā gadījumā ir atkarīga no kompilatora.
Bieži uzdotie jautājumi par Modulo Operator
Q1. Definējiet mod.
Atbilde:
C/C++ programmēšanas valodās mod attiecas uz matemātisko darbību, kurā viens skaitlis tiek dalīts ar citu, bet atlikusī daļa tiek atgriezta.
To var veikt, izmantojot operatora modulis (%) .
Q2. Kas ir modu aritmētika?
Atbilde:
Mod aritmētika attiecas uz procesu, kurā skaitlis turpina apgriezties ap noteiktu punktu tā, ka tas vienmēr ir mazāks par konkrēto punktu. Piemēram,
Apsveriet skaitli n = 10 un punkts p = 20.
Palielinot n 10 reizes, tas būs n = 20, bet modulārajā aritmētikā tam vajadzētu būt mazākam par norādīto punktu.Viens veids, kā to izdarīt, ir izmantot modulo operatoru kā:
n++; n = n % p;>Lai uzzinātu vairāk par modulāro aritmātiku, skatiet rakstu - Modulārā aritmātika
Q3. Kāda ir atšķirība starp modulo un dalīšanas operatoru?
Atbilde:
Galvenā atšķirība starp modulo un sadalīšanas operatoru ir šāda:
- Operatora modulis (%) atgriež atlikumu pēc viena skaitļa dalīšanas ar citu.
- Sadalīt operatoru (/) atgriež koeficientu pēc viena skaitļa dalīšanas ar citu.