GO Program to check the character is an digit

How to check the character is a digit or not

Here we are explaining how to check the character is a digit or not in the Go program. We can use the built-in function fmt.println() to print anything and fmt.scanln() for reading the values. This function is defined under the fmt package and it helps to write standard output. In order to use these functions, we need to import the “fmt” package.

func IsDigit(r rune) bool 

IsDigit reports whether the given rune is a decimal digit

 

How to check the character is a digit in GO Program

We are using fmt.println() function for printing the string to the output screen. Here we are showing how to check the character is a digit in the Go language. Here variable chr is used to hold the read character. For reading the character create the reader using NewReader. Check the character read is a digit or not by using unicode.IsDigit(chr). Finally print the results. Given below are the steps which are used in the Go program. 

ALGORITHM

STEP 1: Import the package fmt, bufio, os, unicode

STEP 2: Start function main()

STEP 3: Create a reader using NewReader as reader := bufio.NewReader(os.Stdin)

STEP 4: Read the character into variable chr as chr, _, _ := reader.ReadRune()

STEP 5: Check the character is an alphabet or not by using unicode.IsDigit(chr)

STEP 6: Use if statement for printing proper message

GO Source Code

                                          package main

import (
    "bufio"
    "fmt"
    "os"
    "unicode"
)

func main() {

    reader := bufio.NewReader(os.Stdin)

    fmt.Print("Enter any character to be checked = ")
    chr, _, _ := reader.ReadRune()

    if unicode.IsDigit(chr) {
        fmt.Printf("%c is a Digit\n", chr)
    } else {
        fmt.Printf("%c is not a Digit\n", chr)
    }
}
                                      

OUTPUT

Enter any character to be checked = 6
6 is a Digit

Enter any character to be checked = c
c is not a Digit