MATHEMATICAL FOUNDATIONS LAB USING CPP

// Truth Table for Two Variables

#include <iostream.h>

#include <conio.h>

 

void main()

{

    int P, Q;

 

    clrscr();

 

    cout << "Truth Table for Two Variables P and Q\n\n";

 

    cout << "P\tQ\tAND\tOR\tNOT P\tXOR\tP->Q\tP<->Q\n";

    cout << "--------------------------------------------------------\n";

 

    for (P = 0; P < 2; P++)

    {

        for (Q = 0; Q < 2; Q++)

        {

            cout << P << "\t";

            cout << Q << "\t";

 

            /* AND */

            cout << (P && Q) << "\t";

 

            /* OR */

            cout << (P || Q) << "\t";

 

            /* NOT P */

            cout << (!P) << "\t";

 

            /* XOR */

            cout << (P != Q) << "\t";

 

            /* Conditional P -> Q */

            cout << ((!P) || Q) << "\t";

 

            /* Biconditional P <-> Q */

            cout << (P == Q) << endl;

        }

    }

 

    getch();

}


 

OUTPUT

Truth Table for Two Variables P and Q

 

P       Q       AND     OR      NOT P   XOR     P->Q    P<->Q

--------------------------------------------------------

0       0       0       0       1       0       1       1

0       1       0       1       1       1       1       0

1       0       0       1       0       1       0       0

1       1       1       1       0       0       1       1

 

// BASIC RULES OF INFERENCE

#include <iostream.h>

#include <conio.h>

 

void main()

{

    int p, q;

 

    clrscr();

 

    cout << "      BASIC RULES OF INFERENCE\n";

 

    cout << "\nEnter value of p (1=True, 0=False): ";

    cin >> p;

 

    cout << "Enter value of q (1=True, 0=False): ";

    cin >> q;

 

 

    /* MODUS PONENS */

 

    cout << "\n1. MODUS PONENS\n";

    cout << "----------------------------\n";

 

    cout << "Premise 1 : p = " << p << endl;

    cout << "Premise 2 : p -> q = "

         << ((!p) || q) << endl;

 

    if (p && ((!p) || q))

    {

        cout << "Conclusion : q = " << q << endl;

        cout << "Modus Ponens is VALID.\n";

    }

    else

    {

        cout << "Premises are not TRUE.\n";

    }

 

 

    /* MODUS TOLLENS */

 

    cout << "\n2. MODUS TOLLENS\n";

    cout << "----------------------------\n";

 

    cout << "Premise 1 : p -> q = "

         << ((!p) || q) << endl;

 

    cout << "Premise 2 : NOT q = "

         << (!q) << endl;

 

    if (((!p) || q) && (!q))

    {

        cout << "Conclusion : NOT p = "

             << (!p) << endl;

 

        cout << "Modus Tollens is VALID.\n";

    }

    else

    {

        cout << "Premises are not TRUE.\n";

    }

 

    getch();

 

}

 

OUTPUT

 

      BASIC RULES OF INFERENCE

 

1. MODUS PONENS

----------------------------

Premise 1 : p = 1

Premise 2 : p -> q = 1

Conclusion : q = 1

Modus Ponens is VALID.

 

2. MODUS TOLLENS

----------------------------

Premise 1 : p -> q = 1

Premise 2 : NOT q = 0

Premises are not TRUE.

 

//EQUIVALENCE RELATION

#include <iostream.h>

#include <conio.h>

 

int main()

{

    int a[10][10], n;

    int i, j, k;

    int r = 1, s = 1, t = 1;

 

    clrscr();

 

    cout << "Enter number of elements: ";

    cin >> n;

 

    cout << "Enter relation matrix:\n";

 

    for(i = 0; i < n; i++)

        for(j = 0; j < n; j++)

            cin >> a[i][j];

 

    /* Reflexive */

    for(i = 0; i < n; i++)

    {

        if(a[i][i] != 1)

            r = 0;

    }

 

    /* Symmetric */

    for(i = 0; i < n; i++)

    {

        for(j = 0; j < n; j++)

        {

            if(a[i][j] != a[j][i])

                s = 0;

        }

    }

 

    /* Transitive */

    for(i = 0; i < n; i++)

    {

        for(j = 0; j < n; j++)

        {

            for(k = 0; k < n; k++)

            {

                if(a[i][j] == 1 && a[j][k] == 1)

                {

                    if(a[i][k] == 0)

                        t = 0;

                }

            }

        }

    }

 

    cout << "\nReflexive  : ";

    if(r == 1)

        cout << "Yes";

    else

        cout << "No";

 

    cout << "\nSymmetric  : ";

    if(s == 1)

        cout << "Yes";

    else

        cout << "No";

 

    cout << "\nTransitive : ";

    if(t == 1)

        cout << "Yes";

    else

        cout << "No";

 

    if(r == 1 && s == 1 && t == 1)

        cout << "\n\nIt is an Equivalence Relation.";

    else

        cout << "\n\nIt is NOT an Equivalence Relation.";

 

    getch();

    return 0;

}

 

OUTPUT

Enter number of elements: 3

Enter relation matrix:

1 1 0

1 1 0

0 0 1

 

Reflexive  : Yes

Symmetric  : Yes

Transitive : Yes

 

It is an Equivalence Relation.


Comments

Popular posts from this blog

Data Structure - Lab

Data Structure - Binary Tree

Mathematical Foundation - Unit 1