Pages

Big Factorial Using Java..Source Code

15 December 2013

Big Factorial!!! Have a fun..........


import
java.math.BigInteger;

public class Factorial {

    public static void main(String[] shan) {

            BigInteger bigInteger =  BigInteger.valueOf(1);

            for (int i = 2; i <= 100; i++)    // calculate 100!
 
                      bigInteger = bigInteger.multiply(BigInteger.valueOf(i));
          
                      System.out.println(bigInteger);
           
            System.out.println("Length is: "+bigInteger.toString().length());
   
    }

}

The output :::

933262154439441526816992388562667004907159682643816214
685929638952175999932299156089414639761565182862536979
20827223758251185210916864000000000000000000000000

Length is: 158
Read more ...

public,protected,private and default access modifiers in java simple example

12 October 2013

package package1;


public class A {
    protected int a=10;
    private int b=12;
    public int c=14;
    int d=16;
    public void print(){
        System.out.println("Package1 and access A class");
        System.out.println("Protected variable ="+ a);
        System.out.println("Private variable ="+ b);
        System.out.println("Public variable ="+ c);
        System.out.println("Default variable ="+ d);
       
        A aa=new A();
       
        System.out.println("Protected variable ="+ aa.a);
        System.out.println("Private variable ="+ aa.b);
        System.out.println("Public variable ="+ aa.c);
        System.out.println("Default variable ="+ aa.d);
       
    }

}
//...............................................................................//
package package1;

public class B extends A{
    public void print(){

        System.out.println("Package1 and access B class");
        System.out.println("Protected variable ="+ a);
        //System.out.println("Private variable ="+ b);//Look Private is not accessible in class B
        System.out.println("Public variable ="+ c);
        System.out.println("Default variable ="+ d);
      
        A aa=new A();
      
        System.out.println("Protected variable ="+ aa.a);
        //System.out.println("Private variable ="+ aa.b);// Same things here
        System.out.println("Public variable ="+ aa.c);
        System.out.println("Default variable ="+ aa.d);
    }

}

//.................................................................................//
package package1;

public class C {
    public void print(){
        A cc=new A();
        System.out.println("Package1 and access C class");
    //    System.out.println("Protected variable ="+ a);
        //System.out.println("Private variable ="+ b);
    //    System.out.println("Public variable ="+ c);
    //    System.out.println("Default variable ="+ d);
      
        //A aa=new A();
      
        System.out.println("Protected variable ="+ cc.a);
    //    System.out.println("Private variable ="+ aa.b);
        System.out.println("Public variable ="+ cc.c);
        System.out.println("Default variable ="+ cc.d);
    }
}

//...............................................................//
package Package2;

import package1.A;
public class AA extends A{

    public void print(){
        System.out.println("Package2 and access AA class");
        System.out.println("Protected variable ="+ a);
    //    System.out.println("Private variable ="+ b);
        System.out.println("Public variable ="+ c);
        //System.out.println("Default variable ="+ d);
      
        A aa=new A();
      
    //    System.out.println("Protected variable ="+ aa.a);
    //    System.out.println("Private variable ="+ aa.b);
        System.out.println("Public variable ="+ aa.c);
    //    System.out.println("Default variable ="+ aa.d);
      
    }

}

//...................................................................//
package Package2;

import package1.A;

public class BB {
    public void print(){
        System.out.println("Package2 and access BB class");
    //    System.out.println("Protected variable ="+ a);
    //    System.out.println("Private variable ="+ b);
    //    System.out.println("Public variable ="+ c);
    //    System.out.println("Default variable ="+ d);
    //  
        A aa=new A();
      
    //    System.out.println("Protected variable ="+ aa.a);
    //    System.out.println("Private variable ="+ aa.b);
        System.out.println("Public variable ="+ aa.c);
    //    System.out.println("Default variable ="+ aa.d);
      
    }

}
Read more ...

Breath-First_search(BFS) Source Code using Queue

29 July 2013

 Implementation BFS algorithm using C++
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
#define M 10000
vector<int> edges[M];
vector<int> cost[M];

void Bfs(int n,int source)
{
  queue<int>Q;
  Q.push(source);;
  int taken[100]={0},distance[100];
  taken[source]=1;
  distance[source]=0;
  while(!Q.empty())
  {

      int u=Q.front();
      int a=edges[u].size();
      for(int i=0;i<a;i++)
      {
          int v=edges[u][i];
          if(!taken[v])
          {

              distance[v]=distance[u]+1;
              taken[v]=1;
              Q.push(v);
          }
      }
      Q.pop();
  }
  for(int i=1;i<=n;i++)
  {

      printf("%d to %d distance %d\n",source,i,distance[i]);
  }

}



int main()
{
    int N,E,i;
    int esize,source;
    scanf("%d%d",&N,&E);//how many nodes and edges
    for(i=1;i<=E;i++)
       {
           int x,y;
           scanf("%d%d",&x,&y);
           edges[x].push_back(y);
           edges[y].push_back(x);
           cost[x].push_back(1);
           cost[y].push_back(1);
       }
printf("plz enter source node:");
scanf("%d",&source);
       Bfs(N,source);
 
       return 0;
}


Read more ...

Breath-First-Search(BFS) Source Code without using Queue :)

29 July 2013
 Implementation BFS algorithm using C++
#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
#define M 10000
vector<int> edges[M];
//vector<int> cost[M];                     // u can use it later

void Bfs(int n,int source)                  // number of nodes and source point(node)
{

    vector<int> v1,v2;             // declare 2 vector instead of queue
    v1.push_back(source);     
    int taken[100]={0};
    printf("Level 0 :: %d\n",source);
    for(int loop=1;loop<n;loop++)
    {

        printf("Level %d ::",loop);
        for(int i=0;i<v1.size();i++)
        {

            int u=v1[i];
            for(int j=0;j<edges[u].size();j++)
            {
                int v=edges[u][j];               // we found an edge from u to v
                if(!taken[v])                    // we can not go same node twice
                   { printf("%d ",v);          // just process
                    taken[v]=1;                // processed an edge and puts a tag
                    v2.push_back(v);         // where we found v1's node and push it v2
                }
            }
        }

        if(v2.empty())
        {puts("Empty");break;         // can not process any more
        }
        else{                                 // process again
            puts("");
            v1.clear();
            v1=v2;
            v2.clear();
        }
    }
}



int main()
{
    int N,E,i;
    int esize,source;                      // esize==edge size
    scanf("%d%d",&N,&E);       //how many nodes and edges
    for(i=1;i<=E;i++)
       {
           int x,y;
           scanf("%d%d",&x,&y);
           edges[x].push_back(y);
           edges[y].push_back(x);
          // cost[x].push_back(1);       // we can use it future
          // cost[y].push_back(1);        // we can use it future
       }
printf("plz enter source node:");
scanf("%d",&source);
       Bfs(N,source);
 
       return 0;
}
// i followed this code by shafayet's lecture....Thanks shafayet :)
Read more ...

Programming Quotes of all times

17 July 2013
  • "Copy and Paste is the Design Error."   -David Parnar
  • "First, solve the problem. Then Write the code."   -John Johson
  • " Programs must be written for people to read, and only incidentally for machines to execute. "-Ableson
Read more ...
12 July 2013
QuickSort.java


package Sorting;
import java.util.*;
public class QuickSort {
public static void main(String[] san){
    while(true){
    Scanner input=new Scanner(System.in);
    int num=input.nextInt();   // input how many numbers
    int[] a=new int[num+1]; 
    for(int i=1;i<=num;i++)
        a[i]=input.nextInt();     // array input
   
    quickSort(a,1,a.length-1);
   
   
    for(int i=1;i<=num;i++)
        System.out.printf("%d ",a[i]);
    }
}

public static void quickSort(int [] a,int p,int r){     //first of all p=pivot=1 and r= last element of the array A
    int q;
    if(p<r){
        q=Partition(a,p,r);
        quickSort(a,p,q-1);                // Left Sub Group
        quickSort(a,q+1,r);               // Right Sub Group
    }
}

public static int Partition(int [] a,int p,int r){
    int x,i,j,t;
    x=a[r];
    i=p-1;
    for(j=p;j<=r-1;j++){
        if(a[j]<=x)           //if A[j]>x   do nothing
        {
            i++;
            t=a[i];
            a[i]=a[j];
            a[j]=t;   
        }
    }
    t=a[i+1];
    a[i+1]=a[r];
    a[r]=t;
   
   
    return i+1;
}
}
Read more ...

QuickSort Algorithm

12 July 2013
According to the CLRS(Cormen, Leiserson, Rivest,    Stein) Book
  The PseudoCode of the QuickSort is as follows:

  QUICKSORT(A,p,r)   :: initial call is
   QUICKSORT(A,1,length[A])
         1. if p<r
         2.   then q=PARTITION(A,p,r)
         3.             QUICKSORT(A,p,q-1)
         4.             QUICKSORT(A,q+1,r)



Patitioning the array:

 The Key to the algorithm is the PARTITION procedure.Which rearranges the subarray A[p....r] in place.

The Pseudo Code of the PARTITION is as follows:

PARTITION(A,p,r)
    1. x=A[r]
    2. i=p-1
    3. for j=p to r-1
    4.       do if A[j]<=x
    5.                then i=i+1
    6.                       exchange A[i] and A[j]
    7. exchange A[i+1] and A[r]
    8. return i+1

The Source Code of the QuickSort.java
Read more ...

Divide and Conquer Algorithm: MergeSort

04 July 2013

import java.util.*;
public class MergeSort {
   
    public static void main(String[] shan){
        while(true){
        Scanner input=new Scanner(System.in);
        int n=input.nextInt();
        int[] arr=new int[n];
        for(int i=0;i<n;i++)
            arr[i]=input.nextInt();
       
        Merge_sort(arr,0,arr.length-1);
        for(int i=0;i<arr.length;i++)
            System.out.printf("%d ", arr[i]);
        }
    }
   
    public static void Merge_sort(int[] arr,int low,int high){
       
        if(low<high){   
        int mid=(low+high)/2;
        Merge_sort(arr,low,mid);
        Merge_sort(arr,mid+1,high);
       
        Merge(arr,low,mid,high);
       
        }
    }
   
    public static void Merge(int[] arr,int l,int mid,int h){
    int len=arr.length;
        int[] b=new int[len];
        for(int i=0;i<len;i++)
            b[i]=arr[i];
       
        int st=l;
        int Midtem=mid+1;
       
        while(st<=mid && Midtem<=h){
           
            if(b[st]>=b[Midtem])
                arr[l++]=b[Midtem++];
            else
                arr[l++]=b[st++];       
        }
        while(st<=mid){
            arr[l++]=b[st++];
        }
        while(Midtem<=h){
            arr[l++]=b[Midtem++];
        }
    }
}

Read more ...
20 June 2013
Tree Implementation Output
Output:
PreOrder
23
19
1
2
45
34
56
77
100
inOrder
1
2
19
23
34
45
56
77
100
postOrder
2
1
19
34
100
77
56
45
23
Read more ...

Tree Implementation Using Java

20 June 2013
package Data_structure;

class TNode{
    TNode left,right;
    int data;
    TNode(int value){
        data=value;
    }
}

public class TreeImplementation {
    TNode root;
    TreeImplementation(){
       
    }
    //insert Elements
    public void insert(int tdata){
        TNode newNode=new TNode(tdata);
        TNode current=root;
       
        if(root==null)root=newNode;
        else{
            while(true){
                if(current.data>tdata){
                    if(current.left==null){
                        current.left=newNode;
                        break;
                    }
                    current=current.left;
                }
               
                if(current.data<tdata){
                    if(current.right==null){
                        current.right=newNode;
                        break;
                    }
                    current=current.right;
                }
            }
        }
    }
   
   
   
    public void preOrder(TNode tem){
        if(tem!=null){
            System.out.println(tem.data);
            preOrder(tem.left);
            preOrder(tem.right);
        }
        else return;
    }

    public void inOrder(TNode tem){
        if(tem!=null){
            inOrder(tem.left);
            System.out.println(tem.data);
            inOrder(tem.right);
        }
        else return;
    }
   
    public void postOrder(TNode tem){
        if(tem!=null){
            postOrder(tem.left);
            postOrder(tem.right);
            System.out.println(tem.data);
        }
        else return;
    }
   
     public static void main(String [] shan){
         TreeImplementation bst=new TreeImplementation();
         bst.insert(23);
         bst.insert(19);
         bst.insert(1);
         bst.insert(45);
         bst.insert(56);
         bst.insert(34);
         bst.insert(2);
         bst.insert(77);
         bst.insert(100);
       
         System.out.println("PreOrder");
         bst.preOrder(bst.root);
       
         System.out.println("inOrder");
         bst.inOrder(bst.root);
       
         System.out.println("postOrder");
         bst.postOrder(bst.root);
     }
}
 
Input:
23 19 1 45 56 34 2 77 100

OutPut:
Read more ...

Simple Linked List Program Using Java

19 June 2013

import java.util.*;

class Node{
    int data;
    Node next;
    Node(int d){
        data=d;
        next=null;
    }
}

public class Linked_list {
  public static void main(String [] shan){
      Scanner input=new Scanner(System.in);
      int data,ch,size=0;
      Node head=null;
      Node tail=null;
     
      while(true){
          System.out.println("           1.Insert   2.Delete   3.Display  4.Exit  ");
          ch=input.nextInt();
          switch(ch){
          case 1:
          {
              System.out.println("Enter an Element.");
              data=input.nextInt();
              Node newNode=new Node(data);
             
              if(head==null){
                  head=newNode;
                  tail=head;
              }
              else{
                  tail.next=newNode;//Inserting Element
                  tail=newNode;
              }
              size++;
              System.out.println("Insertion SuccessFul :)");
              break;
          }
         
          case 2:
          {
              if(size>0){
              System.out.println(" If U want to delete something Enter element");
              data=input.nextInt();
              if(head.data==data)
                  head=head.next;
             
              else
              {
                  tail=head;
                  Node tem=head.next;
                  while(tem!=null){
                      if(tem.data==data){
                          tail.next=tem.next;
                          size--;
                          break;
                         
                      }
                      tail=tem;
                      tem=tem.next;
                  }
              }
              System.out.println("Ok..");
              }
             
              else
             
                  System.out.println("LinkedList is Empty");
              break;
          }
          case 3:
          {
              if(size>0){
                  System.out.println("Wait.......");
                  for(Node tem=head;tem!=null;tem=tem.next){
                      System.out.print(tem.data );
                      System.out.println();
                  }
              }
              else
                  System.out.println("LinkedList is Empty");
              break;
          }
          default:
              System.exit(0);
          }
      }
  }

}

Read more ...