DeMorgan's theory in C++ - ProgrammerTech


DeMorgan's theory in C++

  • Share this:
DeMorgan's theory in C++

 Demorgan's theory C++

DeMorgan's theory is used in the logic rules of programming in order to describe the inverse result of multiplication and addition in logic.

  • Boolean multiplication and.
  • Boolean plural or .

So that:

  1. A sign ( ! ) denotes a logical NOT.
  2. The sign ( && ) denotes a logical AND.
  3. The sign ( || ) denotes the logical combination OR.
  4. The sign ( == ) makes two logically equal.

In the previous lesson, we studied the concept of remainder of division. In the last example, we wrote the code and first made sure that the entry was greater than 100 and at the same time less than 999.

You may disagree with us in that you want to make sure first that the entrance is wrong and not that you make sure that the entrance is correct, you will only do the opposite of the question, so what will you ask?

There are two options for us to reverse the question, the first is to put in parentheses at the beginning the negative sign!, that is, if the condition is not met, print.


The effect of the negative sign on the conditional sentence

If you have a conditional sentence and you enter an exclamation point! It will reverse everything with it.

For example, this condition x==5 means equal to 5 if we include the negative sign for it! It will invert it and x!=5 which is not equal to 5.

This may seem obvious because it is a simple process, but if it is.

x > = 100 && x <= 999;

This is when we enter her exclamation point! you will be

(x < 100 || x > 999)

We see that all greater than or equal to >= are converted to less than <=.

And if it has an equal sign = it will remove the equal sign and turn all and any && into or any ||.

And everything less than or equal to becomes less than, and this is called the Demorgan rule.

In comparison operations greater than or less than and with it is equal to the exclamation point! You extract the equal sign from it and reflect it.

And if it is not worth the exclamation point! It will be worth it.

example 1

#include <iostream>
using namespace std;
int main()
    int x;
    cout << "Write the number three digitsت" << endl;
    cin >> x;

    if(x < 100 || x > 999)
        /wrong number
        cout << "Bye, the number is wrong" << endl;
    else {
           /correct number
        int a1 = x % 10;
        int a2 = (x / 10) % 10;
        int a3 = x / 100;
        cout << a1 << a2 << a3 << endl;
    return 0;

We created a scalar variable int which is x and its value will be entered by the user via cin.

We then printed a message for the user telling him to compulsorily enter a three-digit number to be reversed.

Next we set a function cin which will ask the user to enter the value of x and receive it.

And we set a condition through the if conditional function which is inverse, if x is less than 100 and x is greater than 999 is entered. In two conditions: the first is that the condition is wrong, the user is printed with the wrong number and expels the user from the program, and the second condition is if the condition is true.

We created three variables, a1 for the ones place, a2 for the tens place, and a3 for the hundreds place, and their conditions were in the ones place.

We extract the value of ones based on the fourth property from the previous properties.

And for the tens place a2 we also rely on the fourth property to extract the tens digit via (x/10) %10.

And then in the third column, the hundreds digit, we divide x by 100, because if we divide by 100, the program will delete the ones digit and the tens digit and keep the hundreds digit.

When executing the code and typing a three-digit number 789, it will reverse it to 987.

987   It was instantly reversed

Example 2

Write a program that will test the number entered by the user if the number is positive or negative and print the result on the screen. And then he tests it if it is even or odd and prints the result on the screen below the previous result.

#include <iostream>
using namespace std;
int main()
    cout << "Enter a integer number ....\n";
    int number;
    cin >> number;
    if (number >= 0)
        cout << number << " Positive number" << endl;
        cout << number << " Negative number" << endl;
    if (number % 2 == 0)
        cout << number << " even number" << endl;
        cout << number << " An odd number" << endl;
    return 0;

Through the cout function, we wrote a message to the user to write an integer, and in the second line we defined an integer variable named number.

And we, through the cin function, asked the user to write the value of the variable number, and it must be an integer.

Then we put a conditional sentence if we say if the entered number is greater than zero number >= 0 then it is positive and then write the number and then print its type if it is positive.

Then we wrote an else clause that prints the number and after it if its type is negative.

And we wrote another conditional sentence in order to find out an even or odd number. We wrote if if the number is a remainder, divide it by two. is zero, print as an even number, else print as an odd number.

If we take out an odd positive number,



If we enter a negative even number,



example 3

Write a program that calculates the difference between two numbers and prints the difference on the screen.

#include <iostream>
using namespace std;
int main()
    int integer1;
    int integer2;
    cout << "Enter the first number...\n";
    cin >> integer1;
    cout << "Enter the second number...\n";
    cin >> integer2;
    int diff = integer1 - integer2;
    cout << "The difference is : ";
    if(diff >= 0)
        cout << diff;
        cout << -1 * diff;
    cout << endl;
    return 0;

We created two integer variables, integer1 and integer2 but we didn't give them values ​​because the user will enter them.

Then we showed a message to the user to enter the first number and we wrote the cin function responsible for asking the user to enter data for the program.

And after he enters the first number, we ask him to enter the second number and ask him to enter its value via cin.

After that, we created a variable named diff to be tasked with subtracting the second number from the first and printing the word difference is.

And then he checks the difference, if it is positive, he will print it directly.

And else else we put the print is to multiply two negative numbers together and we know that a negative multiplying a negative gives a positive.

And when executing and writing the two numbers he asked, we will see the result in the form.



As we noticed through the previous execution screen, we entered him the first number 17 and then the second number 9, and he directly printed the number 8, which is the difference between the two numbers entered for this program.

Example 4

Write a program that tests the largest value from three numbers entered by the user and prints the largest value on the screen.

#include <iostream>
using namespace std;
int main()
    int x1;
    int x2;
    int x3;
    cout << "Enter three values" << endl;
    cin >> x1 >> x2 >> x3;
    int max = x1;
    if(max < x2)
        max = x2;
    if(max < x3)
        max = x3;
    cout << "value max : " << max << endl;
    return 0;

We have defined three non-valued variables x1,x2,x3 which the user will enter upon request.

And we showed a message asking the user to enter three values, and the largest value will be shown to him.

And we set the function cin responsible for asking the user to enter the three values.

And we created a variable named max, which is the maximum value, and its initial value is x1.

Then we compare the variable max with x2, i.e. is it less than x2 means is there a number greater than max if it is. There is a number whose value is greater than max. If there is, then max is not the maximum value.

Then we set max to be x2 and then compare it with x3 in the same way as before with x2.

Then we asked him to print the text of the maximum value and write the value after it.

When you run the program and write three numbers it will be in the form.

Enter three values
value max : 95

We note from the previous result that the user entered three values, which are 45, 95 and 66, and when he clicked on the enter button, he printed the number 95 because it is the largest number of the entered values.

We learned how to use the remainder of the division earlier, and in this lesson we learned about an important property. And is the introduction of the exclamation mark! on the condition which is known as Demorgan's rule which negates the condition.

Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.