Skip to content

Cairo 4.0 Assignment Task 4 #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/arithmetic_operations.cairo
Original file line number Diff line number Diff line change
@@ -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);
}


31 changes: 31 additions & 0 deletions src/basic_logic_functions.cairo
Original file line number Diff line number Diff line change
@@ -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}");
}
}
31 changes: 31 additions & 0 deletions src/even_and_odd.cairo
Original file line number Diff line number Diff line change
@@ -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}");
}
}
70 changes: 25 additions & 45 deletions src/lib.cairo
Original file line number Diff line number Diff line change
@@ -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;
}