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

Sunday, February 10, 2019

Circular prime isc 2016

Circular prime is a prime number that remains prime after changing its digits one by one
Eg-///input-131
  311 is a prime
  113 is a prime
So 131 is a circular prime
The program given

import java.util.*;
import java.math.*;
class circularprime
     {
     public static void main(String args[])
       {
       Scanner in=new Scanner(System.in);
      System.out.println("enter the numbers");
       int n=in.nextInt();
       int remainder,nodigit,dupn,check;
       double front,rear;
       dupn=n;
       nodigit=0;
       check=0;
       while(dupn>0)
      {
        nodigit++;
       dupn=dupn/10;
       }
        for(int i=0;i<nodigit;i++)
       {
        front=n/(Math.pow(10,nodigit-1));
       rear=n%(Math.pow(10,nodigit-1));
       n=(int)(rear*(Math.pow(10,1))+front);
       if(checkprime(n))
       check++;
       else
      {
      check=0;
      break;
      }
      }
     if(check==nodigit)
        {
         System.out.println("the number is circular           prime");
        }
        else
       System.out.println("no prime");
      }
    public static boolean checkprime(int n)
        {   int c=0;
       for(int i=1;i<=n;i++)
      if(n%i==0)
      c++;
     if(c==2)
     return true;
     else
     return false;
}
}

Isc practical 2018(banner problem trick)

Qns-design a program to input names of N teams display them in vertical order.it is required that the name of each team should have a space of 8 placed.
Values of N ___2<N<9.
Example names -
1.MARVEL
2.SYGNUS
3.POLLEN
Output-
     M    S   P
     A     Y  O
     R     G    L
     V     N   L
     E     U    E
     L     S    N

Program----//////
import java.util.*;
class banner
{
     public static void main(String args[])
    {
      Scanner sc=new Scanner(System.in);
      System.out.print("enter values");
      int N,i,stringplace,check;
       char st;
      stringplace=-1;
       check=0;
       boolean t=false;
       N=sc.nextInt();
      String banner[]=new String[N];
       if(N>9)
         {
      System.out.println("invalid input");
         }
     for(i=0;i<N;i++)// arrray input
       {
      banner[i]=sc.next();//enter the teams names
       }
    while(!t)//!t(equals true so it will works)
      { 
      ++stringplace;
       for(i=0;i<N;i++)
          {  
      try{st=banner[i].charAt(stringplace);
       System.out.print(""+st+"       ");
        }
  catch(Exception          StringIndexOutOfBoundsException)
    {
           check++;
  System.out.print("        ");
    }
    }
  if(check>=N)
t=true;
          System.out.println();
  check=0;
}

}
}

Saturday, February 9, 2019

To sort a double dimensional array(ISC 2018)

The program given here is the exact answer of the question that was asked in ISC practical 2018
question number 2.
Qns-create a double dimensional  Integer type A[][] array  of order M*N.
1.take the input from user
2.display the content of array
3.sort every row of the array
 
sorting technique that is used selection sort

import java.util.*;
class mat1
{
public static void main(String[] args)
{
System.out.println("Hello World!");

Scanner in = new Scanner(System.in);
     int M,N,i,j,x;
System.out.println("enter number of rows and columns");
M=in.nextInt();
N=in.nextInt();
int A[][]=new int[M][N];
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
A[i][j]=in.nextInt();
}

}
for(i=0;i<M;i++)// diplay matrix
{
for(j=0;j<N;j++)
{
System.out.print(" "+A[i][j]);
}
        System.out.println();
}
int min,swap;
for(i=0;i<M;i++)// to focus on one row at a time
{
for(j=0;j<N;j++)
{
min=j;//to make comparison
for(x=j;x<N;x++)
{
if(A[i][min]>A[i][x])
{
min=x;//finding the smallest number in each row
}

}
swap=A[i][j];
A[i][j]=A[i][min];
A[i][min]=swap;
}
}
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
System.out.print(" "+A[i][j]);
}
       System.out.println();
}
}}

ISC practical caesar cipher(2017)

ISC practicals solved
This question was asked in ISC practical 2017.
Caesar cipher is a encryption technique that is used to prevent the unauthorised use of data.
It is used to convert information into a secret code.it is implemented as ROT13(rotate by 13 places).
          eg Hello!How are you?
          /---- Uryyb? Ubj net lbh?

 import java.util.*;
import java.io.*;
public class encrypted
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
String st=in.nextLine();
String dup="";
char c=65;
int i,j,length,place,ass;
length=st.length();
if(length<3)
{
System.out.println("invalid lengtg");
}
else
{  i=0;
while(i<length)
{   c=st.charAt(i);
place=st.charAt(i);
if((place>=65)&&(place<=90))
{
place=place+13;
if(place>90)
{
ass=place-90;
place=65+ass-1;

}
c=(char)place;
}
else if((place>=97)&&(place<=122))
{
place=place+13;
if(place>122)
{
ass=place-122;
place=97+ass-1;

}
c=(char)place;
}
else if(c=='!'){
c=st.charAt(i);
place=c+13;
c=(char)place;}
dup=dup+c;

i++;}
}
System.out.println("the encrypted text is ="+dup);

}
}

Golbach number(2017)

A Goldbach number is one that can be expressed as the sum of two odd primes
All even number greater than 4 are Goldbach numbers
Example
6=3+3
3 and 3 are Goldbach pairs of 6
Here is the java program to find the Goldbach plairs between 9 and 50

import java.util.*;

public class Main
{
public static void main(String[] args)
{
System.out.println("Hello World!");

Scanner input = new Scanner(System.in);

System.out.print("Enter a number: ");
int n = input.nextInt();
    if((n>9)&&(n<50)&&(n%2==0))
{
for(int x=1;x<=n;x+=2)
{
for(int y=x;y<=n;y+=2)
{  if((x%2!=0)&&(y%2!=0))
if((f1(x))&&(f1(y)))

if((x+y)==n)
{
System.out.println("the numbers are"+x+"+"+y+"="+n);
}
}
}
}
else
{
System.out.println("invalid");
}

}
public static boolean f1(int t)
{  
    int s=0;
for(int x=1;x<=t;x++)
{
if(t%x==0)
s=s+1;
}
if(s==2)
return true;
else
return false;
}


}