---
title: "Write a function to check if a given number is an Armstrong number using Rust."  
description: "Write a function to check if a given number is an Armstrong number using Rust."  
author: "Utpal Vishwas"  
published: 2023-06-22  
updated: 2023-06-23  
canonical: https://www.mindstick.com/forum/158839/write-a-function-to-check-if-a-given-number-is-an-armstrong-number-using-rust  
category: "rust"  
tags: ["programs", "rust"]  
reading_time: 2 minutes  

---

# Write a function to check if a given number is an Armstrong number using Rust.

Write a [function to check](https://www.mindstick.com/forum/161900/write-a-function-to-check-if-a-number-is-prime-with-explanation) if a given number is an Armstrong number using Rust.

## Replies

### Reply by Aryan Kumar

Sure, here is a Rust [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) to [check if](https://www.mindstick.com/forum/12878/how-to-check-if-an-asp-dot-net-file-upload-control-has-a-file-in-jquery) a given number is an Armstrong number:

Rust

```plaintext
fn is_armstrong_number(number: u32) -> bool {
    let mut sum_of_powers = 0;
    let number_digits = number.to_string().len();

    let mut temp = number;
    while temp > 0 {
        let digit = temp % 10;
        sum_of_powers += digit.pow(number_digits);
        temp /= 10;
    }

    return number == sum_of_powers;
}

fn main() {
    let number = 153;
    println!("Is {} an Armstrong number? {}", number, is_armstrong_number(number));
}
```

This function first defines a function called is_armstrong_number that takes a u32 number as input and returns a bool value. The function first calculates the number of digits in the number. Then, it iterates through the number, calculating the sum of the powers of each digit. The sum of the powers is then compared to the original number. If the sum of the powers is equal to the original number, then the number is an Armstrong number.

The main function of the program then calls the is_armstrong_number function with the number 153 as input. The result of the function call is then printed to the console.

To run the program, you can save it as a file called armstrong_number.rs and then compile it using the following command:

Code snippet

```plaintext
rustc armstrong_number.rs
```

Once the program is compiled, you can run it using the following command:

Code snippet

```plaintext
./armstrong_number
```

This will print the following output to the console:

Code snippet

```plaintext
Is 153 an Armstrong number? true
```


---

Original Source: https://www.mindstick.com/forum/158839/write-a-function-to-check-if-a-given-number-is-an-armstrong-number-using-rust

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
