EZ Notes Logo

EZNotes

🎯 Aim

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

🛠️ What You Need

  • Visual Studio Code or any C/C++ compiler
  • Knowledge of characters, logical operators, and conditional statements

📖 Introduction

In English, there are 5 vowels and 21 consonants:

  • Vowels: A, E, I, O, U (and their lowercase versions: a, e, i, o, u)
  • Consonants: All other letters (B, C, D, F, G, etc.)

In this practical, you will write a program that:

  • Takes a character as input from the user
  • Checks if it is a vowel (a, e, i, o, u, A, E, I, O, U)
  • Displays whether it is a vowel or a consonant
  • Handles invalid input (non-alphabetic characters)
💡 Tip: Use || (OR) operator to check multiple conditions. For example, ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'.

📝 Steps

Step 1: Write the Program

  1. Open VS Code or any C compiler.
  2. Create a new file named vowel_consonant.c.
  3. Write the following code:
/* Practical 20: Program to check vowel or consonant */ #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); // Check if the character is a vowel (both uppercase and lowercase) if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') { printf("✅ '%c' is a VOWEL.\n", ch); } // Check if the character is an alphabet (consonant) else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { printf("✅ '%c' is a CONSONANT.\n", ch); } // If not a letter else { printf("⚠️ '%c' is NOT a letter. Please enter an alphabet.\n", ch); } 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. if (ch == 'a' || ch == 'e' || ...) — Checks if the character is a vowel.
  5. else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) — Checks if it is a letter (consonant).
  6. else — Handles non-alphabetic characters.

Step 3: Save, Compile, and Run

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

Run the program:

./vowel_consonant

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. ======================================
Enter a character: 5 ========== CHARACTER CHECK ========== You entered: '5' ⚠️ '5' is NOT a letter. Please enter an alphabet. ======================================

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: '#' → Not a letter
  7. Test 7: '9' → Not a letter

📊 Vowels and Consonants

Type Letters Count
Vowels A, E, I, O, U (and a, e, i, o, u) 5
Consonants B, C, D, F, G, H, J, K, L, M, N, P, Q, R, S, T, V, W, X, Y, Z 21
Total Letters A - Z 26

📊 Alternative Method (Using switch statement)

/* Alternative: Using switch statement */ switch(ch) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': printf("Vowel"); break; default: printf("Consonant or Not a letter"); }

📊 Observations

  • The program successfully takes a character as input from the user.
  • It correctly identifies vowels (both uppercase and lowercase).
  • It identifies consonants for all other alphabetic characters.
  • It handles non-alphabetic characters appropriately.
  • The || operator is used to check multiple vowel conditions.

✅ Result

You have successfully written a C program that checks whether a character is a vowel or a consonant. You have learned how to use logical operators (||) and conditional statements to make decisions based on character input.

📌 Important: The || operator checks if at least one condition is true. If any of the vowel conditions is true, the character is a vowel.
💡 Tip: You can use the tolower() or toupper() functions from the ctype.h library to convert the character to lowercase or uppercase before checking, making the code shorter.

🎯 مقصد

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

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

  • Visual Studio Code یا کوئی بھی C/C++ کمپائلر
  • حروف، منطقی آپریٹرز اور مشروط بیانات کی معلومات

📖 تعارف

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

  • حروفِ علت (Vowels): A, E, I, O, U (اور ان کے چھوٹے حروف: a, e, i, o, u)
  • حروفِ صحیح (Consonants): باقی تمام حروف (B, C, D, F, G, وغیرہ)

اس عملی میں، آپ ایک پروگرام لکھیں گے جو:

  • صارف سے ایک حرف ان پٹ کے طور پر لیتا ہے
  • چیک کرتا ہے کہ آیا یہ حرفِ علت ہے (a, e, i, o, u, A, E, I, O, U)
  • ظاہر کرتا ہے کہ آیا یہ حرفِ علت ہے یا حرفِ صحیح
  • غلط ان پٹ (غیر حروفِ تہجی) کو ہینڈل کرتا ہے
💡 مشورہ: متعدد شرائط کو چیک کرنے کے لیے || (OR) آپریٹر استعمال کریں۔ مثال کے طور پر، ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'۔

📝 اقدامات

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

  1. VS Code یا کوئی بھی C کمپائلر کھولیں۔
  2. vowel_consonant.c نام کی ایک نئی فائل بنائیں۔
  3. نیچے دیا گیا کوڈ لکھیں:
/* Practical 20: Program to check vowel or consonant */ #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); // Check if the character is a vowel (both uppercase and lowercase) if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') { printf("✅ '%c' is a VOWEL.\n", ch); } // Check if the character is an alphabet (consonant) else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { printf("✅ '%c' is a CONSONANT.\n", ch); } // If not a letter else { printf("⚠️ '%c' is NOT a letter. Please enter an alphabet.\n", ch); } printf("======================================\n"); return 0; }

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

  1. #include <stdio.h> — معیاری ان پٹ/آؤٹ پٹ لائبریری شامل کرتا ہے۔
  2. char ch; — ایک کریکٹر متغیر کا اعلان کرتا ہے۔
  3. scanf("%c", &ch); — صارف سے ایک حرف پڑھتا ہے۔
  4. if (ch == 'a' || ch == 'e' || ...) — چیک کرتا ہے کہ آیا حرفِ علت ہے۔
  5. else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) — چیک کرتا ہے کہ آیا یہ حرفِ صحیح ہے۔
  6. else — غیر حروفِ تہجی کو ہینڈل کرتا ہے۔

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

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

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

./vowel_consonant

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

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

📊 حروفِ علت اور حروفِ صحیح

قسم حروف تعداد
حروفِ علت (Vowels) A, E, I, O, U (اور a, e, i, o, u) 5
حروفِ صحیح (Consonants) B, C, D, F, G, H, J, K, L, M, N, P, Q, R, S, T, V, W, X, Y, Z 21
کل حروف A - Z 26

📊 مشاہدات

  • پروگرام کامیابی سے صارف سے ایک حرف ان پٹ کے طور پر لیتا ہے۔
  • یہ حروفِ علت (بڑے اور چھوٹے دونوں) کی درست شناخت کرتا ہے۔
  • یہ باقی تمام حروفِ تہجی کو حروفِ صحیح کے طور پر شناخت کرتا ہے۔
  • یہ غیر حروفِ تہجی کو مناسب طریقے سے ہینڈل کرتا ہے۔
  • || آپریٹر کا استعمال متعدد حروفِ علت کی شرائط کو چیک کرنے کے لیے کیا جاتا ہے۔

✅ نتیجہ

آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو چیک کرتا ہے کہ آیا کوئی حرف حرفِ علت ہے یا حرفِ صحیح۔ آپ نے سیکھ لیا ہے کہ کس طرح منطقی آپریٹرز (||) اور مشروط بیانات کا استعمال کرتے ہوئے حروف کی بنیاد پر فیصلے کیے جاتے ہیں۔

📌 اہم: || آپریٹر چیک کرتا ہے کہ آیا کم از کم ایک شرط درست ہے۔ اگر حروفِ علت کی کوئی بھی شرط درست ہے تو حرف حرفِ علت ہے۔
💡 مشورہ: آپ ctype.h لائبریری سے tolower() یا toupper() فنکشنز کا استعمال کر سکتے ہیں تاکہ حرف کو چیک کرنے سے پہلے چھوٹے یا بڑے حروف میں تبدیل کیا جا سکے، جس سے کوڈ چھوٹا ہو جاتا ہے۔
⬅ Back to Practical Notebooks