Programs to check if a character is a digit or not have been shown here. The numeric digits are 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9.

## 1. Algorithm to Check If a Character Is a Digit or Not

1. Take the character x as input.

2. Check if the ASCII value of x is between 48 and 57.

3. If step 2 is true declare the character x as digit x.

4. If step 2 is false declare the character x is not a digit.

## 2. Pseudocode to Check If a Character Is a Digit or Not

Input : A character $x$

Output : $x$ is a digit or not

1. Procedure checkDigit($x$):

2. If ASCII($x$) $\in [48, 57]$:

3. Return "Digit"

4. Else:

5. Return "Not a digit"

6. End Procedure

## 3. Time complexity to Check If a Character Is a Digit or Not

Time Complexity: O(1)

## 5. Program to Check If a Character Is a Digit or Not

This section covers the C, C++, Java, Python and C# programs to check if a character is a digit or not. The programs declare the character as a digit if the ASCII value of the character lies between 48 and 57.

## 5.1. C Program to Check If a Character Is a Digit or Not

Code has been copied
/***************************
alphabetacoder.com
C program to check if a
character is a digit or not
****************************/

#include <stdio.h>

int main() {
// declare variables
char x;

// take input
printf("Enter a character: ");
scanf("%c", & x);

// check if the character
// is a digit or not
// Range of ASCII values of digits
//  0 to 9 are from 48 to 57
if (x >= 48 && x <= 57)
printf("It is the digit %c", x);
else
printf("%c is not a digit", x);

return 0;
}


Output

#### Case 1:

Enter a character: 8

It is the digit 8

#### Case 2:

Enter a character: 0

It is the digit 0

#### Case 3:

Enter a character: a

a is not a digit

## 5.2. C++ Program to Check If a Character Is a Digit or Not

Code has been copied
/***************************
alphabetacoder.com
C++ program to check if a
character is a digit or not
****************************/

#include <iostream>

using namespace std;

int main() {
// declare variables
char x;

// take input
cout << "Enter a character: ";
cin >> x;

// check if the character
// is a digit or not
// Range of ASCII values of digits
//  0 to 9 are from 48 to 57
if (x >= 48 && x <= 57)
cout << "It is the digit " << x;
else
cout << x << " is not a digit";

return 0;
}


Output

#### Case 1:

Enter a character: 1

It is the digit 1

#### Case 2:

Enter a character: 5

It is the digit 5

#### Case 3:

Enter a character: x

x is not a digit

## 5.3. Java Program to Check If a Character Is a Digit or Not

Code has been copied
/***************************
alphabetacoder.com
Java program to check if a
character is a digit or not
****************************/

import java.util.Scanner;
public class Main {
public static void main(String args[]) {
// declare variables
char x;

// declare an instance of scanner class
Scanner sc = new Scanner(System.in);

// take input
System.out.print("Enter a character: ");
x = sc.next().charAt(0);

// check if the character
// is a digit or not
// Range of ASCII values of digits
//  0 to 9 are from 48 to 57
if (x >= 48 && x <= 57)
System.out.print("It is the digit " + x);
else
System.out.print(x + " is not a digit");
}
}


Output

#### Case 1:

Enter a character: 3

It is the digit 3

#### Case 2:

Enter a character: 4

It is the digit 4

#### Case 3:

Enter a character: p

p is not a digit

## 5.4. Python Program to Check If a Character Is a Digit or Not

Code has been copied
# *****************************
#     alphabetacoder.com
# Python program to check if a
# character is a digit or not
# *****************************

# take input
x = input("Enter a character: ")

# check if the character
# is a digit or not
# Range of ASCII values of digits
#  0 to 9 are from 48 to 57
if ord(x) >= 48 and ord(x) <= 57:
print("It is the digit ", x)
else:
print(x, " is not a digit")


Output

#### Case 1:

Enter a character: 2

It is the digit 2

#### Case 2:

Enter a character: 6

It is the digit 6

#### Case 3:

Enter a character: *

* is not a digit

## 5.5. C# Program to Check If a Character Is a Digit or Not

Code has been copied
/***************************
alphabetacoder.com
C# program to check if a
character is a digit or not
****************************/

using System;

namespace CheckDigit {
class Program {
static void Main(string[] args) {
// declare variables
char x;

// take input
Console.Write("Enter a character: ");

// check if the character
// is a digit or not
// Range of ASCII values of digits
//  0 to 9 are from 48 to 57
if (x >= 48 && x <= 57)
Console.WriteLine("It is the digit " + x);
else
Console.WriteLine(x + " is not a digit");

// wait for user to press any key
}
}
}


Output

#### Case 1:

Enter a character: 7

It is the digit 7

#### Case 2:

Enter a character: 9

It is the digit 9

#### Case 3:

Enter a character: !

! is not a digit