Thursday, 30 August 2012

a closer look at methods & class in java



AUTHOR
//SOME PIRAMID EXAMPLE IN JAVA
package com.swain.cell;

public class Piramid2 {

public static void main(String args[]){
int i,j;
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
System.out.print(" "+j);
System.out.print("\n");
}
}
}
//PIRAMID3 
package com.swain.cell;

public class Piramid3 {

public static void main(String args[]){
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
System.out.print(" "+j);
System.out.print("\n");
}
for(i=4;i>=1;i--)
{
for(j=1;j<=i;j++)
System.out.print(" "+j);
System.out.print("\n");
}
}
}
//PIRAMID4
package com.swain.cell;

public class Piramid4 {

public static void main(String args[]){
int i,k,j;
for(i=1;i<=200;i= i*2){

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

for(k=i/2; k>=1; k=k/2)
System.out.print( k + " ");
System.out.print("\n");
}
}
}
//PRIME NO IN JAVA
package com.swain.cell;

public class Prime {
public static void main(String args[])
  {
     int n, status = 1, num = 3;
n=5;
     if ( n >= 1 )
     {
        System.out.println("prime numbers are: ");
        System.out.println(2);
     }
 
     for ( int count = 2 ; count <=n ;  )
     {
        for ( int j = 2 ; j <= Math.sqrt(num) ; j++ )
        {
           if ( num%j == 0 )
           {
              status = 0;
              break;
           }
        }
        if ( status != 0 )
        {
           System.out.println(num);
           count++;
        }
        status = 1;
        num++;
     }         
  }

}
//STRING PALIDROM IN JAVA
package com.swain.cell;

public class StringPalindrome {
public static void main(String args[])
  {
     String original="1001";
     String rev="";
           
     int length = original.length();
 
     for ( int i = length - 1 ; i >= 0 ; i-- )
        rev = rev + original.charAt(i);
 
     if (original.equals(rev))
        System.out.println(original+" is a palindrome.");
     else
        System.out.println(original+" is not a palindrome.");
 
  }
}

No comments:

Post a Comment