Display Factors of a Number
Rust Programming Language
Problem
Rust program that displays the factors of a given number.
Input
use std::io;fn main() {println!("Please enter a positive integer:");let mut input = String::new();io::stdin().read_line(&mut input).expect("Failed to read line");let input = input.trim().parse::<u32>().unwrap();println!("Factors of {}: ", input);for i in 1..=input {if input % i == 0 {println!("{}", i);}}}{codeBox}
Output
Please enter a positive integer:28Factors of 28:12471428{codeBox}
Explanation
This program prompts the user to enter a positive integer, reads the input integer from the user using the io module from Rust's standard library, and parses it into an unsigned 32-bit integer using parse::<u32>().unwrap().
We then print "Factors of [input]: " to the console.
Next, we loop from 1 to input inclusive, and in each iteration of the loop, we check if input is divisible by the current value of i (using the modulus operator %). If it is, we print the value of i to the console.
This shows the program prompting the user to enter a positive integer, the user entering the integer 28, and the program displaying the factors of 28 to the console.