Sunday, 18 September 2016

Hamming Code: N-bit

Program for N-bit Hamming Code:
import java.util.*;
class hamming{
  public static void main(String sap[]){
    System.out.print("\nNOTE: Example - Format for 7-bit Hamming Code - P1 P2 D3 P4 D5 D6 D7");
    int i,j,k;
    Scanner sc = new Scanner(System.in);

    /*taking no of data bits...*/
    System.out.print("\nEnter no of bits: ");
    int no_of_data_bits = sc.nextInt();

    /*creating parity positions array as per no of data bits...here parity_pos[] array size would be no_of_data_bits and indexing would be from 0 to (no_of_data_bits - 1)...*/
    int parity_pos[] = new int[no_of_data_bits];
    int no_of_parity_bits=0;
    while(no_of_data_bits>Math.pow(2,no_of_parity_bits)){
      parity_pos[no_of_parity_bits] = (int)Math.pow(2,no_of_parity_bits);
      no_of_parity_bits++;
    }
    parity_pos[no_of_parity_bits] = (int)Math.pow(2,no_of_parity_bits);
    no_of_parity_bits++;

    /*creating a sending array of size (no_of_data_bits + no_of_parity_bits + 1)...here send_arr[] indexing would be from 1 to ((no_of_data_bits + no_of_parity_bits + 1) - 1)...index '0' of send_arr[] is not used...*/
    int send_arr[] = new int[no_of_data_bits+no_of_parity_bits+1];

    /*creating a variable 'k' which would be used to bypass the parity positions in send_arr[] by referring parity_pos[]...*/
    k=0;
    System.out.print("\nEnter "+no_of_data_bits+" Data Bits: \n");
    for(j=1;j<send_arr.length;j++){
      if(parity_pos[k]==j){
        k++;
      }
      else{
        /*taking data_bits and storing in send_arr[]...*/
        send_arr[j] = sc.nextInt();
      }
    }

    int result,distance,count;
    /*creating a parity value array as per no_of_data_bits...here indexing of parity_value[] would be from 0 to (no_of_data_bits - 1)...*/
    int parity_value[] = new int[no_of_data_bits];

    /*here variable 'i' would take care of no of parity values to be generated...*/
    i=0;
    while(i<parity_pos.length && parity_pos[i]!=0){
      /*result variable would contain the parity value generated...*/
      result = 0;

      /*here distance and count variable would contain the distance...
      distance would be like...suppose distance = 2 then we would take first 2 values from send_arr[] and skip next 2 values from send_arr[] and repeat the procedure till the end of the send_arr[]...*/
      distance=count=parity_pos[i];

      /*j variable would take care of generating parity as per distance in the send_arr[]...*/
      j=1;
      while(j<send_arr.length){
        /*for this while loop the values of send_arr[] would be considered for generating parity value...and we are not taking care of parity bits as initially parity bits in send_arr[] would be '0'...*/
        while(count<=distance && count>0 && j<send_arr.length){
          result = result + send_arr[j];
          count--;
          j++;
        }

        /*for this while loop the values of send_arr[] would not be responsible for generating parity value...*/
        while(count<distance){
          count++;
          j++;
        }
      }

      /*at the end of every iteration of above while loop...parity value would be generated...and value of parity would be stored in parity_value[]...*/
      parity_value[i] = result%2;
      i++;
    }

    /*merging the send_arr[] and parity_value[]...*/
    j=0;
    for(i=1;i<send_arr.length;i++){
      /*as per parity_pos[] array the values would be inserted respectively in send_arr[]...*/
      if(i==parity_pos[j]){
        send_arr[i] = parity_value[j];
        j++;
      }
    }

    /*Displaying the data to be transmitted...*/
    System.out.print("\nData to be transmitted: ");
    for(i=1;i<send_arr.length;i++){
      System.out.print(send_arr[i]);
    }
    System.out.println();
  }
}

Output:

NOTE: Example - Format for 7-bit Hamming Code - P1 P2 D3 P4 D5 D6 D7
Enter no of bits: 8

Enter 8 Data Bits:
1
0
0
1
1
0
1
0

Data to be transmitted: 011100101010

Saturday, 30 July 2016

Super ASCII String Checker

Problem Statement: 

In the Byteland country a string "S" is said to super ascii string if and only if count of each character in the string is equal to its ascii value.

In the Byteland country ascii code of 'a' is 1, 'b' is 2 ...'z' is 26.

For eg1 - String "bba" is super ascii string
Explanation:- .String "bba" 
The count of character 'b' is 2. Ascii value of 'b' is also 2.
The count of character 'a' is 1. Ascii value of 'a' is also 1.
Hence string "bba" is super ascii string.

For eg2 - String "acc" is not super ascii string
Explanation:- String "acc"
The count of character 'a' is 1. Ascii value of 'a' is also 1
The count of character 'c' is 2. But the ascii value of 'c' is 3
Hence string "acc" is not super ascii string.

Program: 

import java.util.*;
class PS
{
  public static void main(String sap[])
  {
    int i,j;
    Scanner sc = new Scanner(System.in);

    //taking input string
    System.out.print("\nEnter String: ");
    String str = sc.next();

    //character array for retriving the value of respective character
    char char_arr[] = new char[27];

    //loading the character array
    int char_count=97;
    for(i=1;i<27;i++)
    {
      char_arr[i] = (char)(char_count++);
    }

    //it would act like a hash table
    int result_arr[] = new int[27];

    //initially loading result array with 0
    for(i=1;i<27;i++)
      result_arr[i]=0;

    //loading result array i.e hash table
    for(i=0;i<str.length();i++)
    {
      for(j=1;j<27;j++)
      {
        if(char_arr[j]==str.charAt(i))
        {
          int temp = result_arr[j];
          result_arr[j]=temp+1;
          break;
        }
      }
    }

    //comparing result array with its index if yes then accept else break
    for(i=1;i<27;i++)
    {
      if(result_arr[i]>0)
      {
        if(result_arr[i]!=i)
        {
            break;
        }
      }
    }

    //if value of 'i' would be less then 27 then string is rejected...
    if(i<27)
      System.out.print("\n"+str+" not a super ascii string...");
    else
      System.out.print("\n"+str+" is super ascii string...");
  }
}

Output:

1. Enter String: bab
    bab is super ascii string...

2. Enter String: ccdddd
    ccdddd not a super ascii string...

Tuesday, 21 June 2016

Problem Statement - II


Problem Statement:-

                              N x N board is given for 'N' queen. We want to place 'N' queen on the board in non-attacking position.

Note: Queen can attack horizontally, vertically and diagonally.

For this use 1-D array-X where Kth element of array-X will give column of Kth queen.

Example:











Constraint on array-X:
1. Every value stored in array-X should be unique
2. Every value stored in array-X should be between 1 to 'N'


Program:
#include<stdio.h>
#include<math.h>
#define MAXSIZE 10

int x[MAXSIZE];
int n;

void printsolution()
{
    int i, k;
    static int solno=1;

    printf("\nSolution %d: ", solno++);
    for(i=1;i<=n;i++)
    {
        printf("%d ",x[i]);
    }
}

int place(int k, int i)
{
    int j;

    for(j=1; j<k; j++)
    {
        if( (x[j] == i) || (abs(x[j]-i) == abs(j-k)) )
            return 0;
    }
    return 1;
}

void nqueen(int k)
{
    int i;

    for(i=1; i<=n; i++)
    {
        if(place(k,i))
        {
            x[k] = i;
            if(k == n)
                printsolution();
            else
                nqueen(k+1);
        }
    }
}

void main()
{
    printf("\nEnter board size: ");
    scanf("%d",&n);
    if(n>MAXSIZE)
    {
        exit(1);
    }
    nqueen(1);
}

Output:

Enter board size: 4

Solution 1: 2 4 1 3
Solution 2: 3 1 4 2

Monday, 29 February 2016

Problem Statement - I

Problem Statement:
              You have been given a gold prices for n days. You Need to find out max benefit in best time to buy and sell.

import java.util.*;
class Demo
{
  public static void main(String s[])
  {
  Scanner sc = new Scanner(System.in);
       int day[]=new int[100];
        int i,j,res,a=0,b=0,fres=0;
       System.out.println("\nEnter no of days: ");
  int n=sc.nextInt();

  System.out.print("\nEnter "+n+" gold prices: ");
  for(i=0;i<n;i++)
  {
    day[i]=sc.nextInt();
  }
  for(i=0;i<n;i++)
  {
    for(j=i;j<n;j++)
    {
    res=day[j]-day[i];
    System.out.print("Purchase Day: "+(i+1)+" Sell Day: "+(j+1)+" Profit: "+res);
    if(res>=fres)
    {
      a=i;
      b=j;
      fres=res;
    }
    System.out.println();
    }
  }

    System.out.print("\nBest Purchase:\nPurchase Day: "+(a+1)+" Sell Day: "+(b+1)+" Profit: "+fres);
  }
}

Output:
Enter no of days:
5

Enter 5 gold prices: 
100
60
400
500
30

Purchase Day: 1 Sell Day: 1 Profit: 0
Purchase Day: 1 Sell Day: 2 Profit: -40
Purchase Day: 1 Sell Day: 3 Profit: 300
Purchase Day: 1 Sell Day: 4 Profit: 400
Purchase Day: 1 Sell Day: 5 Profit: -70
Purchase Day: 2 Sell Day: 2 Profit: 0
Purchase Day: 2 Sell Day: 3 Profit: 340
Purchase Day: 2 Sell Day: 4 Profit: 440
Purchase Day: 2 Sell Day: 5 Profit: -30
Purchase Day: 3 Sell Day: 3 Profit: 0
Purchase Day: 3 Sell Day: 4 Profit: 100
Purchase Day: 3 Sell Day: 5 Profit: -370
Purchase Day: 4 Sell Day: 4 Profit: 0
Purchase Day: 4 Sell Day: 5 Profit: -470
Purchase Day: 5 Sell Day: 5 Profit: 0

Best Purchase:
Purchase Day: 2 Sell Day: 4 Profit: 440

Saturday, 23 January 2016

PatternDemo in Java - II


class PatternDemo
{
public static void main(String s[])
{
int n = 5;
for(int i=1;i<=n;i++)
{
for(int j=0;j<i;j++)
{
System.out.print((i+j)%2+"\t");
}
System.out.print("\n");
}
}
}

Output:
1
0       1
1       0       1
0       1       0       1
1       0       1       0       1



class PatternDemo
{
public static void main(String s[])
{
int n = 4,k=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<=i;j++)
{
System.out.print((k++)+"\t");
}
System.out.print("\n");
}
}
}

Output:
0
1       2
3       4       5
6       7       8       9

Tuesday, 22 December 2015

PatternDemo in Java - I

import java.util.*;
class PatternDemo
{
    public static void main(String args[])
    {
        char ch='A';
     
        Scanner sc = new Scanner(System.in);
        System.out.print("\nEnter value for n: ");
        int n = sc.nextInt();
     
        for(int i=1;i<=n;i++)
        {
           for(int j=n;j>=i;j--)
           {
              System.out.print(" ");
           }
             
           for(int k=1;k<=i;k++)
           {
               System.out.print(ch++);
           }
           ch--;
         
           for(int m=1;m<i;m++)
           {
              System.out.print(--ch);
           }
           System.out.print("\n");
        }
    }
}
Output2:
Enter value for n: 5
           A
        ABA
      ABCBA
    ABCDCBA
 ABCDEDCBA



import java.util.*;
class PatternDemo
{
    public static void main(String args[])
    {
        char ch='A';

        Scanner sc = new Scanner(System.in);
        System.out.print("\nEnter value for n: ");
        int n = sc.nextInt();
     
        for(int i=0;i<=(n-1);i++)
        {
            for(int j=0;j<=i;j++)
            {
               System.out.print(ch++);
            }
            System.out.print("\n");
        }
    }
}

Output1:
Enter value for n: 4
A
BC
DEF
GHIJ

Tuesday, 13 October 2015

Program For Array Of Object


import java.util.*;
class Student
{
    String name;
    int id,p,c,m,total;
 
    void accept()
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("\nEnter name: ");
        name = sc.next();

        System.out.print("Enter ID: ");
        id = sc.nextInt();

        System.out.print("Enter Physics marks: ");
        p = sc.nextInt();

        System.out.print("Enter Chemistry marks: ");
        c = sc.nextInt();

        System.out.print("Enter Maths marks: ");
        m = sc.nextInt();

        total = p+c+m;
    }

    void display()
    {
        System.out.print("\nName:"+name+"   ID:"+id+"   Physics:"+p+"  Chemistry:"+c+"   Maths:"+m+"   Total:"+total);      
    }
 
    static void sort_marks(Student s[])
    {
        int n = s.length;
        Student temp = new Student();

        for(int i=1; i<n; i++)
        {
            for(int j=0; j<(n-i); j++)
            {
                if(s[j].total<s[j+1].total)
                {
                    temp =s[j];
                    s[j] =s[j+1];
                    s[j+1]=temp;
                }
            }
        }
    }
 
    static void sort_name(Student s[])
    {
        int n = s.length;
        Student temp = new Student();

        for(int i=1; i<n; i++)
        {
            for(int j=0; j<(n-i); j++)
            {
                if(s[j].name.compareTo(s[j+1].name)>0)
                {
                    temp =s[j];
                    s[j] =s[j+1];
                    s[j+1]=temp;
                }
            }
        }
    }
}

class ArrayObjectDemo
{
    public static void main(String soham[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("\nEnter number of students: ");
        int n = sc.nextInt();

        Student s[]=new Student[n];
       
        for(int i=0; i<=(n-1); i++)
        {
System.out.print("\nEnter Details for Student "+(i+1));
            s[i] = new Student();
            s[i].accept();
        }
     
        //Sorted according to total marks
        Student.sort_marks(s);
        System.out.print("\nSorted according to total marks: ");
        for(int i=0; i<=(n-1); i++)
        {
            s[i].display();
        }
        System.out.println();

        //Sorted according to name
        Student.sort_name(s);
        System.out.print("\nSorted according to name: ");
        for(int i=0; i<=(n-1); i++)
        {
            s[i].display();
        }
        System.out.println();
    }
}

Output: 
Enter number of students: 3

Enter Details for Student 1
Enter name: ABC
Enter ID: 101
Enter Physics marks: 90
Enter Chemistry marks: 70
Enter Maths marks: 80

Enter Details for Student 2
Enter name: XYZ
Enter ID: 102
Enter Physics marks: 80
Enter Chemistry marks: 70
Enter Maths marks: 80

Enter Details for Student 3
Enter name: PQR
Enter ID: 103
Enter Physics marks: 90
Enter Chemistry marks: 70
Enter Maths marks: 60

Sorted according to total marks:
Name:ABC   ID:101   Physics:90   Chemistry:70   Maths:80   Total:240
Name:XYZ   ID:102   Physics:80   Chemistry:70   Maths:80   Total:230
Name:PQR   ID:103   Physics:90   Chemistry:70   Maths:60   Total:220

Sorted according to name:
Name:ABC   ID:101   Physics:90   Chemistry:70   Maths:80   Total:240
Name:PQR   ID:103   Physics:90   Chemistry:70   Maths:60   Total:220
Name:XYZ   ID:102   Physics:80   Chemistry:70   Maths:80   Total:230

Monday, 12 October 2015

Program For Tribonacci Series


import java.util.*;
import java.io.*;
class Tribonacci
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number till u want Tribonacci series: ");
        int n=sc.nextInt();

        int a = 0,b = 0,c = 1;
        int d = a+b+c;

        System.out.println("\nTribonacci Series: ");
        System.out.print(a+"\t"+b+"\t"+c);

        for(int i=4;i<=n;i++)
        {
            System.out.print("\t"+d);
            a=b;
            b=c;
            c=d;
            d=a+b+c;
        }

        System.out.println();
    }
}
Output:
Enter number till u want Tribonacci series: 10

Tribonacci Series:
0       0       1       1       2       4       7       13      24      44

Wednesday, 23 September 2015

Program For Pythagorean Triplet


/* 
    Pythagorean Triplet:  
        c*c = a*a + b*b 
*/  
  
public class PythagoreanTriplet  
{  
    public static void main(String s[]) throws Exception  
    {  
        System.out.println("Pythagorean Triplet: ");  
        for(int a=1; a<=50 ; a++)  
        {  
            for(int b=1; b<=50 ; b++)  
            {  
                int csquared = a*a + b*b;  
                double croot = Math.sqrt(csquared);  
  
                if( croot==Math.ceil(croot) )  
                {  
                    System.out.println(a + " " + b + " " + (int)croot);  
                }  
            }  
        }  
    }  
}  
Output:
Pythagorean Triplet:
3 4 5
4 3 5
6 8 10
8 6 10

Monday, 8 June 2015

Program to implement Binary search

//Program to implement Binary Search
import java.io.*;
public class Binary
{
  public static void main(String args[]) throws Exception
  {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    int n, item, i, j, mid, top, bottom;
    int a[] = new int[100];

    System.out.print("Enter number of elements: ");
    n = Integer.parseInt(br.readLine());

    System.out.println("Enter "+n+" elements in ascending order: ");
    for (i = 0; i <= (n-1); i++) 
    {
      a[i]=Integer.parseInt(br.readLine());
    }

    System.out.print("\nEnter the element to search: ");
    item=Integer.parseInt(br.readLine());

    bottom = 0;
    top = n; 
    do 
    {
      mid = (bottom + top) / 2;
      if (item < a[mid])
      {
        top = mid - 1;
      }
      else if (item > a[mid])
      {
        bottom = mid + 1;
      }
    }while(item != a[mid] && bottom <= top);
     
    if (item == a[mid]) 
    {
      System.out.print("\n"+item+" is found at position: "+(mid+1));
    } 
    else 
    {
      System.out.print("\n"+item+" is not found");
    }
    System.out.println();
  }
}
//Program to implement Binary search
import java.io.*;  
import java.util.*;
public class Binary  
{  
  public static void main(String args[]) throws Exception  
  {  
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
        
    int n, item, i;    
        
    System.out.print("Enter number of elements: ");  
    n = Integer.parseInt(br.readLine());  

    int a[] = new int[n];
        
    System.out.println("Enter "+n+" elements: ");  
    for (i = 0; i <= (n-1); i++)   
    {  
      a[i]=Integer.parseInt(br.readLine());  
    }  
       
    Arrays.sort(a);
    System.out.println("\nThe sorted list:");
    for (i=0; i<=(a.length - 1); i++) 
    {
      System.out.println(a[i]+"\t");
    }
        
    System.out.print("\nEnter the element to search: ");  
    item=Integer.parseInt(br.readLine());  

    int retVal = Arrays.binarySearch(a,item);   
    System.out.println("The index of element "+item+": "+(retVal+1));

    System.out.println();  
  }  
}    
Output:
Enter number of elements: 5
Enter 5 elements:
3
5
9
1
2

The sorted list:
1
2
3
5
9

Enter the element to search: 5
The index of element 5: 4