Introduction to Programming (Ref K/616/8258)

 Here is my computer program for criteria 1.1, 2.1, 3.1 and 5.1.

fn main() {
    // This program sums all of the even numbers in the below array.
    let nums = [1, 35, 22, 100, 24, 17, 109];
    let mut sum = 0;

    for num in nums {
        if num % 2 == 0 {
            sum += num;
        }
    }

    println!("The sum of the even numbers was {sum}.");
}

Here is a screenshot of it working.

Here is my computer program for criterion 4.1.

use std::io::{stdin, stdout, Write};

fn main() {
    // To demonstrate and/or/not, this program takes in two boolean values, and computes "a and b", "a or b", and "not a."
    print!("Enter true or false: ");
    stdout().flush().unwrap();
    let mut bool1 = String::new();
    stdin()
        .read_line(&mut bool1)
        .unwrap();
    // Parse.
    let bool1: bool = bool1.trim().parse().unwrap();

    // Get second bool.
    print!("Enter true or false: ");
    stdout().flush().unwrap();
    let mut bool2 = String::new();
    stdin()
        .read_line(&mut bool2)
        .unwrap();
    // Parse.
    let bool2: bool = bool2.trim().parse().unwrap();

    // Calculate.
    println!("bool1 and bool2 evaluates to {}", bool1 && bool2);
    println!("bool1 or bool2 evaluates to {}", bool1 || bool2);
    println!("not bool1 evaluates to {}", !bool1);
}

Here is a screenshot of it working.



Comments