Tuesday, 23 July 2013

E-Commerce

/* 1. Write a program to find the modular arithmatic
Eg. 27/5 result in r = 5
   -18/4 result in r=10
*/
#include<iostream.h>
#include<conio.h>
int main()
{
    int no1, no2;
    clrscr();

    cout << "Enter the two no : ";
    cin >> no1 >> no2;

    if(no1>0 && no2 >0)
    {
cout << no1 << " mod " << no2 << " = " << no1%no2;
    }
    else
    {
cout << no1 << " mod " << no2 << " = " << (no1%no2)+no2;
    }


    getch();
    return 0;
}

// 2. check a congruence to b (mod n) or not
#include<iostream.h>
#include<conio.h>
int main()
{
    int no1, no2, no;
    clrscr();

    cout << "Enter a and b : ";
    cin >> no1 >> no2;
    cout << "Enter n : ";
    cin >> no;

    if( (no2-no1)%no == 0)
    {
cout << no2 << " = " << no1 << " (mod " << no << ")";
    }
    else
    {
cout << no2 << " != " << no1 << " (mod " << no << ")";
    }


    getch();
    return 0;
}

// 3. fIND ADDITIVE INVERSE FOR NO1 OR PERTICULAT Zm
#include<iostream.h>
#include<conio.h>
int main()
{
    int no1, no;
    clrscr();

    cout << "Enter No1 : ";
    cin >> no1 ;
    cout << "Enter n : ";
    cin >> no;

    for( int i=1; i<no; i++ )
    {
if((no1+i)%no == 0)
   cout << "Additive inverse = (" << no1 << " , " << i << ")";
    }

    getch();
    return 0;
}

// 4. pairs of additive and multiplicative invese
#include<iostream.h>
#include<conio.h>
int main()
{
    int no1, no, i, j, temp;
    clrscr();

    cout << "Enter n : ";
    cin >> no;
    for(i=0; i<=no/2 ;i++)
    {
      for( j=1; j<no; j++ )
      {
if((i+j)%no == 0)
   cout << "(" << i << " , " << j << ")" << endl;
      }
    }
    // Multiplicative inverse
    cout << endl << "Multiplicative Inverse " << endl ;
    for(i=0; i<(no/2)+1 ;i++)
    {
      for( j=1; j<no; j++ )
      {
if((i*j)%no == 1 && i==j)
   cout << "(" << i << " , " << j << ")" << endl;
      }
      for( j=1; j<no; j++ )
      {
if((i*j)%no == 1 && i!=j)
   cout << "(" << i << " , " << j << ")" << endl;
      }
    }
    getch();
    return 0;
}

// 5. fIND MULTIPLICATIVE INVERSE FOR NO1 OR PERTICULAT Zm
#include<iostream.h>
#include<conio.h>
int main()
{
    int no1, no;
    clrscr();

    cout << "Enter No1 : ";
    cin >> no1 ;
    cout << "Enter n : ";
    cin >> no;

    for( int i=1; i<no; i++ )
    {
if((no1*i)%no == 1)
   cout << "multiplicatve inverse = (" << no1 << " , " << i << ")";
    }

    getch();
    return 0;
}

// 6. write a program to find Multiplicative inverse using extended eculidian algorithm
#include<iostream.h>
#include<conio.h>
int main()
{
    int no1, no2, r1, r2, r=0, q, t1=0, t2=1, t=0;
    clrscr();

    cout << "Enter the two no : ";
    cin >> no1 >> no2;
    r1 = no1;
    r2 = no2;
cout << "q\t|r1\t|r2\t|r\t|t1\t|t1\t|t" << endl;
    while(r2>0)
    {
q=r1/r2;

r=r1-(q*r2);
t=t1-(q*t2);
cout << "*********************************************************" << endl;
cout << q << "\t|" << r1 << "\t|" <<  r2 << "\t|" << r << "\t|" << t1 << "\t|" << t2 << "\t|" << t << endl;

r1 = r2;
r2 = r;

t1 = t2;
t2 = t;
    }
    cout << "*********************************************************" << endl;
    cout << "\t|" << r1 << "\t|" <<  r2 << "\t|\t|" << t1 << "\t|" << t2 << "\t|" << endl;
    getch();
    return 0;
}