Basic programming course: Lesson #3 Operations

in #devjr-s20w33 days ago (edited)

Hello, my fellow programmers, it is the third week of the Steemit Engagement Challenge for season 20 and I'm delighted to be part of this week's contest organized by @alejos7ven which talks about Operations.

OIG3.A_jei7hYQD.NsgU.jpeg
Source

The lesson was quite an interesting one for me and at some point, I wished to continue reading through it over and over again as I always get something new from it anytime I do that. I will be attempting the questions given below by the professor. So without further ado let's get started.

mine.PNG

Give a brief summary of what arithmetic, comparison, and logical operations are for.

mine.PNG

Arithmetic Operations

Arithmetic operations are basic operations that are used often to perform mathematical calculations. We have a lot of these operations but for the sake of this study today, we are going to be looking at the basics such as multiplication (*), addition (+), division (/), subtraction (-) module (%), etc.

Now let's look at these operations one after the other to enable you to understand them better just as in the lesson delivered by the professor. We are going to take it one after the other then we will go ahead to combine it in one place to see how it works.

To begin we need to define the variables that we will use in the course of this class and below we will look at the data type needed for this operation.

Define x, x2, addition, subtraction, multiplication, division, module As Real;

// I'm using the Real data type in case of decimal points which are not accepted by integers or to minimize the memory space.

X=5;
X2=2;

Addition: In the case of the addition operator, the symbol + is used to add or sum 2 integer or real variables with each other:

Example
// 1- Addition
addition = x + x2;
Print "The sum of " x " with " x2 " is equal to " addition;

Subtraction: In the case of the subtraction operator, the symbol - to operate 2 integer or real variables with each other:

Example
// 2- Subtraction
Subtraction = x - x2;
Print "The subtraction of " x " with " x2 " is equal to " Subtraction;

Multiplication: In the case of the multiplication operator, the symbol * to operate 2 integer or real variables with each other:

Example
// 3- Multiplication
Multiplication = x * x2;
Print "The Multiplication of " x " with " x2 " is equal to " Multiplication;

Division: In the case of the multiplication operator, the symbol / to operate 2 integer or real variables with each other:

Example
// 4- Division
Division = x / x2;
Print "The Division of " x " with " x2 " is equal to " Division;

Module: In the case of the module operator, the symbol % is used to operate on 2 integer or real variables with each other. When we talk about module, we are referring to the reminder that two values and divided by each other. For instance, if you have the value 5 divided by 2, the reminder is 1 which is the module to our question.

Example
// 5- Module
Module = x % x2;
Print "The Module of " x " with " x2 " is equal to " Module;

So having done that, let's put all the work in one place and execute the program. The algorithm will be in the form below.

Algorithm Arithmetic Operations

Define x, x2, addition, subtraction, multiplication, division, module As Real;

// I'm using the Real data type in case of decimal points which are not accepted by integers or to minimize the memory space.

X=5;
X2=2;

// 1- Addition
addition = x + x2;
Print "The sum of " x " with " x2 " is equal to " addition;

// 2- Subtraction
Subtraction = x - x2;
Print "The subtraction of " x " with " x2 " is equal to " Subtraction;

// 3- Multiplication
Multiplication = x * x2;
Print "The Multiplication of " x " with " x2 " is equal to " Multiplication;

// 4- Division
Division = x / x2;
Print "The Division of " x " with " x2 " is equal to " Division;

// 5- Module
Module = x % x2;
Print "The Module of " x " with " x2 " is equal to " Module;

EndAlgorithm

IMG_20240925_103039.jpg
Code

IMG_20240925_103002.jpg
Result

Comparison Operators

In this type of operation, two or more variables are compared to obtain either a True or False answer. These two results are called Boolean. So there are different comparison operators available but today we will be looking at Equality (==), Different from (!=), Greater than (>), Greater than or equal to (>=) Less than (<), Less than or equal to (<=) etc.

Just like we did in the arithmetic operations, we are going to look at the comparison operators one after the other before we combine them into a single code and run it via the Editor. So let's get started.

To effectively carry out this task, I will be declaring the variables to use in our comparison and we are going to be declaring three variables as seen below.

Define x, x2, x3 As Real;
X=3;
X2=5;
X3=3;

Equal to In the case of the equal to operator, the symbol == is used to operate on 2 integer or real variables. If both variables are the same, the program returns TRUE but if they are different, it will return FALSE. Let's take into consideration the variable we have declared above.

// 1- Equal to
Print "The number " x " is equal to " x2 "? " X==x2;
Print "The number " x " is equal to " x3 "? " X==x3;

From the above sample, x==x2 will print FALSE because the numbers are not the same whereas x==x3 will print TRUE. After all, the numbers are the same.

Different from: In the case of the Different from operator, the symbol != is used to operate on 2 integer or real variables. Here the program checks if the variables have different values. If they have different values, it should print TRUE but if the values are the same it should print FALSE. Let's take into consideration the variable we have declared above.

// 2- Different from
Print "The number " x " is equal to " x2 "? " X==x2;
Print "The number " x " is equal to " x3 "? " X==x3;

From the above sample, x!=x2 will print TRUE because the numbers are not the same whereas x!=x3 will print FALSE. After all, the numbers are the same.

Greater than: In the case of the Greater than operator, the symbol > is used to operate on 2 integer or real variables. Here 2 numbers are compared and if the first is greater than the second we get True but if it is not we get false.

//3- Greater than
Print "The number " x2 " is greater than " x "? " X2>x;
Print "The number " x " greater than " x2 "? " x>x2;

Greater than or equal to: In the case of the Different from operator, the symbol >= is used to operate on 2 integer or real variables. Here if the two numbers are equal to each other it will return TRUE and if it is less it will return FALSE.

//4- Greater than or equal to
Print "The number " x " greater than or equal to " x3 "? " x>=x3;

Based on our data above, this result shoulpld print True because x>=x3.

Less than: In the case of the Less than operator, the symbol < is used to operate on 2 integer or real variables. Here 2 numbers are compared and if the first is less than the second we get TRUE but if it is not we get FALSE.

//5- Less than
Print "The number " x2 " is less than " x "? " X2<x;
Print "The number " x " less than " x2 "? " x<x2;

Less than or equal to: In the case of the Different from operator, the symbol <= is used to operate on 2 integer or real variables. Here if the two numbers are equal to each other it will return TRUE and if it is greater it will return FALSE.

//6- Less than or equal to
Print "The number " x " less than or equal to " x3 "? " x>=x3;

Based on our data above, this result shoulp print True because x<=x3.

So having done that, let's put all the work in one place and execute the program. The algorithm will be in the form below.

Algorithm Comparison Operations

Define x, x2, x3 As Real;
X=3;
X2=5;
X3=3;

// I'm using the Real data type in case of decimal points which are not accepted by integers or to minimize the memory space.

// 1- Equal to
Print "The number " x " is equal to " x2 "? " X==x2;
Print "The number " x " is equal to " x3 "? " X==x3;

// 2- Different from
Print "The number " x " is equal to " x2 "? " X==x2;
Print "The number " x " is equal to " x3 "? " X==x3;

//3- Greater than
Print "The number " x2 " is greater than " x "? " X2>x;
Print "The number " x " greater than " x2 "? " x>x2;

//4- Greater than or equal to
Print "The number " x " greater than or equal to " x3 "? " x>=x3;

//5- Less than
Print "The number " x2 " is less than " x "? " X2<x;
Print "The number " x " less than " x2 "? " x<x2;

//6- Less than or equal to
Print "The number " x " less than or equal to " x3 "? " x>=x3;

EndAlgorithm

IMG_20240925_103107.jpg
Code

IMG_20240925_103201.jpg
Result

Logic Operators

In this type of operation, two or more variables are compared at the same time. For instance saying a a guy is +18 and that the guy he is not yet married. Here we use the AND, OR to compare and make decisions. Let's look at them one after the other.

And logical: Here, Evaluation of two expression are checked and are assured that the meet the criteria. If for any reason one of them doesn't meet the criteria then it will return false but if it meets the criteria it will return true. The symbol use to represent it is ampersang symbol (&) or the word and (AND) literally. Let's consider an example code.

Given the criteria

is_older = age>=18;
is_single = civil_state=="single";

// 1- AND
Print "Does the user meet both conditions? " is_older and is_single;

This program will return TRUE if the age is greater than or equal to 18 and if the user single. But if any of the condition is not correct it will return FALSE.

Or logical: Here, Evaluation of two expression are checked and are assured that at least one of them meet the criteria. If for any reason two of them doesn't meet the criteria then it will return FALSE but if anyone meets the criteria it will return TRUE. The symbol use to represent it is (OR) or the symbol and (||) literally. Let's consider an example code. Let's consider an example code.

Given the criteria

is_older = age>=18;
is_single = civil_state=="single";

// 2- OR
Print "Does the user meet at least one condition? " is_older or is_single;

This program will return TRUE if atleast one of the conditions are met and it returns FALSE when known of the conditions are met.

Algorithm Logical Operations

Define age as Integer;
Define civil_state as Character;
Define is_older, is_single as logical;
Print "Enter your age: ";
Read age;
Print "Enter your marital status (single/married):";
Read civil_state;
is_older = age>=18;
is_single = civil_state=="single";
// 1- AND
Print "Does the user meet both conditions? " is_older and is_single;
// 2- OR
Print "Does the user meet at least one condition? " is_older or is_single;

EndAlgorithm

IMG_20240925_103225.jpg
Code

IMG_20240925_103240.jpg
Result

mine.PNG

Make a program that asks the user for 2 numbers and evaluates if both numbers are the same.

mine.PNG

Algorithm for 2 numbers

Define x, x2 as Real;
Print "Enter your x: ";
Read x;
Print "Enter your x2:";
Read x2;
Print "The number " x " is equal to " x2 "? " X==x2;

EndAlgorithm

IMG_20240925_103257.jpg
Code

IMG_20240925_103317.jpg
Result

mine.PNG

Transform the following mathematical expressions into arithmetic expressions on your computer:

mine.PNG

Algorithm for expression

Define y, x, z as Real;
Define all_positive as Real;

//Expression 1

y = (8-2) * (5+4);
y = (8-2)(5+4);
Print "result:" y;

//Expression 2
X= 8/2 + 5/5;
Print "result:" x;

//Expression 3
z = (4 + 8) * 3/2/3
z = (((4+8)3)/2)/3;
Print "result:" z;

Check if x, y, and z are all greater than or equal to 0

all_positive = (x >= 0) and (y >= 0) and (z >= 0)
print(f"Are x, y, and z all greater than or equal to 0? {all_positive}")

EndAlgorithm

IMG_20240925_112211.jpg
Code

IMG_20240925_112224.jpg
Result

This is indeed a nice course as it reminds me a lot. I want to finally invite a few friends to also join the contest @josepha, @suboohi, and @solaymann.

Note: I used python programming language because it was difficult executing the program using the editor which the professor used in the task due to language barrier.

Sort:  
Loading...

Upvoted. Thank You for sending some of your rewards to @null. It will make Steem stronger.

@tipu curate

;) Holisss...

--
This is a manual curation from the @tipU Curation Project.

Your post has been rewarded by the Seven Team.

Support partner witnesses

@seven.wit
@cotina
@xpilar.witness

We are the hope!

CONGRATULATIONS!!

Your post has been supported by TEAM SHINING STARS. We support quality posts, good comments anywhere, and any tags.


1000152665.gif

Curated by : @josepha

This post has been upvoted/supported by Team 7 via @philhughes. Our team supports content that adds to the community.

image.png

Coin Marketplace

STEEM 0.20
TRX 0.15
JST 0.030
BTC 65641.55
ETH 2676.11
USDT 1.00
SBD 2.91