C Program To Check For Alphabet, Number and Special Symbol

Any character is entered through the keyboard, write a C program to determine whether the character entered is a capital letter, a small case letter, a digit or a special symbol.

The following table shows the range of ASCII values for various characters:
Character A – Z : ASCII Value 65 – 90
Character a – z : ASCII Value 97 – 122
Character 0 – 9 : ASCII Value 48 – 57
Special Symbol : ASCII Value 0 – 47, 58 – 64, 91 – 96, 123 – 127

ascii codes

Related Read:
else if statement in C
Relational Operators In C
C Program To Print All ASCII Characters and Code

Expected Output for the Input

User Input:
Enter a Character
$

Output:
$ is a Special Character

Video Tutorial: C Program To Check For Alphabet, Number or Special Symbol


[youtube https://www.youtube.com/watch?v=NBcG-r0P9P8]

YouTube Link: https://www.youtube.com/watch?v=NBcG-r0P9P8 [Watch the Video In Full Screen.]

Source Code: C Program To Check For Alphabet, Number and Special Symbol

#include<stdio.h>

int main()
{
    char ch;

    printf("Enter a Character\n");
    scanf("%c", &ch);

    if(ch >= 65 && ch <= 90)
    {
        printf("%c is an Uppercase Alphabet\n", ch);
    }
    else if(ch >= 97 && ch <= 122)
    {
        printf("%c is an lowercase Alphabet\n", ch);
    }
    else if(ch >= 48 && ch <= 57)
    {
        printf("%c is a Number\n", ch);
    }
    else if( (ch >= 0  && ch <= 47) ||
             (ch >= 58 && ch <= 64) ||
             (ch >= 91 && ch <= 96) ||
             (ch >= 123 && ch <= 127))
    {
        printf("%c is a Special Character\n", ch);
    }

    return 0;
}

Output 1:
Enter a Character
A
A is an Uppercase Alphabet

Output 2:
Enter a Character
i
i is an lowercase Alphabet

Output 3:
Enter a Character
8
8 is a Number

Output 4:
Enter a Character
$
$ is a Special Character

Logic To Check For Alphabet, Number and Special Symbol

We use &&(AND) operator check check for range. i.e., for number, we check from the range 48 to 57. To check if the user entered character lies in this range we use (ch >= 48 && ch <= 57). To check for multiple ranges we use ||(OR) operator. For example, for special symbol:


(ch >= 0 && ch <= 47) ||
(ch >= 58 && ch <= 64) ||
(ch >= 91 && ch <= 96) ||
(ch >= 123 && ch <= 127)

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Draw Pyramid of Alphabets, using While Loop

Lets write a C program to draw / print / display a pyramid / triangle formed from uppercase / capital letter English Alphabets, using while loop.

Related Read:
Nested While Loop: C Program
C Program To Draw Pyramid of Stars, using While Loop

Expected Output for the Input

User Input:
Enter the number of rows for Pyramid
5

Output:

    
     A
    BCD
   EFGHI
  JKLMNOP
 QRSTUVWXY

Pyramid With 20 Rows
pyramid of alphabets

Video Tutorial: C Program To Draw Pyramid of Alphabets, using While Loop


[youtube https://www.youtube.com/watch?v=eDPYA_c73I4]

YouTube Link: https://www.youtube.com/watch?v=eDPYA_c73I4 [Watch the Video In Full Screen.]

Logic To Draw Pyramid of Alphabets, using While Loop

We ask the user to enter the maximum number of rows for the pyramid and store it inside the address of variable num. We declare and initialize a variable count to 1(indicating first row of the pyramid).

In the outer while loop we check if count is less than or equal to num. For each iteration of the while loop we increment the value of count by 1. So the count value will have the row number i.e., for each iteration of the outer while loop we select row one by one (to print the alphabets).

Inside first inner while loop we print the adequate number of space for each row. Inside the second inner while loop we actually print the alphabets needed for each row.

At the end the result will be a pyramid with the number of rows as input by the user.

ASCII Value of Capital Letter English Alphabets

ASCII value of A is 65

ASCII value of B is 66

ASCII value of C is 67

ASCII value of D is 68

ASCII value of E is 69

ASCII value of F is 70

ASCII value of G is 71

ASCII value of H is 72

ASCII value of I is 73

ASCII value of J is 74

ASCII value of K is 75

ASCII value of L is 76

ASCII value of M is 77

ASCII value of N is 78

ASCII value of O is 79

ASCII value of P is 80

ASCII value of Q is 81

ASCII value of R is 82

ASCII value of S is 83

ASCII value of T is 84

ASCII value of U is 85

ASCII value of V is 86

ASCII value of W is 87

ASCII value of X is 88

ASCII value of Y is 89

ASCII value of Z is 90

Reference: C Program To Print All ASCII Characters and Code

Source Code: C Program To Draw Pyramid of Alphabets, using While Loop

#include < stdio.h >

int main()
{
    int num, count = 1, i, alphabet = 65;

    printf("Enter the number of rows for Pyramid\n");
    scanf("%d", &num);

    while(count <= num)
    {
        i = 0;
        while(i <= (num-count))
        {
            printf(" ");
            i++;
        }

        i = 0;
        while(i < (2*count-1))
        {
            printf("%c", alphabet);

            if(alphabet == 90)
            {
                alphabet = 64;
            }

            alphabet++;
            i++;
        }
        printf("\n");
        count++;
    }

    return 0;
}

Output
Enter the number of rows for Pyramid
14

              A
             BCD
            EFGHI
           JKLMNOP
          QRSTUVWXY
         ZABCDEFGHIJ
        KLMNOPQRSTUVW
       XYZABCDEFGHIJKL
      MNOPQRSTUVWXYZABC
     DEFGHIJKLMNOPQRSTUV
    WXYZABCDEFGHIJKLMNOPQ
   RSTUVWXYZABCDEFGHIJKLMN
  OPQRSTUVWXYZABCDEFGHIJKLM
 NOPQRSTUVWXYZABCDEFGHIJKLMN

Note: Inside second nested while loop we are checking for the condition – if alphabet is equal to 90(ASCII value of Z), and then reinitializing it to 64(but ASCII value of A is 65), that is because after the if condition is executed we have alphabet++; statement which increments the value of variable alphabet from 64 to 65(which is the ASCII value of A).

Alphabets and ASCII Value
We can even write the code in if else block as shown below:

        i = 0;
        while(i < (2*count-1))
        {
            printf("%c", alphabet);

            if(alphabet == 90)
            {
                alphabet = 65;
            }
            else
            {
                alphabet++;
            }
            i++;
        }

In this case we assign the value of variable alphabet to 65(ASCII value of A) once it has reached a value of 90(ASCII value of Z).

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Print Uppercase Alphabet(A-Z) using While loop

In this video tutorial we show you how to write C program to print all the upper case alphabets(A-Z) using simple while loop.

Related Read:
while loop in C programming

Note: In C programming language, every character variable holds an ASCII value rather than the character itself. You can check ASCII value of all the characters here: C Program To Print All ASCII Characters and Code

Note:
ASCII value range of upper case alphabets:
ASCII value of A is 65.
ASCII value of B is 66.
ASCII value of C is 67.
and so on till Z ..
ASCII value of Z is 90.

Source Code: C Program To Print Uppercase Alphabet(A-Z) using While loop

#include < stdio.h >

int main()
{
    char ch = 'A';

    printf("Uppercase English Alphabets:\n");
    while(ch <= 'Z')
    {
        printf("%c ", ch);
        ch++;
    }
    printf("\n\n");

    return 0;
}

Output:
Uppercase English Alphabets:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Logic To Print Uppercase Alphabet(A-Z) using While loop

Each alphabet has it’s own(unique) ASCII value. So we initialize the character variable to A, and then using while loop we iterate until our character variable is equal to Z. C program checks with the characters underlying ASCII values. Inside while loop we increment the value of character variable by one for each iteration and also print the character present in it.

Video Tutorial: C Program To Print Uppercase Alphabet(A-Z) using While loop


[youtube https://www.youtube.com/watch?v=NTNsLcMeYRE]

YouTube Link: https://www.youtube.com/watch?v=NTNsLcMeYRE [Watch the Video In Full Screen.]

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert