C program to check a given character is a digit or not without using the library function

Here, we are going to learn how to check a given character is a digit or not without using the library function in C programming language?
Submitted by Nidhi, on July 16, 2021

Problem statement

Given a character, we have to check whether the given character is a digit or not without using the library function.

Program

The source code to check a given character is a digit or not without using the library function is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to check a given character is a digit or not
// without using library function

#include <stdio.h>

int isDigit(char ch)
{
    if (ch >= '0' && ch <= '9')
        return 1;

    return 0;
}

int main()
{
    char ch = 0;

    printf("Enter character: ");
    scanf("%c", &ch);

    if (isDigit(ch))
        printf("Given character is a digit\n");
    else
        printf("Given character is not a digit\n");

    return 0;
}

Output

RUN 1:
Enter character: 8
Given character is a digit

RUN 2:
Enter character: 0
Given character is a digit

RUN 3:
Enter character: H
Given character is not a digit

Explanation

In the above program, we created two functions isDigit() and main(). The isDigit() function is used to check the given character is a digit or not.

In the main() function, we read a character from the user and check given character is a digit or not by calling the isDigit() function and print the appropriate message on the console screen.

C Basic Programs »


Related Programs

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.