Monday, February 13, 2012

Console based Playing Cards Trick In Java

 import java.io.*;
 class card
 {
    static char[]Suits=new char[21];
    static char[]Number=new char[21];
    static char[][]StacksS=new char[3][7];
    static char[][]StacksN=new char[3][7];
    public static void ReAdjust(int a, int b)
    {
         for(int i=0; i<=6; i++)
         {
             char A=StacksS[a][i];
             StacksS[a][i]=StacksS[b][i];
             StacksS[b][i]=A;
             A=StacksN[a][i];
             StacksN[a][i]=StacksN[b][i];
             StacksN[b][i]=A;
           }
       }
       public static void D2Stacks()
       {
           int k=0;
           for(int i=0; i<=6; i++)
           {
               for(int j=0; j<3; j++)
               {
                   StacksS[j][i]=Suits[k];
                   StacksN[j][i]=Number[k++];
               }
           }
       }
           public static void D2Pile()
           {
           int k=0;
           for(int i=0; i<3; i++)
           {
               for(int j=0; j<7; j++)
               {
                   Suits[k]=StacksS[i][j];
                   Number[k++]=StacksN[i][j];
               }
           }
       }
            public static void Display(int a)
           {
               for(int i=0; i<=6; i++)
               {
                   for(int j=0; j<3; j++)
                  {
                      for(int v=0; v<=888888; v++)
                      System.out.print("");
                      System.out.print(""+StacksS[j][i]+""+StacksN[j][i]+"   ");
                   }

               }
               System.out.println();
               System.out.println("(Please note: C=Club, H=Heart, D=Diamond, S=Spade, T=10)");
           }
           public static void Display()
           {
               System.out.println("Stack-1 Stack-2 Stack-3");
               for(int i=0; i<=6; i++)
               {
                   for(int a=0; a<=888888; a++)
                      System.out.print("");
                   for(int j=0; j<3; j++)
                  {
                      System.out.print(""+StacksS[j][i]+""+StacksN[j][i]+"   ");
                   }
                   System.out.println("");
               }
           }
          public static void main(String args[])throws IOException
          {
              BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
              char[][]A=new char[4][13];
              char[]B={'H', 'S', 'D', 'C'};
              char[]C={'A',50,51,52,53,54,55,56,57,'T','J','Q','K'};
              for(int i=0; i<=3; i++)
              for(int j=0; j<=12; j++)
              A[i][j]=C[j];
              int q, b, c=0;
              for(c=0; c<=20; c++)
              {
                  do{
                  q=(int)(Math.random()*4);
                  b=(int)(Math.random()*13);
               }while(A[q][b]==' ');
               Suits[c]=B[q];
               Number[c]=A[q][b];
               A[q][b]=' ';
           }
           D2Stacks();
           System.out.println("Select a card from the following cards in your mind: ");
           for(int a=0; a<=888888; a++)
                      System.out.print("");
           Display(1);
           System.out.println("\n\nNow press enter: ");
           String S=br.readLine();
           D2Pile();
           D2Stacks();
           System.out.println("\n\nSelect the stack in which your card lies this time around: ");
           Display();
           System.out.println("\n\nNow type the stack number and press enter: ");
           int a=Integer.parseInt(br.readLine());
           if(a!=2)
           ReAdjust(1, a-1);
           for(int p=0; p<=888888; p++)
                      System.out.print("");
           D2Pile();
           D2Stacks();
           System.out.println("\n\nSelect the stack in which your card lies for the last time now: ");
           Display();
           System.out.println("\n\nNow type the stack number and press enter: ");
           a=Integer.parseInt(br.readLine());
           if(a!=2)
           ReAdjust(1, a-1);
           for(int p=0; p<=888888; p++)
                      System.out.print("");
           D2Pile();
           D2Stacks();
           System.out.println("\n\nSelect the stack in which your card lies: ");
           Display();
           System.out.println("\n\nNow type the stack number and press enter: ");
           a=Integer.parseInt(br.readLine());
           if(a!=2)
           ReAdjust(1, a-1);
           for(int p=0; p<=888888; p++)
                      System.out.print("");
           System.out.print("You selected .");
           for(int i=1; i<=399999999; i++)
           if(i%122222222==0)
           System.out.print(" .");
                      System.out.print(" "+StacksS[1][3]+""+StacksN[1][3]);
       }
   }

Friday, February 10, 2012

Java program to find the divisibility by 2 without using mod(%)

/**to find divisibility by 2 without using "%"
*/
public class Shivam
{
public static boolean check(int a)
{
if(a/2.0-a/2==0)
return true;
return false;

}
}

Recursive function in java to print all natural numbers from 1 upto (n-1)

// recursive func to print a series of natural nos. upto (x-1)
import java.io.*;
public class RecFUNCseries
{
public static void main(String args[]) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter an integer: ");
int x=Integer.parseInt(br.readLine());
series(x);
}
public static void series(int x)
{
if(x>0)
series(--x);
if(x!=0)
System.out.print(x+" ");
}
}

Java programs to find the coprimes of a number

// coprimes of any number n are numbers bw 1 and n, which are not divisible by any factors of n
public class Coprimes
{

public static void main(String args[])
{
int sum=0;
int p=0;
int y;
for(int i=18; i<=23; i++)
{
sum=0;
p=0;
int[]a=new int[i/2];
for(int j=2; j<=i/2; j++)
{
if(i%j==0 && IsPrime(j)==true)
a[p++]=j;
}

System.out.println("For "+i+", the co-primes are:");
for(int x=1; x {
for(y=0; y {
if(x%a[y]==0)
break;
}
if(y==p)
{
sum+=x;
System.out.println(x);
}
}
System.out.println("Their sum is "+sum);
System.out.println("");
System.out.println("");
}
}

public static boolean IsPrime(int a)
{
a=Math.abs(a);
for(int i=2; i<=a/2; i++)
if(a%i==0)
return false;
return true;
}
}

Java Program to print the initials of a name with last name written in full

public class Initials
{
public static void initials(String S)
{
int[]A=new int[S.length()];
int sp=0;
for(int i=0; i if(S.charAt(i)==' ')
A[sp++]=i;

if(sp==0)
System.out.println(S);
else
{

System.out.print(S.charAt(0)+". ");

for(int i=0; i
System.out.print(S.charAt(A[i]+1)+". ");


System.out.print(S.substring(S.lastIndexOf(' ')+1, S.length()));
}
}
}

Java program To remove duplicates from an array of integers so that each number may appear only once

import java.io.*;
class remDup
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the length of the array: ");
int l=Integer.parseInt(br.readLine());
int[]a=new int[l];
for(int i=0; i {
System.out.print("Enter the element number "+(i+1)+" of the array: ");
a[i]=Integer.parseInt(br.readLine());
}

//now the real work begins

for(int i=0; i {
for(int j=i+1; j {
while(a[i]==a[j])
{
for(int A=j; A a[A]=a[A+1];
l--;
}
}
}

// real work is done now!!!!


for(int i=0; i {
System.out.println(a[i]);
}

}// end of main
}// end of class

Console based two-player Cross and Noughts game in Java

import java.io.*;
public class CrossZero
{
static char[][] A=new char[3][3];




public static void main(String args[])throws IOException

{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Player 1: X");
System.out.println("Player 2: O");
for(int i=0; i<=2; i++) for(int j=0; j<=2; j++) A[i][j]='*'; Print(); for(int i=1; i<=9; i++) { System.out.println("Player "+((i%2)==1?1:2)); System.out.println("Enter Row Number: "); int r=Integer.parseInt(br.readLine()); System.out.println("Enter Column Number: "); int c=Integer.parseInt(br.readLine()); if(i%2==1) A[r][c]='X'; else A[r][c]='O'; Print(); if(i>4)
{
if(check('X')==true||check('O')==true)
{
System.out.println("Congrats, You've Won");
break;
}
if(i==9)
System.out.println("Sorry dudes! No one won..");
}

}
}

public static void Print()
{
System.out.print(" ");
for(int i=0; i<=2; i++)
System.out.print(" "+i+" ");
System.out.println("");
System.out.println("");
for(int i=0; i<=2; i++)
{ for(int j=0; j<=2; j++)
if(j==0)
System.out.print(i+" "+A[i][j]);
else
System.out.print(" "+A[i][j]);
System.out.println("");
System.out.println("");


}
System.out.println("");

System.out.println("");
System.out.println("");

}

public static boolean check(char a)
{
for(int i=0; i<=2; i++)
{
if(A[i][0]==a&&A[i][1]==a&&A[i][2]==a)
return true;

if(A[0][i]==a&&A[1][i]==a&&A[2][i]==a)
return true;


}

if(A[0][0]==a&&A[1][1]==a&&A[2][2]==a)
return true;

if(A[0][2]==a&&A[1][1]==a&&A[2][0]==a)
return true;
return false;
}
}

java program To print a Name in big letters/ To convert a name into large Letters

The following is a code that will convert the Input name into Large Letters of approximately 5cm x 5cm.
Before compiling the code, save the text as a text file named MyFile.txt.
click here to download the text file

And give the ADDRESS of MyFile.txt in the required line in the code.
Have Fun...

import java.io.*;
class better1
{
public static void main(String args[]) throws IOException
{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a name:");
String S=br.readLine();
S=S.toLowerCase();
String D[][]=new String[26][8];
FileReader a=new FileReader("MyFile.txt");//MyFile sent as an attachment
BufferedReader b=new BufferedReader(a);

for(int i=0; i<26;i++)
for(int j=0; j<8;j++)
D[i][j]=b.readLine();

for(int i=0; i<=7; i++)
{
for(int j=0; j{
char ch=S.charAt(j);
int c=ch-97;
if(ch==' ')
System.out.print(" ");
else
System.out.print(D[c][i]+" ");
}
System.out.println("");
}
b.close();
a.close();
}
}

Wednesday, February 8, 2012

java program To find the coprimes of a number

// coprimes of any number n are numbers bw 1 and n, which are not divisible by any factors of n
public class Coprimes
{

public static void main(String args[])
{
int sum=0;
int p=0;
int y;
for(int i=18; i<=23; i++)
{
sum=0;
p=0;
int[]a=new int[i/2];
for(int j=2; j<=i/2; j++)
{
if(i%j==0 && IsPrime(j)==true)
a[p++]=j;
}

System.out.println("For "+i+", the co-primes are:");
for(int x=1; x{
for(y=0; y{
if(x%a[y]==0)
break;
}
if(y==p)
{
sum+=x;
System.out.println(x);
}
}
System.out.println("Their sum is "+sum);
System.out.println("");
System.out.println("");
}
}

public static boolean IsPrime(int a)
{
a=Math.abs(a);
for(int i=2; i<=a/2; i++)
if(a%i==0)
return false;
return true;
}
}

java program: Recursive function to multiply two numbers

// to multiply 2 nos. using recursive functions
import java.io.*;
class recursiveMultiplication
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a number: ");
String s=br.readLine();
System.out.print("Enter another number: ");
String S=br.readLine();
int a=Integer.parseInt(s);
int b=Integer.parseInt(S);
if((a<0&&b>0)||(a>0&&b<0))
System.out.print(-Mult(Math.abs(a),Math.abs(b)));
else System.out.print(Mult(Math.abs(a),Math.abs(b)));
}
public static int Mult(int a, int b)
{
if(a==0||b==0)
return 0;
else if(b==1)
return a;

return(a+Mult(a,(b-1)));
}

}

java program To print a String in reverse order using recursion

// to print a string in reverse order using recursion
import java.io.*;
class reverseString
{
public static void main(String args[])throws IOException
{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter a String: ");

String s=br.readLine();
reverse(s, s.length());
}

public static void reverse(String S, int a)throws IOException
{
System.out.print(S.charAt(--a));
if(a>0)
reverse(S,a);
}
}