Basic programming course: Lesson #3 Operations

in #devjr-s20w32 months ago
Hello Everyone
I'm AhsanSharif From Pakistan
Greetings you all, hope you all are well and enjoying a happy moment of life with steem. I'm also good Alhamdulillah.

image.png
Made in Canva

Completing the last two tasks taught us a lot about programming. And so this learning series of ours continues and we will continue to learn something new from here till the end. Likewise, we will try our best to perform today's task according to the knowledge we have gained from here. I hope you guys like my task.

You must have guessed that I do programming in C++. So I have dev c plus plus software for that. And on it, I will solve my example and perform the given task.

So let's move on to our first task.

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

Arithmetic Operations

We use arithmetic operations to perform all the calculations in mathematics, especially those involving numerical objects. As integer floats etc. are included in it. The best operation for doing arithmetic is the arithmetic operation and it is the backbone of it.

Some common operators in Arithmetic can be used to do this.

Operators:

Addition:

To sum two values we use addition, which we know by the plus (+) sign. For example, if we want to add five and three together, we will solve this in C plus plus. 5+3=8

In C++:

int b = 3;
int sum = a + b;  // sum is 8

Subtract:

To subtract one value from another we use subtraction. We know this by the sign of negative (-). For example, we will subtract four out of ten. 10-4=6

In C++:

int b = 4;
int difference = a - b;  // difference is 6

Multiplication:

To multiply two values together we use multiplication. This doubles the value. We know this by the star (*) sign. For example, we have to multiply seven by two. 7*2=14

In C++:

int b = 2;
int product = a * b;  // product is 14

Division:

To divide one value by another value we use division, we know it by the (/) sign. For example, we have to divide eight by two. 8/2=4

In C++:

int b = 2;
int quotient = a / b;  // quotient is 4

Modulus:

This returns us to the remainder after division, which we know from the sing of percentage (%). For example, we have to divide 10 by 3, so it will remind us. 10%3=1

In C++:

int b = 3;
int remainder = a % b;  // remainder is 1

Practical Example of All Operators

image.png

image.png

Comparison Operations

To compare two things we use the comparison operation to determine the relationship between them. We often get back a Boolean value that will either be true or false. Often we also find it in conditional statements where we use if or while. There are some common comparison operators that we use in general.

Operators:

Equal To (==):

This lets us know if the two values are equal or not. If both values are equal, it will return true. For example, if five equals five, the result will be true. 5==5 True

In C++:

int b = 5;
bool isEqual = (a == b);  // isEqual is true

Not Equal To (!=):

It checks that the two values are different and do not match. For example, if 5 is not equal to 2, the result will be true. 5!=2 True

In C++:

int b = 3;
bool isNotEqual = (a != b);  // isNotEqual is true

Greater Than (>):

This sign is used to check that one value is greater than another value. For example, seven is greater than three, so its result will also be true. 7>3 True

In C++:

int b = 3;
bool isGreater = (a > b);  // isGreater is true

Less Than (<):

It is also a comparison operator that checks if one value is less than another value. For example, four is smaller than eight. Its result will also be true. 4<8 True

In C++:

int b = 8;
bool isLess = (a < b);  // isLess is true

Greater Than or Equal To (>=):

The function of this operator is to check that one value is either greater than or equal to another. For example, if six is greater than or equal to six, the result is true. 6>=6 True

In C++:

bool isGreaterOrEqual = (a >= 6);  // isGreaterOrEqual is true

Less Than or Equal To (<=):

The function of this operator is to specify that a value is either less than or equal to another value. For example, if five is less than or equal to nine, the result will also return true. 5<=9 True

In C++:

bool isLessOrEqual = (a <= 9);  // isLessOrEqual is true

Practical Example of All Operators

image.png
image.png

image.png

Logic Operations

To combine different conditions and expressions, we use logical operations. This also often gives us an entry in true and false. If the value is true, it will return true, if the value is false, it will return false according to combinations. There are three commonly used logical operators.

Operators

AND (&&):

If both the conditions are true then it will also give us the answer true. For example, we have two conditions one condition is five greater than two and the other condition is 8 less than 10 so it will give us a true result. 5>2 & 8<10True`

In C++:

bool condition = (5 > 2) && (8 < 10); // true because two conditions are true

OR (||):

If we have two conditions then if one of these conditions is true it will give us true in the answer. For example, we have two conditions in one condition three is greater than five and in the other condition seven is less than nine so one of the conditions is true so it will also give us the answer true. 3>5 || 7<9 True

In C++:

bool condition = (3 > 5) || (7 < 9); // true because one condition is true

Not (!):

This operator of the logic operation changes the original condition. If we have the right answer, it ignores it. For example, four is equal to four and this result is correct. But because of the sign(!) it will reverse it so the result will be false. 4==4 True But ! negates it so it's False

In C++:

bool condition = !(4 == 4); // false because 4 == 4 is true, and ! negates it

Practical Example of All Operators

image.png

image.png

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

In this program, we will take two values from the user which will tell if it is the same or opposite of each other. I will do this program in Dev C Plus Plus. Because I have that software installed and I do my coding in it.

Below is a picture of my program.

image.png

Below is the output images of the program.

Different ValuesSame Values
Evaluate if the results of the 3 operations are greater than or equal to 0 (>=) and show it on the screen.

Below is a picture of my program.

image.png

Below is the output image of the program.

image.png

It has all values greater than or equal to zero, so all values are non-negative.

  • Y = 54
  • X = 5
  • Z = 6

Thank you so much for staying on my post. I would like to invite my friends @rumaish, @josepha, and @aviral123 to join the challenge.

Cc:
@alejos7ven

Dated: 23-09-2024 About Me
Sort:  
Loading...

Great job, I enjoyed it. I hope you can figure out my riddles too? ))) I want to set difficult tasks. But I used to think that there are no people here who learn to program.

Thank you so much for staying here. Yes absolutely I do my best to figure out your riddle's too. I wish you more success.

Your post has been rewarded by the Seven Team.

Support partner witnesses

@seven.wit
@cotina
@xpilar.witness

We are the hope!

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

image.png

Thank you so much dear @httr4life for your kind support and love.

This post is awesome! You have explained important programming concepts like arithmetic, comparison and logic operations very well. The examples you have given are also very useful, helping readers to understand the practical use of these operations in C++.

Your explanation is not only comprehensive but suitable not only for beginners but also for those who want to refresh their programming knowledge.

Your programming learning curve is really commendable. May Allah grant you more success!

Tell me your Whatsapp no plz

Thank you so much for your support. I appreciate the commendation.

+923068037647