How to get the last digit of a big int in Rust

1 Answer

0 votes
use num_bigint::BigInt;
use num_traits::{Zero, ToPrimitive};

fn main() {
    // Example BigInt value
    let big_number = BigInt::parse_bytes(b"12345678901234567890123456789123", 10).unwrap();

    // Get the last digit
    let last_digit = get_last_digit(&big_number);

    // Print the result
    println!("The last digit of {} is {}", big_number, last_digit);
}

// Function to get the last digit of a BigInt
fn get_last_digit(num: &BigInt) -> u8 {
    if num.is_zero() {
        return 0; // Special case: last digit of 0 is 0
    }

    // Calculate the remainder when dividing by 10
    (num % 10u8).to_u8().unwrap()
}




/*
run:

The last digit of 12345678901234567890123456789123 is 3

*/
  
 

 



answered Aug 27, 2025 by avibootz

Related questions

1 answer 146 views
1 answer 81 views
1 answer 86 views
1 answer 71 views
1 answer 89 views
1 answer 82 views
1 answer 70 views
...