Sure, here is a Rust function to check if a given number is an Armstrong number:
Rust
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
rustc armstrong_number.rs
Once the program is compiled, you can run it using the following command:
Code snippet
./armstrong_number
This will print the following output to the console:
Code snippet
Is 153 an Armstrong number? true
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, here is a Rust function to check if a given number is an Armstrong number:
Rust
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
Once the program is compiled, you can run it using the following command:
Code snippet
This will print the following output to the console:
Code snippet