Posts

Showing posts from January 17, 2014

Java program of common difference

import java.io.*;

class commondifference
{
public static void main(String args[]) throws Exception

{
int f=1,d=0,d2=0,n,r,r2;


BufferedReader br= new BufferedReader(new InputStreamReader(System.in));


System.out.println("ENTER a number");
n=Integer.parseInt(br.readLine());

do
{

r=n%10;

r2=(n/10)%10;

d=r-r2;

if(d2==0||d2==d)
d2=d;

else if (d2!=d)
 {
f=2;
break;

  }
n=n/10;

}while(n>9);



if(f==2)

System.out.println("No Common Difference");
else
System.out.println("Common difference is" + d2);
}
}

Java program for printing a pattern of java

import java.io.*;

class pattern
{
public static void main(String args[]) throws Exception

{
int n,i,j,s,t=-1;


BufferedReader br= new BufferedReader(new InputStreamReader(System.in));


System.out.println("ENTER a number");
n=Integer.parseInt(br.readLine());






for(i=n;i>=1;i--)
{

for(j=i;j>=1;j--)
System.out.print("*");


for(s=1;s<=t;s++)
System.out.print(" ");
t+=2;


j=i;

if(j==n)
j=j-1;

for(;j>=1;j--)
System.out.print("*");

System.out.println();

}


t=t-2;

for(i=2;i<=n;i++)
{

for(j=1;j<=i;j++)
System.out.print("*");

t-=2;

for(s=t;s>=1;s--)
System.out.print(" ");



j=i;

if(j==n)
j=j-1;

for(;j>=1;j--)
System.out.print("*");

System.out.println();

}




}
}

ENTER a number ********* ****  **** ***      *** **         ** *             * **         ** ***      *** ****  **** *********

Snakes and ladder game in java

import java.io.*;
class game
{
int pos,p,f;
String pname;
game(String name)
{
pname=name;
pos=0;
f=0;
}

public void play(int val)
 {
if(val==0)
val=1;

pos=pos+val;
System.out.println(pname+"\t is at \t"+ pos + " position");

f=0;
switch (pos)
  {
case 4:  pos+=10;  f=4; break;
case 9:  pos+=22; f=22; break;
case 17: pos-=17; f=-17; break;
case 19: pos+=19; f=19; break;
case 21: pos+=21; f=21; break;
case 28: pos+=56; f=56; break;
case 51: pos+=16; f=16; break;
case 54: pos-=20; f=-20; break;
case 62: pos-=43; f=-43; break;
case 64: pos-=4;  f=-4; break;
case 71: pos+=20; f=20; break;
case 80: pos+=20; f=20; break;
case 87: pos-=63; f=-63; break;
case 93: pos-=20; f=-20; break;
case 95: pos-=20; f=-20; break;
case 98: pos-=19; f=-19; break;
}
if(f>0)
System.out.println(pname+"\t has climbed a ladder and reached to\t"+ pos + " with "+f+ " points increment");
else if (f<0)
System.out.println(pname+"\t gotten bitten by a snake a…