Posts

Number To Word In Java

//NUMBER TO WORD class Number2Word{     private String a1[]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fiteen","Sixteen","Seventeen","Eighteen","Nineteen"};     private String a2[]={"Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety"};     public void display(int n){         if(n<=19)         f1(n);         else if(n<=99)         f2(n);         else         f3(n);     }     public void f1(int n){         System.out.println(a1[n]);     }     public void f2(int n){         int x,y;         x=n/10;         y=n%10;         if(y==0)         System.out.println(a2[x-2]);...

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         {         ...