logo

C/C++'da Modül Operatörü

Modülü anlamadan önce Şebeke , terim hakkında bilgi sahibi olmamız gerekiyor Şebeke . Programlamada operatör sembolü, derleyiciye, geçirilen işleme bağlı olarak belirli bir sayıda belirli bir işlemi gerçekleştirmesini söyler.

Sayılar üzerinde farklı işlemleri (+, -, *, /) gerçekleştirmek için kullanılır. Programdaki çeşitli operatörleri kullanarak programcının mantıksal ve matematiksel yeteneğini geliştirmeye yardımcı olur. Bu dört temel operatörün dışında aşağıdaki gibi başka operatörler de vardır: modül operatörü (%), kapsam çözümleme operatörü (::), vb.

Java'da birleştirme sıralaması

Bu bölümde şu konulara odaklanacağız: modül operatörü.

C/C++'da Modül Operatörü

C ve C++'daki modül operatörü nedir?

modül operatörü çeşitli programlama dillerinde kullanılan bir semboldür. Yüzde sembolüyle gösterilir ( % ). Aritmetik operatörde kullanılan bir modül operatörüdür. Geri kalanı belirler. Bazı durumlarda kalan 0 olabilir, bu sayının bölene tamamen bölünebildiği anlamına gelir.

Sözdizimi:

 rem = a % b 

Yukarıdaki Sözdiziminde a ve b iki tam sayıdır ve % (Yüzde) sembolü a'yı b'ye bölen ve kalanı veren bir modül operatörüdür.

Modül Operatörünün Geri Dönüş Olanakları

Yalnızca bir kalan değer döndürmek için ilk sayının ikinci sayıya bölünmesiyle ilgili olasılıklar aşağıda verilmiştir.

  1. A değişkeni ikinci sayıya (b) tamamen bölünebiliyorsa sıfır (0) döndürür veya kalan 0 olur.
  2. a değişkeni ikinci sayıya (b) tam olarak bölünemiyorsa, [1, a - 1] aralığında sayısal bir değer döndürür. Veya kalanı sıfırdan farklı bir tamsayı değerine döndürdüğünü söyleyebiliriz.
  3. İlk sayı (a) sıfırdan farklı ve ikinci sayı 0 ise derleme zamanında hata verir.

Modül Operatörü C/C++'da nasıl çalışır?

Modül operatörü, son kullanıcı tarafından alınan iki işlenenle çalışır. Daha sonra birinci sayıyı ikinci sayıya bölerek kalanı bulur. Modül operatörünün işlevselliğini gösteren aşağıdaki örneği anlayalım.

beats kulaklık nasıl eşleştirilir

Örnek : 8 ile 5 arasında modül operatörü yaptığımızda yani %8 5 kalan 3 değerini verir çünkü 8 5'e bölündüğünde bölüm olarak 1, kalan olarak 3 verir.

Benzer şekilde %7 2, kalan olarak 1 sonucunu verir, çünkü 7, 2'ye bölündüğünde bölüm olarak 3, kalan olarak da 1 verir.

Örnek 1: C'de Modül Operatörünü uygulayacak bir program yazın.

Mod.c

 #include #include void main() { int a, b; int res; // store the resultant of modulus expression a = 5; b = 2; res = a % b; // define modulus expression printf(' Modulus returns a remainder: %d', res); res = b % a; // define modulus expression printf(' 
 Modulus returns a remainder: %d', res); a = 10; b = 5; res = a % b; // define modulus expression printf(' 
 Modulus returns a remainder: %d', res); getch(); } 

Çıktı:

 Modulus returns a remainder: 1 Modulus returns a remainder: 2 Modulus returns a remainder: 0 

Not: Bir kayan sayıyı başka bir sayıya böldüğümüzde, derlenmiş zamanlayıcı hatasını geçersiz bir işlenen olarak döndürür. Dolayısıyla float sayı ile çalışmadığını söyleyebiliriz.

C++'ta modül operatörünü uygulayan program.

javac tanınmıyor

Mod3.cpp

 #include using namespace std; int main(void) { int a, b; int res; // store the resultant of modulus expression a = 5; b = 2; res = a % b; // modulus expression cout &lt;<' modulus returns a remainder: ' << res; res="b" % a; expression cout <<'
 b="5;" b; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Modulus returns a remainder: 1 Modulus returns a remainder: 2 Modulus returns a remainder: 0 </pre> <h2>Modulus Operator in C++</h2> <p> <strong>Mode4.cpp</strong> </p> <pre> #include using namespace std; int main(void) { int a, b; int res; // store the result of modulus expression a = -5; b = 2; res = a % b; // modulus expression cout &lt;<' modulus returns a remainder: ' << res; b="-3;" res="a" % b; expression cout <<'
 return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Modulus returns a remainder: -1 Modulus returns a remainder: 2 Modulus returns a remainder: -1 </pre> <h3>Chaining of Modulus Operator</h3> <p>We can use the Chaining of Modulus Operator to perform modular division on more than two operands in a single statement. Following is the pseudo-code for the chaining of modulus operator, as given below.</p> <pre> res = operand1 % operand2 % operand3 % .... % operand_n </pre> <p>Let&apos;s consider the program of Chaining of Modulus Operator to take more than two operands.</p> <p> <strong>Mode5.cpp</strong> </p> <pre> #include using namespace std; int main() { // Use of modulus operator in C++ int x = 14; int y = 6; int z = 3; int modulo = x % y % z; // x % y returns 2, and 2 % z returns 2 cout &lt;&lt; &apos;Modulus is : &apos;&lt;&lt; modulo; } </pre> <p> <strong>Output:</strong> </p> <pre> Modulus is: 2 </pre> <hr></'></pre></'>

C++'da Modül Operatörü

Mod4.cpp

 #include using namespace std; int main(void) { int a, b; int res; // store the result of modulus expression a = -5; b = 2; res = a % b; // modulus expression cout &lt;<\' modulus returns a remainder: \' << res; b="-3;" res="a" % b; expression cout <<\'
 return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Modulus returns a remainder: -1 Modulus returns a remainder: 2 Modulus returns a remainder: -1 </pre> <h3>Chaining of Modulus Operator</h3> <p>We can use the Chaining of Modulus Operator to perform modular division on more than two operands in a single statement. Following is the pseudo-code for the chaining of modulus operator, as given below.</p> <pre> res = operand1 % operand2 % operand3 % .... % operand_n </pre> <p>Let&apos;s consider the program of Chaining of Modulus Operator to take more than two operands.</p> <p> <strong>Mode5.cpp</strong> </p> <pre> #include using namespace std; int main() { // Use of modulus operator in C++ int x = 14; int y = 6; int z = 3; int modulo = x % y % z; // x % y returns 2, and 2 % z returns 2 cout &lt;&lt; &apos;Modulus is : &apos;&lt;&lt; modulo; } </pre> <p> <strong>Output:</strong> </p> <pre> Modulus is: 2 </pre> <hr></\'>

Modül Operatörünün Zincirlenmesi

Tek bir ifadede ikiden fazla işlenen üzerinde modüler bölme işlemi gerçekleştirmek için Modül Operatör Zincirlemesini kullanabiliriz. Aşağıda modül operatörünün zincirlenmesine yönelik sözde kod verilmiştir.

 res = operand1 % operand2 % operand3 % .... % operand_n 

İkiden fazla işlenen alacak şekilde Modül Operatörü Zincirleme programını ele alalım.

Mod5.cpp

 #include using namespace std; int main() { // Use of modulus operator in C++ int x = 14; int y = 6; int z = 3; int modulo = x % y % z; // x % y returns 2, and 2 % z returns 2 cout &lt;&lt; &apos;Modulus is : &apos;&lt;&lt; modulo; } 

Çıktı:

 Modulus is: 2