Basic programming course: Lesson #3 Operations

in devjr-s20w3 •  2 months ago  (edited)

Hello Steemians, welcome to my post and stay safe as you read through this post.

1000178150.png
Canvas


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

Arithmetic operations

This is used for mathematical calculations Luke multiplication (*), addition (+), division (/), subtraction (-), and other complex mathematical expression like exponentiation (^) and others. It is an operation that is mostly used to manipulate numerical values which have been discussed briefly.

Addition:
To sum two or more values we can use addition (+) to sum up our value. For example, If I want to sum up or add the value of 10 and 2 I will have to write it in the format below

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

Subtraction
This means minus one value (figure) from another which if we are to use the same figure 10-2 will be written as shown below.

// 2- subtraction = n - n2;
Print "The subtraction of " n " with " n2 " is equal to " subtraction;

Multiplication
If we want to multiply two values (figures) together, multiplication (*) is what we can use. For example, if we want to multiply ten by two 10*2 = 20 will be our answer which will be written as shown below in our code.

multiplication = n * n2; 
Print "The multiplication of " n " with " n2 " is equal to " multiplication;

Division:
To divide one value (figure) by another value (figure) we have to use the division (/) sign. For example, if we want to divide ten by two 10/2 = 5 will be written as shown below.

division = n / n2; 
Print "The division of " n " with " n2 " is equal to " division;

Module:
The module of 2 numbers is the remaining value that remains after we have divided them, for example, if I divide 7 by 2 it is not an exact division, in this case, it would result in 3 and the module would be the remaining, in this case, 1. I will use the symbol % to operate 2 integer or real variables with each other:

// 5- Module
module = n % n2;
Print "The remainder of dividing " n " with "

Comparison Operators

For us to compare two values and return a true or false (Boolean result) we are comparison operators that we need. Examples of comparison operators are;

  • Equality (==)
  • Inequality (!=)
  • Greater than (>)
  • Less (<)

All the above, are used when making decisions.

Equal To (==):
These are the operators that let us know whether two values are equal or not. True is return if two values are equal and false is return if the two values aren't equal. For example, if we say ten equals ten, our results will be true. 2==3 which in our code will be written as shown below.

// 1- Equal to
Print "The number " n " is equal to " n2 "? " n==n2;
Print "The number " n " is equal to " n3 "? " n==n3;

Having looked at that above, in a situation where we say two values are not the same for example we say if 5 isn't equal to 3, our results will be true. '5!=3` True


Greater Than (>):
This is the operator (sign) that lets us know the greater value (higher) than other values. For example, twenty is greater than five, and the result will be true. 2>2 True.

// 3- Greater than
Print "The number " n2 " is greater than " n "? " n2>n;
Print "The number " n " greater than " n2 "? " n>n2;

Less Than (<):
This is the opposite of the greater than (>) operator. It is an operator that is used in checking the value that is less than another value. For example, if 5 is less than 10 the result will be true 2<2 True

// 4- Less than
Print "The number " n2 " is less than " n "? " n2<n;
Print "The number " n " less than " n2 "? " n<n2;

Logical Operators:

This is to combine or invert Boolean values. For example, AND (&&) returns true if both conditions are true, while OR (II) returns if at least one condition is true. Decision-making and control flow are what they are used for.

AND (&&):
As introduced earlier when both conditions are true we can use AND (&&). For example, let's say a Steemian is over or equal to 20 years old and is single the expression will be TRUE.

Print "Does the user meet both conditions? " is_older and is_single;


OR (||):
As introduced earlier if we have two conditions then if one of these conditions is true it will give us true as our answer, but if both conditions aren't met it will give us FALSE. For example, using the same age we have used above.

Print "Does the user meet at least one condition? " is_older or is_single;


Make a program that asks the user for 2 numbers and evaluates whether both numbers are equal.

Here I will be using a Python program that I am familiar with to run this program ghag as the user for 2 numbers and check whether both numbers are equal. Below is the code.

# Asking for two numbers from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

# Checking if the two numbers are equal
if num1 == num2:
    print("The numbers are equal.")
Else:
    print("The numbers are not equal.")

Code
1000178642.jpg

Output
1000178640.jpg

  • 10 is the first number that I enter.

  • 5 is the second number that I enter.

The above program prompts the user to enter 2 numbers, convert the input to float (to handle both decimals and integers), and compare both numbers using the == operator which we have learned. It then prints whether the numbers are equal or not equal.


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

Here I made use of a Online IDE editor program to transform mathematical expressions into arithmetic expressions;
For the first expression, it is 80 and 70

* First = 80
* Second = 70

### Arithmetic expressions
sum_result = first + second  # Addition
diff_result = first - second  # Subtraction
prod_result = first * second  # Multiplication
div_result = first / second  # Division

# Output of the results
print("Sum:", sum_result)
print("Difference:", diff_result)
print("Product:", prod_result)
print("Quotient:", div_result)

Code
1000178615.jpg

Output
1000178617.jpg

For the second expression (60 and 60)

First = 60
Second = 60

### Arithmetic Expressions 
sum_result = first + second  # Addition
diff_result = first - second  # Subtraction
prod_result = first * second  # Multiplication
div_result = first / second  # Division

# Out of the results
print("Sum:", sum_result)
print("Difference:", diff_result)
print("Product:", prod_result)
print("Quotient:", div_result)

Code
1000178619.jpg

Output
1000178621.jpg

Using the above code will help us to calculate and print the sum, difference product, and quotient for each of the numbers.

I am inviting: @pelon53, @dove11, @simonnwigwe, and @ruthjoe

Cc:-
@alejos7ven

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  
Loading...

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

Great job as ever, my best wishes to you for continued success.

Thank you so much