diff --git a/src/arithmetic_operations.cairo b/src/arithmetic_operations.cairo new file mode 100644 index 0000000..b2fa5a1 --- /dev/null +++ b/src/arithmetic_operations.cairo @@ -0,0 +1,29 @@ +// solution to: +// 1. Arithmetic Operations +// Subtraction: performs subtraction of two numbers. +// Multiplication: performs multiplication of two numbers. +// Division: performs division of two numbers (ensure handling of division by zero). + + +pub fn subtract(x: i32, y: i32) -> i32 { + // subtract and return the res + x - y +} + +pub fn multiply(x: i32, y: i32) -> i32 { + x * y +} + + +pub fn divide(x: i32, y: i32) { + // condition to handle an edge case where there is a denominator of zero + if y == 0 { + println!("Division by zero is undefined!"); + return; + } + + let res = x / y; + println!("{}", res); +} + + diff --git a/src/basic_logic_functions.cairo b/src/basic_logic_functions.cairo new file mode 100644 index 0000000..0975e56 --- /dev/null +++ b/src/basic_logic_functions.cairo @@ -0,0 +1,31 @@ +// Solution to: + +// 4. Basic Logic Functions +// Number Sign Checker: Implement a function to check if a number is positive, negative, or zero. +// Maximum of Two Numbers: Implement a function to return the maximum of two numbers. + + +pub fn sign_checker(x: i128) { + // condition checking to see if x is a positively signed integer + if x > 0 { + println!("Positive!"); + // condition checking to see if x a negatively signed integer + } else if x < 0 { + println!("Negative!"); + } else { + // default condition if x is zero + println!("Zero"); + } +} + + +pub fn larger_num(x: u32, y: u32) { + // checks to see if x is larger than y + if x > y { + // print the value of x if it is larger + println!("{x}"); + } else { + // print y if x is not largest + println!("{y}"); + } +} diff --git a/src/even_and_odd.cairo b/src/even_and_odd.cairo new file mode 100644 index 0000000..5b636ca --- /dev/null +++ b/src/even_and_odd.cairo @@ -0,0 +1,31 @@ +// Solution to: + +// 2. Even or Odd Sum +// Implement a function that takes two arguments, computes their sum, and determines whether the sum is even or odd. + +// 3. Odd Number Checker +// Implement a function that checks if a given number is odd. + +pub fn is_sum_even(x: i32, y: i32) { + // storing the result of x + y in the sum variable + let sum = x + y; + // checking if the result of the sum is even or odd + if sum % 2 == 0 { + // if even print true + println!("{true}"); + } else { + // if odd print odd + println!("{false}"); + } +} + +pub fn is_odd(x: i32) { + // checking to see if x is not an even number + if x % 2 != 0 { + // if odd print true + println!("{true}"); + } else { + // if even print true + println!("{false}"); + } +} diff --git a/src/lib.cairo b/src/lib.cairo index 2861ea0..35d9b93 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -1,52 +1,32 @@ +// Importing then + +mod arithmetic_operations; +mod basic_logic_functions; +mod even_and_odd; + +// Importing then +use arithmetic_operations::{subtract, multiply, divide}; +use basic_logic_functions::{sign_checker, larger_num}; +use even_and_odd::{is_sum_even, is_odd}; + + + fn main() { - // Function calls (Uncomment to execute them) - // say_name("Sylvia Nnoruka!"); - // intro_to_felt(); - - let num_1 = 5; - let num_2 = 10; - let sum = sum_num(num_1, num_2); - println!("The sum of {} and {} is = {}", num_1, num_2, sum); - - // check_u16(6553); // Uncomment if needed - is_greater_than_50(3); -} + // using imported functions in my main file + is_sum_even(4, 7); + is_odd(3); + sign_checker(-3); + larger_num(2, 6); + let sub_res: i32 = subtract(9, 1); + println!("subtraction result: {sub_res}"); -// DATA TYPES IN CAIRO -// - felts: felt252 (Field elements) -// - ByteArray: Represents a sequence of bytes -// - Integers: -// - Signed: i8, i16, i32, i64, i128, i256 -// - Unsigned: u8, u16, u32, u64, u128, u256 -// - Boolean: bool - -// Function to demonstrate ByteArray usage -fn say_name(x: ByteArray) { - println!("{}", x); -} + let mul_res = multiply(15, 2); + println!("multiplication result: {mul_res}"); -// Function to demonstrate felt252 usage -fn intro_to_felt() { - let x = 40000; - println!("{}", x); + divide(3, 10); + } -// Function to sum two u8 integers -fn sum_num(x: u8, y: u8) -> u8 { - return x + y; -} -// Function to print a u16 integer -fn check_u16(x: u16) { - println!("{x}"); -} -// Function to check if a u32 integer is greater than 50 -fn is_greater_than_50(x: u32) -> bool { - if x > 50 { - println!("true"); - return true; - } - println!("false"); - return false; -} +