Posts

Showing posts from April, 2016

Recursive program on Tower of Hanoi

Recursive program on Tower of Hanoi. import java.util.*; class TowerHRec {     public static void main(String args[])     {         int n;         char beg='A',aux='B',end='C';         Scanner sc=new Scanner(System.in);         System.out.println("Enter number of disks:");         n=sc.nextInt();         TowerHRec obj=new TowerHRec();         obj.move(n,beg,aux,end);     }     void move(int n,char beg,char aux,char end)     {         if(n==1)           System.out.println("Move "+n+" disks from "+beg+" -> "+end);                   else         {             move(n-1,beg,end,aux);             System.out.printl...

Queue Implement Array

Queue data structure using Array. import java.util.*; class arrayQueue {     protected int Queue[] ;     protected int front, rear, size, len;     //Constructor     public arrayQueue(int n)     {         size = n;         len = 0;         Queue = new int[size];         front = -1;         rear = -1;     }       //Function to check if queue is empty     public boolean isEmpty()     {         return front == -1;     }       //Function to check if queue is full     public boolean isFull()     {         return front==0 && rear == size -1 ;     }       //Function to get the size of the queue     public int getSize()     {    ...

Implement Stack data structure using Array.

Implement Stack data structure using Array. import java.util.*; class Stack1 {     int s[],max,top;     public static void main()     {         int ch,size;         Scanner sc =new Scanner(System.in);         System.out.println("Enter the size of the stack");         size=sc.nextInt();         Stack1 obj=new Stack1();         obj.assign(size);         while(true)         {             System.out.println("1:PUSH\n2:POP\n3:DISPLAY\n4:EXIT");             System.out.println("Enter your choice");             ch=sc.nextInt();             switch(ch)             {                 case 1: System.out.pr...

Evil Number In Java

Write a Program in Java to input a number and display the Evil Numbers from 1 to 50. import java.util.*; class EvilNumber {     String toBinary(int n) // Function to convert a number to Binary     {         int r;         String s="";         char dig[]={'0','1'};         while(n>0)             {                 r=n%2; //finding remainder by dividing the number by 2                 s=dig[r]+s;                 n=n/2;             }         return s;     }//toBinary         int countOne(String s)     {         int c = 0, l = s.length();         char ch;         for(int...

Coordinates And Point In Java

Write a menu driven program to perform the following using objects of the same class: i)distance of a given point from the origin ii)distance between two given points iii)coordinates of the midpoint of a line joining two points import java .util.*; class Point { double x,y;   public Point()   {x=0;    y=0;   }   public Point(double a,double b)   {x=a;    y=b;   }   public double distance(Point ob)   {double d;    d=Math.sqrt((Math.pow((this.x-ob.x),2))+(Math.pow((this.y-ob.y),2)));     return d;   }//end of distance()    public double disfromOrigin()   {double dis;    dis=Math.sqrt(x*x+y*y);      return dis;   }    public Point mid(Point ob)   {Point m;    m=new Point();    m.x=(ob.x+this.x)/2;    m.y=(ob.y+this.y)/2;    return m;   }//end of mid()   public stat...

Form Palindrome In Java

Take two positive integer of two digits or more, reverse the digits, and add to the original number. This is the operation of the reverse-then-add-sequence. Now repeat the procedure with the sum so obtained until a palindromic number is obtained. import java.util.*; class Reverse_sequence { public static boolean isPalindrome(int n)   { int d,r=0,p=n;    while(p!=0)    { d=p%10;      r=r*10+d;      p=p/10;    }     if (r==n)      return true;     else     return false;  }//end of fucntion isPalindrome  public static int reverse(int n)  { int m=n,d=0;    while(m!=0)    { d=d*10+(m%10);      m=m/10;    }    return d;  }  public static void main()  {  int n,s;     Scanner sc= new Scanner (System.in);     System.out.println("Enter a no.");   ...

Complex Number Operations In Java

To design a class Complex to represent operations on Complex numbers,ie. Addition, subtraction, modulus, conjugate, multiplication, division using objects of the same class. import java.util.*; class Complex { double r,i;     public Complex()     {r=0;      i=0;     }     public Complex(double a,double b)     {r=a;      i=b;     }     public double mod()     { return Math.sqrt(r*r+i*i);}     public Complex conju()     {Complex ob= new Complex();      ob.r=this.r;      ob.i=-this.i;      return ob;     }     public Complex sum(Complex ob)    { Complex xy= new Complex();       xy.r=this.r+ob.r;       xy.i=this.i+ob.i;       return xy;    }//end of sum()     public Complex dif(Complex ob)   ...

Mobius function In Java

Write a program in Java to find the value of Mobius function of a user input number. import java.util.*; class MobiusFn {     int n;         MobiusFn()     {         n = 0;     }         void input()     {         Scanner sc = new Scanner(System.in);           System.out.print("Enter a number : ");         n = sc.nextInt();     }         /*  The function primefac() either returns '0' if prime factors are repeated      *  or returns the no.of prime factors */     int primeFac()     {         int a=n, i=2, m=0, c=0, f=0;                     while(a > 1) // loop to generate prime factors         {           ...

Fascinating Number In Java

Write a program to input a number and check whether it is a Fascinating Number or not      within the range 100 to 3000. import java.util.*; class FascinatingNumber {     boolean isUnique(String q)     {         int A[] = {0,0,0,0,0,0,0,0,0,0}; //to store frequency of every digit from '0' to '9'         int i, flag = 0;         char ch;         for(i=0; i<q.length(); i++)         {             ch = q.charAt(i);             A[ch-48]++;         }         for(i=1; i<10; i++)         {             if(A[i]!=1)             {                 flag = 1; //flag is set to 1 if frequency is not 1       ...

Amicable Number In Java

Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number. Write a program in Java to find out the given two numbers are amicable or not. class amicable {  static int a,b;  static void input(int m,int n)  {      a=m;      b=n;      display();     }//input     static boolean check()     {         int s=0,i;         for(i=1;i<a;i++)         {             if(a%i==0)             {                 s=s+i;             }         }         if(s==b)         {             s=0;             for(i=1;i<b;i++) ...

Anagram in Java

Write a Program in Java to input a word and print its anagrams. import java.util.*; class Anagrams {     int c = 0;         void input()     {         Scanner sc = new Scanner(System.in);         System.out.print("Enter a word : ");         String s = sc.next();         System.out.println("The Anagrams are : ");         display("",s);         System.out.println("Total Number of Anagrams = "+c);     }//input       void display(String s1, String s2)     {         if(s2.length()<=1)         {             c++;             System.out.println(s1+s2);         }         else         {         ...

Consecutive letters in a sentence in Java

Write a program to print the consecutive letters in a sentence. Both input and output      are considered in uppercase. import java.util.*; class Consecutive{     public static void main(){         Scanner sc=new Scanner(System.in);         String str,s="";         int i,l,j;         System.out.println("Enter a sentence");         str=sc.nextLine();         str=str.toUpperCase();         l=str.length();         for(i=65;i<=90;i++){             for(j=0;j<l;j++){                 if(str.charAt(j)==i){                     s=s+str.charAt(j);                     break;               ...

Infix To Postfix In Java Using Stack

25--- Write a program to convert a given infix expression to postfix expression using stack. import java.util.*; import java.util.*; class Infix2Postfix {  char s[]=new char[100];     int top=-1;     void push(char ch){         top=top+1;         s[top]=ch;     }     char pop(){         char ch;         ch=s[top];         top=top-1;         return(ch);     }     int pre(char ch){         switch(ch){             case'^':return 3;             case'*':return 2;             case'/':return 2;             case'+':return 1;             case'-':return 1;         }         return 0; ...

Hit the nail on the head program in Java

24---Write a program to read a sentence which terminates with a full stop(.).The words are to          be sperated by a single blank space and are in lower case.Arrange the words in ascending          order according to the length of the words.If 2 words are of same length then the word          occuring first in the input sentence should come first.For for input and output the          sentence must begin with   upper case.          SAMPLE INPUT : Hit the nail on the head.          SAMPLE OUTPUT : On hit the the nail head. import java.util.*; class LengthWise{     public static void main()     {Scanner sc=new Scanner(System.in);         String s,t,x="",y;         int i,l;         System.out.println("Enter a String");       ...

Date difference in Java

23---WAP to perform date difference.Accept 2 dates in  the format dd/mm/yy.          check the dates are valid or not and find difference between 2 dates.          SAMPLE INPUT         Date 1:20/12/2012                               Date 2:11/02/2013          SAMPLE OUTPUT     Difference=54 days import java.util.*; class Date_Difference {     Scanner sc=new Scanner(System.in); int month[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; //function for checking for Leap Year int isLeap(int y)     {         if((y%400==0) || ((y%100!=0)&&(y%4==0)))             return 29;         else             return 28;     } //function for checking date validation boole...

Wap to input an integer array of order m*n and arrange them along the ROW

22---Wap to input an integer array of order m*n and arrange them along the         ROW in a way given below.         SAMPLE INPUT                                           SAMPLE OUTPUT         3 2 4 1 5                                                       5 3 1 2 4         4 5 7 9 0                                                       9 5 0 4 7 import java.util.*; class Pendulum_Array {     public static void main()     {         Scanner sc=new Scanner(System.in);       ...

Union of 2 sets,A and B(2)Intersection of 2 sets,A and B.(3)Show that operation A-B and B-A are always false for sets.

21---Program to perform.         (1)Union of 2 sets,A and B.          (2)Intersection of 2 sets,A and B.          (3)Show that operation A-B and B-A are always false for sets. import java.util.*; class U_I_M {    private int l1,l2;    public void intersection(int a[],int b[]){        int i,j,k=0,l3;        l1=a.length;        l2=b.length;        l3=Math.min(l1,l2);        int c[]=new int[l3];        for(i=0;i<l1;i++){            for(j=0;j<l2;j++){                if(a[i]==b[j]){                    c[k++]=a[i];                    break;               ...

Insertion sort technique upon strings In Java ISC

Program to perform Insertion sort technique upon strings. class Insertstring //start of class { public void accept (String str[]) //start of method { String key=" ";int j=0; int n=str.length; for( int i=1;i<n;i++) { key= str [i]; j=i-1; while ( j>=0 && key.compareTo(str[j])<0){ str[j+1]=str[j]; j--; } str[j+1]=key; } System.out.println("The output in ascending order"); for (int i=0;i<n;i++) { System.out.println(str[i]); } }//end of method }//end of class OUTPUT--- The output in ascending order ANIK IS LOVE

Recursive Series In Java (1) 1/2+(1+1)/2^2+(1+1+2)/2^3+......n terms.(2) 1!/X^2-2!/X^3+......n terms.

Menu driven recursive program to perform.          (1)  1/2+(1+1)/2^2+(1+1+2)/2^3+......n terms.          (2) 1!/X^2-2!/X^3+......n terms. class Recursion{      public double s1(int N){          if(N==0)          return 0;          else          return(fibo(N)/Math.pow(2,N)+s1(N-1));         }//s1         public double s2(int N,int x){             if(N==0)             return 0;             else if(N%2==1)             return((fact(N)/Math.pow(x,N+1))+s2(N-1,x));             else             return((fact(N)/Math.pow(x,N+1))+s2(N-1,x));         }//s2     ...

Recursive program to extract digits and print them in words.

Recursive program to extract digits and print them in words. class digword //start of class { public void accept ( int num ) { System.out.println(" The number is "+ "\t\t"+ num); int p= revdig( num); } public int revdig ( int k) { String a[]={"zero","one","two","three","four","five","six","seven","eight","nine"}; if (k==0){ return 0; } else { int r=k%10; System.out.println("the number"+"\t\t"+r+"\t\t"+ "in words"+"\t" +a[r]); } return revdig(k/10); }// end of method }// end of class OUTPUT--- The number is 2442441 the number 1 in words one the number 4 in words four the number 4 in words four the number 2 in words two the number 4 in words four the number 4 in words four the number 2 in words two

Menu driven recursive program to perform.(1)Calculate Factorial.(2)sum of digits of a multidigit number.(3)value of x^n.(4)multiplication of 2 numbers.

Menu driven recursive program to perform.         (1)Calculate Factorial.         (2)sum of digits of a multidigit number.          (3)value of x^n.          (4)multiplication of 2 numbers. import java.util.*; class Menudriven{     public static void main(){         Scanner sc=new Scanner(System.in);         System.out.println("Enter 1 for factorial");         System.out.println("Enter 2 for sum of digits of a multididgit number");         System.out.println("Enter 3 for value of x^n");         System.out.println("Enter 4 for multiplication of 2 numbers");         int a,b,c;         System.out.println("Enter your choice");         c=sc.nextInt();         switch(c){         ...

Menu driven recursive(1)Fibonacci Series.(2)Tribonacci Series.

Menu driven recursive program to perform.         (1)Fibonacci Series.         (2)Tribonacci Series. import java.util.*; class Recursion_fibtribseries{ public void accept(){ Scanner sc=new Scanner(System.in); System.out.println("Enter your choice: Press 1 for fibonacci series & press 2 for tribonacci series"); int n=sc.nextInt(); switch(n) { case 1 : System.out.println("Enter the number of terms"); System.out.println("The fibonacci series"); int num = sc.nextInt(); for (int i=1;i<=num;i++){ int fib= fibo (i); System.out.println(fib+ "\t\t"); } break; case 2: System.out.println("Enter the number of terms"); System.out.println("The tribonacci series"); int num1 = sc.nextInt(); for (int i=1;i<=num1;i++){ int trib= tribo (i); System.out.println(trib+ "\t\t"); } break; default : System.out.println(" Wrong Choice "); }} int fibo ( int i){ if (i==0) return -1; else if ( ...

Recursive program to calculate Greatest Common Divisor Of 2 programs.

Recursive program to calculate Greatest Common Divisor Of 2 programs. class GCD{ public void accept(int n1,int n2){ int big=(n1>n2)?n1:n2; int small=(n1<n2)?n1:n2; int gcd=Grcodi(big,small); System.out.print("The greatest common divisor"+"\t\t"+gcd); } int Grcodi(int p,int q){ if(q==0) return p; return Grcodi(q,p%q); }//accept }//End of class OUTPUT--- When Input is 3 & 2 The greatest common divisor 1

Recursive program to perform the Binary search technique.

Recursive program to perform the Binary search technique. import java.util.*; class BS{     static int a[];     public void check(int lb,int ub,int s){         if(lb>ub)         System.out.println("NOT FOUND");         else         {             int m=(lb+ub)/2;             if(a[m]==s)             System.out.println(s+" Found at "+(m+1)+"th position");             else             if(s>a[m])             check(m+1,ub,s);             else             check(lb,m-1,s);         }     }     public static void main(){         Scanner sc=new Scanner(System.in);    ...

Menu Driven recursive program to.(1)calculate reverse of a string.(2)calculate reverse of a number.

Menu Driven recursive program to.          (1)calculate reverse of a string.          (2)calculate reverse of a number. import java.util.*; class Reverse{     public String rev(String s){         if(s.length()==1)             return s;         else             return(rev(s.substring(1))+s.charAt(0));     }  public static void main(){         Scanner sc=new Scanner(System.in);         System.out.println("Enter 1 for reversing a string or 2 for reversing a number");         int c=sc.nextInt();         sc.nextLine();         Reverse r=new Reverse();         switch(c){             case 1:System.out.println("Enter a String");        ...

Recursive program to convert a decimal number to any base.

Recursive program to convert a decimal number to any base. import java.util.*; class dectoanyrec{     public String anyB(int n,int b){         if(n<b)         return n<=9?""+n:""+(char)(n+55);         else         return anyB(n/b,b)+(n%b<=9?""+n%b:(char)(n%b+55));     }//anyB     public static void main(){          Scanner sc=new Scanner(System.in);         System.out.println("Enter any decimal number");         int n=sc.nextInt();         System.out.println("Enter the base");         int b=sc.nextInt();         dectoanyrec DR=new dectoanyrec();         System.out.println("The number in the base  "+b+"  Is  "+DR.anyB(n,b));     }//main }//class  OUTPUT--- Enter any decimal numb...

Symmetric Matrix In Java ISC

11---Program to declare a square matrix A[ ] [ ] of order (M x M) such that M must be greater than 2 and less than 10 and check whether it is a Symmetric matrix or not. class Symmetric{ //start of class public void accept(int as[][]){ int r=as.length; int c=as[0].length;int d=0; if(c!=r|| c<2||c>10){ System.out.println("Not a Symmetric matrix"); System.exit(1); } for(int i=0;i<r;i++){ for(int j=0;j<c;j++){ if(as[i][j]!=as[j][i]) d++; }} if(d==0) System.out.println("The Given matix is Symmetric"); else System.out.println("not a symmetric matrix"); }}//end of class OUTPUT--- WHEN INPUT IS {{1,2,3},{5,6,7}} OUTPUT IS Not a Symmetric matrix

Program to fill a square matrix of size ‘n*n” in a circular fashion (clockwise) with natural numbers from 1 to n*n, taking ‘n’ as input.

Program to fill a square matrix of size ‘n*n” in a circular fashion (clockwise) with natural numbers from 1 to     n*n, taking ‘n’ as input. import java.util.*; class Circular_Matrix     {      //start of class         public static void main()         {             Scanner sc=new Scanner(System.in);             System.out.print("Enter the number of elements : ");             int n=sc.nextInt();             int A[][]=new int[n][n];             int k=1, c1=0, c2=n-1, r1=0, r2=n-1;             while(k<=n*n)                 {                     for(int i=c1;i<=c2;i++)                 ...

Saddle Point Matrix In Java ISC

Program to find the SADDLE POINT for the matrix. class Saddlepoint{ //start of class public void accept(int a[][]){ int n=0; int r=a.length; int c=a[0].length; for(int i=0;i<r;i++){ int min=a[i][0]; int p=0; for(int j=0;j<c;j++){ if(a[i][j]<min){ min=a[i][j]; p=j; }} int max=a[0][p]; for(int k=0;k<r;k++){ if(a[k][p]>max){ max=a[k][p];  n=k; }} if(n==i){ System.out.println("The  Saddle point "+a[i][p]); break; }} }}//end of class OUTPUT---WHEN INPUT IS {{1,2,2},{2,3,5}} The  Saddle point 2

Upper Triangular Matrix In Java ISC

Program to input a 2-D square matrix and check whether it is a Upper Triangular Matrix or not. import java.util.*; class UpperTriangularMatrix {     public static void main()     {         Scanner sc=new Scanner(System.in);         System.out.print("Enter the size of the matrix : ");         int m=sc.nextInt();         int A[][]=new int[m][m];         for(int i=0;i<m;i++)         {             for(int j=0;j<m;j++)             {                 System.out.print("Enter an element : ");                 A[i][j]=sc.nextInt();             }         }         System.out.println("The Matrix is : ");       ...