C Program to Check Whether a Character is Alphabet or Digit

  • Write a C program to check whether number is alphabet or digit.
  • Wap in C to check whether a character is alphabet or digit using isalpha and isdigit function.

Required Knowledge


C program to check whether a character is alphabet or digit

/*
 * C program to check whether a character is Alphabet or Digit 
 * or Other Graphical Character
 */  
  
#include <stdio.h>  
  
int main() {  
    char character;
    /* 
     * Take a character as input from user 
     */
    printf("Enter a Character\n");  
    scanf("%c", &character);  
      
    if((character >='a' && character <='z')||(character >='A' && character <='Z')){  
        printf("%c is an Alphabet\n", character);  
    } else if(character >= '0' && character <= '9') {
        printf("%c is a Digit \n", character);  
    } else {
        printf("%c is a Graphical Character\n", character);  
    }
  
    return 0;  
}

Output
Enter a Character
J
J is an Alphabet
Enter a Character
8
8 is a Digit
Enter a Character
#
# is a Graphical Character

C program to check whether a character is alphabet or digit using isalpha and isdigit function

We will use isdigit function to check whether character is a digit or not. If passed character is a decimal digit character, then isdigit function returns non-zero integer otherwise 0.
We will use isalpha function to check whether character is a alphabet or not. If passed character is an alphabet(a-z, A-Z), then isalpha function returns non-zero integer otherwise 0.

/*
 * C program to check whether a character is Alphabet or Digit 
 * or Other Graphical Character using isalpha and isdigit function
 */  
  
#include <stdio.h>
#include <ctype.h>
  
int main() {  
    char character;
    /* 
     * Take a character as input from user 
     */
    printf("Enter a Character\n");  
    scanf("%c", &character);  
      
    if(isalpha(character)) {  
        printf("%c is an Alphabet\n", character);  
    } else if(isdigit(character)) {
        printf("%c is a Digit \n", character);  
    } else {
        printf("%c is a Graphical Character\n", character);  
    }
  
    return 0;  
}

Output
Enter a Character
T
T is an Alphabet
Enter a Character
1
1 is a Digit
Enter a Character
%
% is a Graphical Character

Related Topics
C program to check whether a character is alphabet or not
C Program to Check Whether an Alphabet is Vowel or Consonant using Switch Case Statement
C Program to Check Whether a Number is Odd or Even Number using Switch Case Statement
C Program to Check a Number is Odd or Even Using Conditional Operator
C Program to Check Whether an Alphabet is Vowel or Consonant
C Program to Check a Number is Palindrome or Not
C Program to Check Whether a Number is Prime or Not
C Program to Check a String is Palindrome
C Program to Check If Two Strings are Anagram
List of all C programs