Learn how to use the modulus operator (%) and logical operators to check if a number is even or odd.
To write a C/C++ program that takes a number as input from the user and checks whether it is an even or odd number using the modulus operator (%) and logical operators.
In mathematics, numbers are classified as even or odd:
In C/C++, you can check if a number is even or odd using the modulus operator (%):
number % 2 == 0, the number is evennumber % 2 != 0, the number is odd
In this practical, you will also use the logical OR operator (||) to check multiple conditions, even though it's not strictly necessary for this simple case. This will help you understand how to combine conditions.
% gives the remainder after division. For example, 5 % 2 = 1 (odd), 4 % 2 = 0 (even).
Run the program:
Sample Input & Output:
| Type | Definition | Examples |
|---|---|---|
| Even | Divisible by 2 (remainder = 0) | 2, 4, 6, 8, 10, 12, 0, -2, -4 |
| Odd | Not divisible by 2 (remainder = 1) | 1, 3, 5, 7, 9, 11, -1, -3, -5 |
| Operator | Name | Meaning | Example |
|---|---|---|---|
|| |
OR | At least one condition is true | num % 2 == 0 || num % 4 == 0 |
&& |
AND | Both conditions must be true | num > 0 && num % 2 == 0 |
% is used to check if the number is divisible by 2.num % 2 == 0, the number is even; otherwise, it is odd.|| was demonstrated to show how multiple conditions can be combined.
You have successfully written a C program that checks whether a number is even or odd using the modulus operator (%). You have also learned how to use the logical OR operator (||) to combine conditions in a program.
% gives the remainder. num % 2 gives 0 for even numbers and 1 for odd numbers.
(num % 2 == 0 || num % 3 == 0) checks if the number is divisible by 2 OR 3.
ایک C/C++ پروگرام لکھنا جو صارف سے ایک عدد ان پٹ کے طور پر لے اور ماڈیولس آپریٹر (%) اور منطقی آپریٹرز کا استعمال کرتے ہوئے چیک کرے کہ آیا یہ جفت ہے یا طاق۔
ریاضی میں، اعداد کو جفت اور طاق میں تقسیم کیا جاتا ہے:
C/C++ میں، آپ ماڈیولس آپریٹر (%) کا استعمال کرتے ہوئے چیک کر سکتے ہیں کہ آیا کوئی عدد جفت ہے یا طاق:
number % 2 == 0، تو عدد جفت ہےnumber % 2 != 0، تو عدد طاق ہے
اس عملی میں، آپ منطقی OR آپریٹر (||) کا بھی استعمال کریں گے تاکہ متعدد شرائط کو یکجا کرنے کا طریقہ سیکھ سکیں، حالانکہ اس سادہ صورت میں اس کی ضرورت نہیں ہے۔
% تقسیم کے بعد بقیہ دیتا ہے۔ مثال کے طور پر، 5 % 2 = 1 (طاق)، 4 % 2 = 0 (جفت)۔
پروگرام چلائیں:
نمونہ ان پٹ اور آؤٹ پٹ:
| قسم | تعریف | مثالیں |
|---|---|---|
| جفت (Even) | 2 سے تقسیم ہو (بقیہ = 0) | 2، 4، 6، 8، 10، 12، 0، -2، -4 |
| طاق (Odd) | 2 سے تقسیم نہ ہو (بقیہ = 1) | 1، 3، 5، 7، 9، 11، -1، -3، -5 |
| آپریٹر | نام | معنی | مثال |
|---|---|---|---|
|| |
OR | کم از کم ایک شرط درست ہے | num % 2 == 0 || num % 4 == 0 |
&& |
AND | دونوں شرائط درست ہونی چاہئیں | num > 0 && num % 2 == 0 |
% کا استعمال یہ چیک کرنے کے لیے کیا جاتا ہے کہ آیا عدد 2 سے تقسیم ہوتا ہے۔num % 2 == 0، تو عدد جفت ہے؛ ورنہ طاق ہے۔|| کو یہ ظاہر کرنے کے لیے استعمال کیا گیا کہ متعدد شرائط کو کیسے یکجا کیا جا سکتا ہے۔
آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو ماڈیولس آپریٹر (%) کا استعمال کرتے ہوئے چیک کرتا ہے کہ آیا کوئی عدد جفت ہے یا طاق۔ آپ نے منطقی OR آپریٹر (||) کا استعمال کرتے ہوئے شرائط کو یکجا کرنا بھی سیکھ لیا ہے۔
% بقیہ دیتا ہے۔ num % 2 جفت اعداد کے لیے 0 اور طاق اعداد کے لیے 1 دیتا ہے۔
(num % 2 == 0 || num % 3 == 0) چیک کرتا ہے کہ آیا عدد 2 سے یا 3 سے تقسیم ہوتا ہے۔