Tuesday 5 February 2013

multiplication table in java


import java.util.Scanner;

class MultiplicationTable
{
   public static void main(String args[])
   {
      int number, a;
      System.out.println("which table you want");
      Scanner in = new Scanner(System.in);
      number = in.nextInt();
      System.out.println("Multiplication table of "+number+" is :-");

      for ( a = 1 ; a <= 10 ; a++ )
         System.out.println(number+"*"+a+" = "+(number*a));
   }
}


Output: 
  
                 

which table you want
4
Multiplication table of 4 is :-
4*1 = 4
4*2 = 8
4*3 = 12
4*4 = 16
4*5 = 20
4*6 = 24
4*7 = 28
4*8 = 32
4*9 = 36
4*10 = 40

Reverse a string in java


public class ReverseString {

      public static void main(String[] args) {
            String word = "Hai this is karthik";
            word = new StringBuffer(word).reverse().toString();
            System.out.println(word);

      }

}



Output:

             kihtrak si siht iaH





import java.util.*;

class ReverseString
{
   public static void main(String args[])
   {
      String reverse ="";
      System.out.println("Enter the String");
      Scanner scan = new Scanner(System.in);
      String  original = scan.nextLine();

      int length = original.length();

      for ( int i = length - 1 ; i >= 0 ; i-- )
         reverse = reverse + original.charAt(i);

      System.out.println("Reverse string is: "+reverse);
   }
}


Output:

Enter the String
hai this is karthik
Reverse string is: kihtrak si siht iah


Using StringTokenizer word by word 



   import java.util.StringTokenizer;

public class ReverseLine {

      public static void main(String[] args) {

            String word = "Java Reverse String";
            StringTokenizer st = new StringTokenizer(word, " ");
            String stringReversedLine = "";

            while (st.hasMoreTokens()) {
                  stringReversedLine = st.nextToken() + " " + stringReversedLine;
            }

            System.out.println("Reversed string by word :" + stringReversedLine);
      }
}

Output:

                   Reversed string is : String Reverse Java 


     







Saturday 2 February 2013

swapping two numbers in java


import java.util.Scanner;

public class SwapElementsWithoutThirdVariableExample {

      public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            System.out.println("Enter the value of a ");
            int a = scan.nextInt();
            System.out.println("Enter the value of b");
            int b = scan.nextInt();

            System.out.println("Before Swapping");
            System.out.println("Value of a is :" + a);
            System.out.println("Value of b is :" + b);

            a = a + b;
            b = a - b;
            a = a - b;

            System.out.println("After Swapping");
            System.out.println("Value of a is :" + a);
            System.out.println("Value of  b is :" + b);
      }

}
Output:
Enter the value of a
100
Enter the value of b
200
Before Swapping
Value of a is :100
Value of b is :200
After Swapping
Value of a is :200
Value of  b is :100

factorial program in java


import java.util.Scanner;

public class Factorial {

      public static void main(String[] args) {
        System.out.println("Enter the number");
            Scanner scan = new Scanner(System.in);
            int number = scan.nextInt();

            int factorial = number;
            for (int i = 1; i < number; i++) {
                  factorial = factorial * i;
            }
            System.out.println("Factorial of a "+number+" is"+" "+ factorial);
      }
}
Output:
Enter the number
5
Factorial of a 5 is 120