>

Friday, November 9, 2012

Swapping Without Third Variable

import java.util.Scanner;
class SwappingNoThird
    {
        public static void  main(String san[])
    {
        int firstNumber,secondNumber;
        Scanner in=new Scanner(System.in);
   
        System.out.println("Enter the first number:");
        firstNumber=in.nextInt();
   
        System.out.println("Enter the second Number :");
        secondNumber=in.nextInt();
   
        System.out.println("Before Swapping two variables  FirstNumber : "  +firstNumber +" Second Number " +secondNumber);
       
        firstNumber=firstNumber+secondNumber;
        secondNumber=firstNumber-secondNumber;
        firstNumber=firstNumber-secondNumber;
       
   
        System.out.println("After  Swapping two variables  FirstNumber : "  +firstNumber +" Second Number " +secondNumber);
    }
    }

OUTPUT:

Enter the first number:
76
Enter the second Number :
67
Before Swapping two variables  FirstNumber : 76 Second Number 67
After  Swapping two variables  FirstNumber : 67 Second Number 76

Swapping Variables with Third Variable

import java.util.Scanner;
class SwappingWithThird
    {
        public static void  main(String san[])
    {
        int firstNumber,secondNumber,temp;
        Scanner in=new Scanner(System.in);// for receiving input from user
   
        System.out.println("Enter the first number:");
        firstNumber=in.nextInt();// get input one
   
        System.out.println("Enter the second Number :");
        secondNumber=in.nextInt(); // get input two
   
        System.out.println("Before Swapping two variables  FirstNumber : "  +firstNumber +" Second Number " +secondNumber);
   
        temp=firstNumber;
        firstNumber=secondNumber;
        secondNumber=firstNumber;
   
        System.out.println("After  Swapping two variables  FirstNumber : "  +firstNumber +" Second Number " +secondNumber);
    }
    }


OUTPUT:

Enter the first number:
6
Enter the second Number :
7
Before Swapping two variables  FirstNumber : 6 Second Number 7
After  Swapping two variables  FirstNumber : 7 Second Number 7

Thread Example

class ThreadExample{
  public static void main(String args[])
   {
        Thread th=new Thread();
      try{
          for(int i=0;i<10;i++)
    {
                System.out.println(" Value "+i);
     th.sleep(2000);
              }
          }
           catch(Exception e)
    {
     System.out.println(" "+e.getMessage());
    }
    }
         }

OUTPUT:

This program will print and pause for one second until next print.

 Value 0
 Value 1
 Value 2
 Value 3
 Value 4
 Value 5
 Value 6
 Value 7
 Value 8
 Value 9

Getting User Name of System

public class UserName
    {
        public static void main(String args[])
    {
        String version=System.getProperty("user.name");
        System.out.println("Currently logged in as "+version);
    }
    }

OUTPUT:

Currently logged in as K SARAVANAN

Ten Multiplicatioin Table

package basics;

public class TenMultiplicationTable {

    public static void main(String[] args) {

        for(int i=1;i<=10;i++)
        {
            for(int j=1;j<=10;j++)
            {
                System.out.print(" "+i*j);
            }
            System.out.println("");
        }
    }

}

Output:

 1 2 3 4 5 6 7 8 9 10
 2 4 6 8 10 12 14 16 18 20
 3 6 9 12 15 18 21 24 27 30
 4 8 12 16 20 24 28 32 36 40
 5 10 15 20 25 30 35 40 45 50
 6 12 18 24 30 36 42 48 54 60
 7 14 21 28 35 42 49 56 63 70
 8 16 24 32 40 48 56 64 72 80
 9 18 27 36 45 54 63 72 81 90
 10 20 30 40 50 60 70 80 90 100

Thursday, November 8, 2012

Checking Whether Last Two Numbers Are same or not


import java.util.Scanner;

public class LoneTeen {
public static void loneteen(int a,int b)
    {
   
    if(((a<13)||(a>19)&&(b<=13)||(b>=19))||((b<13)||(b>19)&&(a<=13)&&(a>=19)))
       
                System.out.println(true);
       
        else
            System.out.println(false);
     
        //THIS PROGRAM IS 90 % only correct by k.saravanan
           
    }
    public static void main(String args[])
    {
        Scanner in=new Scanner(System.in);
        System.out.println("enter two integers :");
        int i=in.nextInt();
        int j=in.nextInt();
        loneteen(i,j);
    }

Finding Largest Among Three Numbers



import java.util.Scanner;

public class LargestAmongThree {
public static void bigThree(int a,int b,int c)

    {
        if((a>b)&&(a>c))
        {
            System.out.println(a+" is biggest");
        }
        else if((b>c)&&(b>a))
        {
            System.out.println(b+" is biggest");
        }
        else
            System.out.println(c+" is biggest");
    }
public static void main(String args[])
    {
    System.out.println("enter three integers");
    Scanner in=new Scanner(System.in);
    int i,j,k;
    i=in.nextInt();
    j=in.nextInt();
    k=in.nextInt();
    bigThree(i,j,k);
    }
}