Wednesday, October 29, 2014

Java Starting Pro



Flip.java

public class Flip 
{
              public static void main(String[] args)
{ 
        if (Math.random() < 0.5) 
        System.out.println("Heads");
        else                     
        System.out.println("Tails");
    }
}

TenHellos.java

 
public class TenHellos
{ 
 public static void main(String[] args) 
{
 
       System.out.println("1st Hello");
      System.out.println("2nd Hello");
      System.out.println("3rd Hello");
                              int i = 4;
                 while (i <= 10) 
     {
         System.out.println(i + "th Hello");
         i = i + 1;
      }
    }
}

PowersOfTwo.java

public class PowersOfTwo 
{
public static void main(String[] args) 
{
 
        int N = Integer.parseInt(args[0]);
        int i = 0;                
        int powerOfTwo = 1;       
                     while (i <= N) 
 {
            System.out.println(i + " " + powerOfTwo);   
            powerOfTwo = 2 * powerOfTwo;                
            i = i + 1;
        }
     }
}

DivisorPattern.java

public class DivisorPattern 
{
public static void main(String[] args) 
{ 
        int N = Integer.parseInt(args[0]);
                           for (int i = 1; i <= N; i++)
            {
            for (int j = 1; j <= N; j++) 
            {
                if (i % j == 0 || j % i == 0) 
            {
                    System.out.print("* "); 
                }
                else 
            {
                    System.out.print("  "); 
                }
            }
            System.out.println(i);
        }
    }
}

No comments:

Post a Comment