From 0d636f55bfe3df4555165d8551ee795ec3b21c3c Mon Sep 17 00:00:00 2001 From: Soloxie Date: Thu, 16 Jan 2025 13:22:56 +0100 Subject: [PATCH 1/3] operational functions --- src/main.rs | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3cc3d3e..e340b6f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,24 +1,33 @@ + fn main() { + intro_to_u(); } - -// function to encapsulate all integers -fn intro_to_u() { - let sum_result: u8 = sum(5, 10); - println!("the sum result is: {}", sum_result); - - +fn intro_to_u(){ +let sum_result : u8 = sum(5, 5); +let sub_result : u8 = subtract(10, 5); +let multiply_result : u8 = multiply(10, 5); +let divide_result : u8 = divide(10, 5); +println!("the sum result is: {}", sum_result); +println!("the subtraction result is: {}", sub_result); +println!("the multiplication result is: {}", multiply_result); +println!("the dvision result is: {}", divide_result); } -fn sum(x: u8, y: u8) -> u8 { - x + y // implicit return -// return x + y; // explicit return +fn sum(x: u8, y:u8) -> u8 { +x + y +// return x+y;//explecit return } +fn subtract(x: u8, y:u8) -> u8 { +x-y +} -// subtract -// multiplication -// division - +fn multiply(x: u8, y:u8) -> u8 { +x*y +} +fn divide(x: u8, y:u8) -> u8 { +x/y +} \ No newline at end of file From f75e4654619ba2cfce65afbf65b5363069e974c4 Mon Sep 17 00:00:00 2001 From: Soloxie Date: Fri, 17 Jan 2025 11:32:36 +0100 Subject: [PATCH 2/3] Assignment --- src/main.rs | 67 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/src/main.rs b/src/main.rs index e340b6f..ae0b356 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,33 +1,60 @@ - fn main() { - intro_to_u(); } -fn intro_to_u(){ -let sum_result : u8 = sum(5, 5); -let sub_result : u8 = subtract(10, 5); -let multiply_result : u8 = multiply(10, 5); -let divide_result : u8 = divide(10, 5); -println!("the sum result is: {}", sum_result); -println!("the subtraction result is: {}", sub_result); -println!("the multiplication result is: {}", multiply_result); -println!("the dvision result is: {}", divide_result); +fn intro_to_u() { + let sum_result: u8 = sum(5, 4); + let sub_result: u8 = subtract(10, 5); + let multiply_result: u8 = multiply(10, 5); + let divide_result: u8 = divide(10, 5); + let is_even_result: bool = is_even_sum(5, 5); + let is_even : bool = is_even(8); + let full_name = fullname("Uche", "Caleb", "Solomon"); + + println!("The sum result is: {}", sum_result); + println!("The subtraction result is: {}", sub_result); + println!("The multiplication result is: {}", multiply_result); + println!("The division result is: {}", divide_result); + println!( + "The sum of 5 and 5 is even: {}", + if is_even_result { "true" } else { "false" } + ); + println!("The number is: {}", is_even); + println!("{}", full_name); +} + +fn sum(x: u8, y: u8) -> u8 { + x + y + // return x+y;//explicit return +} + +fn subtract(x: u8, y: u8) -> u8 { + x - y +} + +fn multiply(x: u8, y: u8) -> u8 { + x * y } -fn sum(x: u8, y:u8) -> u8 { -x + y -// return x+y;//explecit return +fn divide(x: u8, y: u8) -> u8 { + x / y } -fn subtract(x: u8, y:u8) -> u8 { -x-y +/// Function to check if the sum of two numbers is even +fn is_even_sum(x: u8, y: u8) -> bool { + let sum = x + y; + sum % 2 == 0 } -fn multiply(x: u8, y:u8) -> u8 { -x*y +fn is_even(x: u8,) -> bool{ + x % 2 == 0 } -fn divide(x: u8, y:u8) -> u8 { -x/y +fn fullname(first: &str, second: &str, last: &str) -> String { + format!( + "{first} {second} {last}.", + first = first, + second = second, + last = last, + ) } \ No newline at end of file From 045476a457eae83cc0fbce6602d6369f9de9fad8 Mon Sep 17 00:00:00 2001 From: Soloxie Date: Fri, 17 Jan 2025 13:25:34 +0100 Subject: [PATCH 3/3] Floating Assignment --- src/main.rs | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index ae0b356..c6a0c4d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,20 +7,28 @@ fn intro_to_u() { let sub_result: u8 = subtract(10, 5); let multiply_result: u8 = multiply(10, 5); let divide_result: u8 = divide(10, 5); + let floatsum_result: f64 = floatsum(5.5, 4.4); + let floatsub_result: f64 = floatsubtract(2.4, 3.5); + let floatmultiply_result: f64 = floatmultiply(1.0, 5.0); + let floatdivide_result: f64 = floatdivide(10.4, 5.3); let is_even_result: bool = is_even_sum(5, 5); - let is_even : bool = is_even(8); + let is_even: bool = is_even(8); let full_name = fullname("Uche", "Caleb", "Solomon"); println!("The sum result is: {}", sum_result); println!("The subtraction result is: {}", sub_result); println!("The multiplication result is: {}", multiply_result); println!("The division result is: {}", divide_result); + println!("The sum result is: {}", floatsum_result); + println!("The subtraction result is: {}", floatsub_result); + println!("The multiplication result is: {}", floatmultiply_result); + println!("The division result is: {}", floatdivide_result); println!( "The sum of 5 and 5 is even: {}", if is_even_result { "true" } else { "false" } ); println!("The number is: {}", is_even); - println!("{}", full_name); + println!("{}", full_name); } fn sum(x: u8, y: u8) -> u8 { @@ -40,13 +48,35 @@ fn divide(x: u8, y: u8) -> u8 { x / y } +// floating point +fn floatsum(x: f64, y: f64) -> f64 { + x + y +} + +fn floatsubtract(x: f64, y: f64) -> f64 { + x - y +} + +fn floatmultiply(x: f64, y: f64) -> f64 { + x * y +} + +fn floatdivide(x: f64, y: f64) -> f64 { + if y != 0.0 { + x / y + } else { + panic!("Division by zero is not allowed"); + } +} + + /// Function to check if the sum of two numbers is even fn is_even_sum(x: u8, y: u8) -> bool { let sum = x + y; sum % 2 == 0 } -fn is_even(x: u8,) -> bool{ +fn is_even(x: u8) -> bool { x % 2 == 0 } @@ -57,4 +87,5 @@ fn fullname(first: &str, second: &str, last: &str) -> String { second = second, last = last, ) -} \ No newline at end of file +} +