Q:

Rust program to check whether a character variable contains a digit or not

belongs to collection: Rust Basic Programs

0

Here, we will create two-character variables and check created variable contains a digit or not.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to check a character variable contains a digit or not is given below. The given program is compiled and executed successfully.

// Rust program to check whether a character variable
// contains a digit or not

fn main() {
    let mut ch:char = '5';
    let mut ch1:char = 'B';
    
    if (ch >= '0' && ch <= '9')
    {
       println!("Character '{}' is a digit",ch);
    }
    else
    {
        println!("Character '{}' is not a digit",ch);
    }
    
    if (ch1 >= '0' && ch1 <= '9')
    {
       println!("Character '{}' is a digit",ch1);
    }
    else
    {
        println!("Character '{}' is not a digit",ch1);
    }
}

Output:

Character '5' is a digit
Character 'B' is not a digit

Explanation:

Here, we created two characters variables ch and ch1 that are initialized with '5' and 'B' respectively. Then we checked the given characters contains digits or not and printed the appropriate message.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Rust Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Rust program to check whether a given character is... >>
<< Rust program to check whether the given character ...