EZ Notes Logo

EZNotes

🎯 Aim

To write a C/C++ program that takes a character as input and checks whether it is a vowel or consonant using the switch statement.

🛠️ What You Need

  • Visual Studio Code or any C/C++ compiler
  • Knowledge of switch statement and character input

📖 Introduction

The switch statement is a multi-way decision-making statement in C/C++. It is used to execute one block of code from multiple alternatives. It is often more readable than multiple if-else if statements when comparing a single variable against many constant values.

In this practical, you will use the switch statement to check if a character is a vowel (A, E, I, O, U) or a consonant.

The vowels in English are:

  • Uppercase: A, E, I, O, U
  • Lowercase: a, e, i, o, u
💡 Tip: The switch statement is perfect for checking a single variable against multiple constant values. It is cleaner than multiple if-else if statements.

📝 Steps

Step 1: Write the Program

  1. Open VS Code or any C compiler.
  2. Create a new file named vowel_switch.c.
  3. Write the following code:
/* Practical 31: Program to check vowel using switch statement */ #include <stdio.h> int main() { // Declare a variable to store the character char ch; // Take input from user printf("Enter a character: "); scanf("%c", &ch); // Display the character entered printf("\n========== CHARACTER CHECK ==========\n"); printf("You entered: '%c'\n", ch); /* ============================================ Using switch statement to check vowel or consonant ============================================ */ switch(ch) { // Check for uppercase vowels case 'A': case 'E': case 'I': case 'O': case 'U': // Check for lowercase vowels case 'a': case 'e': case 'i': case 'o': case 'u': printf("✅ '%c' is a VOWEL.\n", ch); break; // Default case for consonant or non-alphabetic default: printf("❌ '%c' is a CONSONANT or NOT a letter.\n", ch); break; } printf("========================================\n"); return 0; }

Step 2: Explanation of the Code

  1. #include <stdio.h> — Includes the standard input/output library.
  2. char ch; — Declares a character variable.
  3. scanf("%c", &ch); — Reads a character from the user.
  4. switch(ch) — Switches based on the character value.
  5. case 'A': case 'E': ... — Checks for each vowel.
  6. break; — Exits the switch after finding a match.
  7. default: — Executes if no vowel matches.

Step 3: Save, Compile, and Run

  1. Save the file (Ctrl + S).
  2. Open the terminal and compile using the command:
gcc vowel_switch.c -o vowel_switch

Run the program:

./vowel_switch

Sample Input & Output:

Enter a character: a ========== CHARACTER CHECK ========== You entered: 'a' ✅ 'a' is a VOWEL. ========================================
Enter a character: b ========== CHARACTER CHECK ========== You entered: 'b' ❌ 'b' is a CONSONANT or NOT a letter. ========================================
Enter a character: E ========== CHARACTER CHECK ========== You entered: 'E' ✅ 'E' is a VOWEL. ========================================

Step 4: Test with Different Inputs

  1. Test 1: 'a' → Vowel
  2. Test 2: 'E' → Vowel
  3. Test 3: 'i' → Vowel
  4. Test 4: 'b' → Consonant
  5. Test 5: 'Z' → Consonant
  6. Test 6: '5' → Not a letter
  7. Test 7: '#' → Not a letter

📊 Switch Statement Syntax

Syntax Description
switch(expression) Starts the switch statement
case value: Checks if expression equals value
break; Exits the switch block
default: Executes if no case matches

📊 Comparison: if-else vs switch

Feature if-else switch
Best for Complex conditions Single variable with many constant values
Readability Less readable for many conditions More readable for many conditions
Performance Slower for many conditions Faster (uses jump table)
Default case else default
Example if (ch == 'a') case 'a':

📊 Observations

  • The program successfully takes a character as input from the user.
  • The switch statement checks if the character is a vowel.
  • Multiple case statements are stacked together for each vowel.
  • The default case handles consonants and non-alphabetic characters.
  • The switch statement is cleaner and more readable for this type of check.

✅ Result

You have successfully written a C program that uses the switch statement to check whether a character is a vowel or consonant. You have learned how to use the switch statement for multi-way decision-making.

📌 Important: The switch statement can only be used with integer or character expressions. The break statement is important to prevent falling through to the next case.
💡 Tip: The default case is optional but recommended to handle unexpected values.

🎯 مقصد

ایک C/C++ پروگرام لکھنا جو صارف سے ایک حرف ان پٹ کے طور پر لے اور switch بیان کا استعمال کرتے ہوئے چیک کرے کہ آیا یہ حرفِ علت ہے یا حرفِ صحیح۔

🛠️ آپ کو کیا چاہیے

  • Visual Studio Code یا کوئی بھی C/C++ کمپائلر
  • switch بیان اور حروف کی معلومات

📖 تعارف

switch بیان C/C++ میں ایک کثیر شاخی فیصلہ سازی کا بیان ہے۔ یہ متعدد اختیارات میں سے کوڈ کے ایک بلاک کو انجام دینے کے لیے استعمال ہوتا ہے۔ یہ اکثر ایک ہی متغیر کا متعدد مستقل اقدار سے موازنہ کرتے وقت متعدد if-else if بیانات سے زیادہ پڑھنے کے قابل ہوتا ہے۔

اس عملی میں، آپ switch بیان کا استعمال کرتے ہوئے چیک کریں گے کہ آیا کوئی حرف حرفِ علت (A, E, I, O, U) ہے یا حرفِ صحیح۔

انگریزی میں حروفِ علت یہ ہیں:

  • بڑے حروف: A, E, I, O, U
  • چھوٹے حروف: a, e, i, o, u
💡 مشورہ: switch بیان ایک ہی متغیر کا متعدد مستقل اقدار سے موازنہ کرنے کے لیے بہترین ہے۔ یہ متعدد if-else if بیانات سے زیادہ صاف ہے۔

📝 اقدامات

پہلا قدم: پروگرام لکھیں

  1. VS Code یا کوئی بھی C کمپائلر کھولیں۔
  2. vowel_switch.c نام کی ایک نئی فائل بنائیں۔
  3. نیچے دیا گیا کوڈ لکھیں:
/* Practical 31: Program to check vowel using switch statement */ #include <stdio.h> int main() { // Declare a variable to store the character char ch; // Take input from user printf("Enter a character: "); scanf("%c", &ch); // Display the character entered printf("\n========== CHARACTER CHECK ==========\n"); printf("You entered: '%c'\n", ch); /* ============================================ Using switch statement to check vowel or consonant ============================================ */ switch(ch) { // Check for uppercase vowels case 'A': case 'E': case 'I': case 'O': case 'U': // Check for lowercase vowels case 'a': case 'e': case 'i': case 'o': case 'u': printf("✅ '%c' is a VOWEL.\n", ch); break; // Default case for consonant or non-alphabetic default: printf("❌ '%c' is a CONSONANT or NOT a letter.\n", ch); break; } printf("========================================\n"); return 0; }

دوسرا قدم: کوڈ کی وضاحت

  1. #include <stdio.h> — معیاری ان پٹ/آؤٹ پٹ لائبریری شامل کرتا ہے۔
  2. char ch; — ایک کریکٹر متغیر کا اعلان کرتا ہے۔
  3. scanf("%c", &ch); — صارف سے ایک حرف پڑھتا ہے۔
  4. switch(ch) — حرف کی قیمت کی بنیاد پر سوئچ کرتا ہے۔
  5. case 'A': case 'E': ... — ہر حرفِ علت کے لیے چیک کرتا ہے۔
  6. break; — میچ ملنے کے بعد سوئچ سے باہر نکلتا ہے۔
  7. default: — اگر کوئی حرفِ علت میچ نہ ہو تو انجام دیتا ہے۔

تیسرا قدم: محفوظ کریں، کمپائل کریں اور چلائیں

  1. فائل کو محفوظ کریں (Ctrl + S)۔
  2. ٹرمینل کھولیں اور نیچے دی گئی کمانڈ استعمال کرتے ہوئے کمپائل کریں:
gcc vowel_switch.c -o vowel_switch

پروگرام چلائیں:

./vowel_switch

نمونہ ان پٹ اور آؤٹ پٹ:

Enter a character: a ========== CHARACTER CHECK ========== You entered: 'a' ✅ 'a' is a VOWEL. ========================================

📊 switch بیان کا نحو

نحو وضاحت
switch(expression) switch بیان شروع کرتا ہے
case value: چیک کرتا ہے کہ آیا expression value کے برابر ہے
break; switch بلاک سے باہر نکلتا ہے
default: اگر کوئی case میچ نہ ہو تو انجام دیتا ہے

📊 مشاہدات

  • پروگرام کامیابی سے صارف سے ایک حرف ان پٹ کے طور پر لیتا ہے۔
  • switch بیان چیک کرتا ہے کہ آیا حرف حرفِ علت ہے۔
  • متعدد case بیانات ہر حرفِ علت کے لیے ایک ساتھ لگائے گئے ہیں۔
  • default کیس حروفِ صحیح اور غیر حروفِ تہجی کو ہینڈل کرتا ہے۔
  • switch بیان اس قسم کی جانچ کے لیے زیادہ صاف اور پڑھنے کے قابل ہے۔

✅ نتیجہ

آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو switch بیان کا استعمال کرتے ہوئے چیک کرتا ہے کہ آیا کوئی حرف حرفِ علت ہے یا حرفِ صحیح۔ آپ نے کثیر شاخی فیصلہ سازی کے لیے switch بیان کا استعمال کرنا سیکھ لیا ہے۔

📌 اہم: switch بیان صرف انٹیجر یا کریکٹر ایکسپریشن کے ساتھ استعمال کیا جا سکتا ہے۔ break بیان اگلے کیس میں جانے سے روکنے کے لیے ضروری ہے۔
💡 مشورہ: default کیس اختیاری ہے لیکن غیر متوقع اقدار کو ہینڈل کرنے کے لیے تجویز کیا جاتا ہے۔
⬅ Back to Practical Notebooks