Learn how to use the if-else statement 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 even or odd using the if-else structure.
The if-else statement is one of the most fundamental decision-making structures in C/C++. It allows you to execute different blocks of code based on whether a condition is true or false.
In this practical, you will use the if-else structure to check if a number is even or odd:
You will use the modulus operator (%) to find the remainder when the number is divided by 2.
if-else structure is perfect for this type of binary decision (even or odd). The condition num % 2 == 0 checks if the number is even.
Run the program:
Sample Input & Output:
| Syntax | Description |
|---|---|
if (condition) |
Executes the block if condition is true |
{ } |
Block of code to execute |
else |
Executes the block if condition is false |
num % 2 == 0 |
Condition for even number |
| Type | Numbers | Rule |
|---|---|---|
| Even | 2, 4, 6, 8, 10, 12, 0, -2, -4, -6 | Divisible by 2 |
| Odd | 1, 3, 5, 7, 9, 11, -1, -3, -5, -7 | Not divisible by 2 |
| Feature | if-else |
?: (Ternary) |
|---|---|---|
| Readability | More readable for beginners | More compact |
| Multiple Statements | Supports multiple statements | Only single expressions |
| Best Used For | Complex decision making | Simple assignments |
| Learning Curve | Easier to understand | Requires more practice |
if-else structure is easy to read and understand.% is used to find the remainder when the number is divided by 2.
You have successfully written a C program that uses the if-else structure to check whether a number is even or odd. You have learned how to use the modulus operator and conditional statements to make decisions in your program.
if-else structure is fundamental to C programming. It allows your program to make decisions based on conditions. The condition num % 2 == 0 checks if the number is even.
ایک C/C++ پروگرام لکھنا جو صارف سے ایک عدد ان پٹ کے طور پر لے اور if-else ڈھانچے کا استعمال کرتے ہوئے چیک کرے کہ آیا یہ جفت ہے یا طاق۔
if-else بیان C/C++ میں سب سے بنیادی فیصلہ سازی کے ڈھانچوں میں سے ایک ہے۔ یہ آپ کو شرط کے درست یا غلط ہونے کی بنیاد پر کوڈ کے مختلف بلاکس کو انجام دینے کی اجازت دیتا ہے۔
اس عملی میں، آپ if-else ڈھانچے کا استعمال کرتے ہوئے چیک کریں گے کہ آیا کوئی عدد جفت ہے یا طاق:
آپ عدد کو 2 سے تقسیم کرنے پر بقیہ معلوم کرنے کے لیے ماڈیولس آپریٹر (%) استعمال کریں گے۔
if-else ڈھانچہ اس قسم کے دو طرفہ فیصلے (جفت یا طاق) کے لیے بہترین ہے۔ شرط num % 2 == 0 چیک کرتی ہے کہ آیا عدد جفت ہے۔
پروگرام چلائیں:
نمونہ ان پٹ اور آؤٹ پٹ:
| نحو | وضاحت |
|---|---|
if (condition) |
اگر شرط درست ہے تو بلاک کو انجام دیتا ہے |
{ } |
کوڈ کا بلاک |
else |
اگر شرط غلط ہے تو بلاک کو انجام دیتا ہے |
num % 2 == 0 |
جفت عدد کی شرط |
| قسم | اعداد | قاعدہ |
|---|---|---|
| جفت (Even) | 2, 4, 6, 8, 10, 12, 0, -2, -4, -6 | 2 سے تقسیم ہوتا ہے |
| طاق (Odd) | 1, 3, 5, 7, 9, 11, -1, -3, -5, -7 | 2 سے تقسیم نہیں ہوتا |
if-else ڈھانچہ پڑھنے اور سمجھنے میں آسان ہے۔% عدد کو 2 سے تقسیم کرنے پر بقیہ معلوم کرنے کے لیے استعمال ہوتا ہے۔
آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو if-else ڈھانچے کا استعمال کرتے ہوئے چیک کرتا ہے کہ آیا کوئی عدد جفت ہے یا طاق۔ آپ نے ماڈیولس آپریٹر اور مشروط بیانات کا استعمال کرتے ہوئے اپنے پروگرام میں فیصلے کرنا سیکھ لیا ہے۔
if-else ڈھانچہ C پروگرامنگ میں بنیادی حیثیت رکھتا ہے۔ یہ آپ کے پروگرام کو شرائط کی بنیاد پر فیصلے کرنے کی اجازت دیتا ہے۔ شرط num % 2 == 0 چیک کرتی ہے کہ آیا عدد جفت ہے۔