>

Friday, November 9, 2012

Java Program without Semicolon

 class Program
{
public static void main( String[] args )
{
 if( System.out.printf( "India not poor country%n" )== null ){
 }
}}

OUTPUT:
India not poor country

Factorial Using While loop in java

import java.util.Scanner;

class FactorialWhile{

 public static void main(String args[]){

    int fact=1,num,i=1;

    System.out.println("Enter the number:");

   Scanner in=new Scanner(System.in);

   num=in.nextInt();

  while(i<=num){

    fact=fact*i;

    i++;
}

  System.out.println("factorial of given number is "+fact);
}  }


OUTPUT:

Enter the number:
4
factorial of given number is 24

String Reverse Program in Java Wihtout Reverse Function

import java.util.Scanner;

public class StringReverse {
public static void stringReverse(String name)
{
    String str=name;
    char c[]=str.toCharArray();
        System.out.println("Reversed String : ");
    for(int i=str.length();i>0;i--)
    {
        System.out.print(c[i-1]);

    }
       

       
        }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in=new Scanner(System.in);
        System.out.println("enter your string:");
        String input=in.nextLine();
        System.out.println("Given String Value :"+ input);
        stringReverse(input);
    }

}


OUTPUT:
enter your string:
Kanimozhi
Given String Value :Kanimozhi
Reversed String :
ihzominaK

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