Monday, February 18, 2019

Diassarim number isc

Diassariun number detecting program
     

import java.math.*;
class Diassarium
{
public static int num,size;
public Diassarium(int h)
{
num=h;
size=0;
}
public static void countDigit()
{  
int n=num;
while(n>0)
{
size++;
n=n/10;
}

}
public static int sumofDigits(int n,int p)

   if(p==0)
   return 0;
    else
return ((int)Math.pow((n%10),p)+sumofDigits(n/10,p-1));
}
public static void check(int numcheck)
{  int n=sumofDigits(num,size);
if(n==numcheck)
System.out.println("Diassarium");
else
System.out.println("not Diassarium");
}
public static void main(String args[])
{
Diassarium obj=new Diassarium(135);
obj.countDigit();
obj.check(135);
Diassarium n=new Diassarium(124);
obj.countDigit();
obj.check(124);

}
}

Quick sort on array of integers

Quick sort is one of the most famous recursive technique available today.
       This short program effectively implement the principle of recursion to sort an array of integer

class p
{
public static int p,l,r;//p means pivot l left limit r denotes right
public static void qsort(int []a,int le,int re)
{
int l1=le;
int r1=re;
p=a[le];
while(le<re)
{
while((a[re]>=p)&&(le<re))
{
re--;
}
if(le!=re)
{
a[le]=a[re];
le++;
}
while((a[le]<=p)&&(le<re))
{
le++;
}
if(le!=re)
{
a[re]=a[le];
r--;
}
}
a[le]=p;
p=le;
le=l1;
re=r1;
if(le<p)
qsort(a,l,p-1);
if(re>p)
qsort(a,p+1,re);
}
public static void main(String args[])
{
int as[]={4,3,5,6,8,1,0};
qsort(as,0,6);
for(int i=0;i<7;i++)
System.out.println(i+" ");
}
}

Recursion decimal to binary

This program can convert any number to its binary equivalent.
class binar
{
public static void main(String args[])
{  
decitobin(2);
System.out.println();
decitobin(4);
System.out.println();
decitobin(8);
System.out.println();
decitobin(3);
}
public static void decitobin(int num)
{  
if(num>0)

decitobin(num/2);
}

System.out.print(num%2);
}
}

Thursday, February 14, 2019

Queue(array implementation)

This program will deal with queue.
A data structure based on FIFO principle which states that deletion will occur at front(where array starts 0) and insertion will occur at rear end(where array ends at index   length-1).
With every insertion rear becomes rear+1.
And with every deletion front becomes front+1.

class queue
{
public static int q[];// queue
public static int size;// length
public static int rear;
public static int front;
public queue(int n)
{
q=new int[n];
size=n;
front=rear=0;

}
public static void  enqueue(int num)
{  
if(rear!=size)
     {
q[rear]=num;
rear=rear+1;}
else
System.out.println("QUEUE FULL");

}
public static void dequeue()
{  
if(front!=rear){
System.out.println("removed "+(q[front]));
front=front+1;}
else {
System.out.println("UNDER FLOW");
}
}
public static void show()
{  System.out.println("queue is");
for(int i=front;i<rear;i++)
System.out.println(q[i]+"   ");
}
}
class que2
{
public static void main(String args[])
{
queue Queue1=new queue(5);//size of array
Queue1.enqueue(2);
Queue1.enqueue(3);
Queue1.enqueue(4);
Queue1.enqueue(8);
Queue1.enqueue(9);
Queue1.show();
Queue1.dequeue();
Queue1.show();


}
}

Base converaion through recursion

This program can convert a decimal number to its binary, hexadecimal or octal number.

import java.util.*;
class MAIN
{  public  static final char vb[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
   public static String st="";
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
System.out.println("give the string");
int N;
int y;
N=in.nextInt();
System.out.println("in hexadecimal");
base(N,16);
System.out.println("back conversion");
y=Integer.parseInt(st);

System.out.println("in binary");
base(N,2);
System.out.println("in octal");
base(N,8);

}
public static void base(int n,int b)
{
if(n>=b)
{
base(n/b,b);
}
System.out.print(vb[n%b]);
}
}

Tuesday, February 12, 2019

String frequency ISC PRACTICAL

In this question it was required to find the frequency of each word reappearing words should not be shown again.
Eg-hydra killed the soldier killed?
Input -frequency count-
   hydra-1
   killed-2
   the-1
   soldier-1
  This program is quite tough.
Solutions
I have taken two strings st-it reads string from user.st3-it store the words that are not repeating themselves.at the end of while loop the value of st3 collected is again stored in st .St  gets small every time.





The program-
import java.util.*;
class string
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
System.out.println("give the string");
int N,len;
String st;
String dupst,st1,st2,st3;
N=in.nextInt();
if((N<1)||(N>5))
System.out.println("INvVALID");
st=in.nextLine().trim();
dupst=st;
st1=st2=st3="";
len=st.length();
int lst1,lst2,len2;
int j,freq=0;
j=lst1=lst2=0;
for(int i=0;i<len;i++)
{  
if((st.charAt(i)==' ')||(st.charAt(i)=='?')||(st.charAt(i)=='!'))
{  
st1=st.substring(lst1,i);//extract words
lst1=i+1;
lst2=0;
j=0;
st2="";
len2=dupst.length();
while(j<len2)

if((dupst.charAt(j)==' ')||(dupst.charAt(j)=='?')||(dupst.charAt(j)=='!'))
{   
     st2=dupst.substring(lst2,j);
lst2=j+1;
if(st1.compareTo(st2)==0)
{
freq++;


}
else

st3=st3+" "+st2;
}
}

j++;
}

if(freq>0)
{
System.out.println("the frequency of "+st1+" is  "+freq);
freq=0;
}
dupst=st3.trim()+" ";
st3="";
len2=dupst.length();
}
}
}
}

Monday, February 11, 2019

Array non boundry elements(isc 2016)

Qns write a program in java to create an array A[][] of M*M.
1.display the original array
2.then sort the non boundary elements
3.print diagonal array
Eg
  1 2  3 4
  6 3 7 2
  1 2 9 0
  3 4 6 4

Output
1 2 3 4
6 2 3 2
1 7 9 0
3 4 6 4
  Diagnol
  1       4
      23
      79
   3      4
      

The program for this problem is
import java.util.*;
class boundry
    {
    public static void main(String args[])
   {
    Scanner in=new Scanner(System.in);
    System.out.println("enter the dimenaions");
    int M=in.nextInt();
    int A[][]=new int[M][M];
    int m=M*M-4*M+4;
    int a[]=new int[m];//to collect non boundry elements
   int x=0;
   for(int i=0;i<M;i++)
     {
       for(int j=0;j<M;j++)
       {
       A[i][j]=in.nextInt();//storing elements in array
       }
     }
    System.out.println("the original matrix");
  for(int i=0;i<M;i++)
  {
  for(int j=0;j<M;j++)
  System.out.print(" "+A[i][j]);
  System.out.println();
   }
   for(int i=1;i<M-1;i++)
   for(int j=1;j<M-1;j++)
   a[x++]=A[i][j];
   arraysort(a,m);// sortin the non boundry elements
   x=0;
  for(int i=1;i<M-1;i++)
  for(int j=1;j<M-1;j++)
  A[i][j]=a[x++];
    System.out.println("the sorted matrix");
  for(int i=0;i<M;i++)
  {
  for(int j=0;j<M;j++)
  System.out.print("  "+A[i][j]);
  System.out.println();
  }
   System.out.println("array in diagonal order");
    x=4;
  for(int i=0;i<M;i++)
    {    --x;
   for(int j=0;j<M;j++)

    if((i==j)||(j==x))
   System.out.print("  "+A[i][j]);
    else
    System.out.print("  "+"  ");
    System.out.println();
    }
   }
     public static void arraysort(int []a,int j)
   {
   int min,swap=0;
   for(int i=0;i<j;i++)
   {
   for(int k=0;k<j;k++)
  {
  if(a[i]<a[k])
  { 
  swap=a[i];
    a[i]=a[k];
  a[k]=swap;
  }}
}
}
}