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;
}

Monday, 22 July 2013

Core Java

Write a java program to count the frequency of words
import java.io.*;
import java.util.*;
public class FrequencyOfWords
{

    public static void main(String[] args)
    {
        System.out.print("\nEnter Some Text:");

        Scanner input=new Scanner(System.in);

        String text = input.nextLine();
        String ans = "";
        do
        {

            System.out.print("\nEnter A Word You Want To Verify The Frequency:");
            String word = input.nextLine();

            int count = 0;

            StringTokenizer st = new StringTokenizer(text);

            while (st.hasMoreTokens())
            {
                if (st.nextToken().equals(word))
                {
                    count++;
                }
            }

            System.out.println("\nFrequency of \"" + word + "\" is " + count + " times");
            System.out.print("\nDo You Want Verify The Frequency Of One More Word[Y/N]:");

            ans = input.nextLine();
        } while (ans.equals("Y") || ans.equals("y"));
    }

}
Click Here to Download










Write a program to store coordinates ( x, y, z) for data points in 3 Dimensional space. Prompt for how many points to consider and then collect the coordinates as x1, y1, z1, x2, y2, z2, ........ xn, yn, zn (Where n is the number of data points. ). Find the distances among all pairs of points and then display the pair of points having the minimum distance as given by the formula
Distance = Sqrt [ (x1-x2)^2 + (y1-y2)^2 +   (z1-z2)^2 ] 

import java.util.Scanner;
import java.lang.*;

public class Distance
{
double dist[] = new double [10];
double d;
int count=0;
double x1, x2, y1, y2, z1, z2;
double min;


public void distance(double xx1, double yy1, double zz1, double xx2, double yy2, double zz2)
{
dist[count] = Math.sqrt( ((xx1-xx2)*(xx1-xx2)) + ((yy1-yy2)*(yy1-yy2)) + ((zz1-zz2)*(zz1-zz2)) );
System.out.println("The distance between the two points is (" + xx1 + "," + yy1 + "," + zz1 + ") (" + xx2 + "," + yy2 + "," + zz2 + ") = "+ dist[count] + " .");

    if(count>0)
    {
if(dist[count]<dist[count-1])
{
min=dist[count];

x1=xx1;y1=yy1;z1=zz1;x2=xx2;y2=yy2;z2=zz2;

}
}
else
{
min=dist[count];
x1=xx1;y1=yy1;z1=zz1;x2=xx2;y2=yy2;z2=zz2;
}count++;
    }
public static void main(String[] args)
{
double x[][] = new double [10][10];
double y[][] = new double [10][10];
double z[][] = new double [10][10];
int no,temp=0;

Scanner scan = new Scanner (System.in);

System.out.println("Enter the number of points : ");
no = scan.nextInt();

for(int i=0; i<no; i++)
{
for(int j=0; j<3; j++)
{
System.out.println("Enter the x coordinate for point: "+ i + " " + j);
x[i][j] = scan.nextDouble();
}
}

Distance obj = new Distance();

for(int i=0; i<no-1; i++)
{
for(int j=i+1; j<no; j++)
{
obj.distance(x[i][0],x[i][1],x[i][2],x[j][0],x[j][1],x[j][2]);
}
}
System.out.println("The Shortest distance between the two points is (" + obj.x1 + "," + obj.y1 + "," + obj.z1 + ") (" + obj.x2 + "," + obj.y2 + "," + obj.z2 + ") = "+ obj.min + " .");
}
}
Click Here to Download