Tuesday, November 19, 2013

Java - String Rotate

import java.io.*;


class StringRotate {   

    public static void main(String[] args) {

            String inpstring = "";
            InputStreamReader input = new InputStreamReader(System.in);
            BufferedReader reader = new BufferedReader(input);

            try
            {
                  System.out.println("Enter a string to rotate:");
                  inpstring = reader.readLine();

                  int len = inpstring.length();
                  int lastindex = len - 1;

                  char[] outstring = inpstring.toCharArray();


                  for (int i = 0; i < len; i++)
                  {
                        char ch = outstring[0];
                        for (int j = 0; j < len - 1; j++)
                        {
                              outstring[j] = outstring[j + 1];
                        }
                        outstring[len - 1] = ch;
                        {
                              for (int k = 0; k < outstring.length; k++)
                                    System.out.print(outstring[k]);
                              System.out.println();
                        }
                  }

                  System.out.println();
                  System.out.println();

                  for (int i = 0; i < len; i++)
                  {
                        char ch = outstring[len - 1];
                        for (int j = len - 1; j > 0; j--)
                        {
                              outstring[j] = outstring[j - 1];
                        }
                        outstring[0] = ch;
                        {
                              for (int k = 0; k < outstring.length; k++)
                                    System.out.print(outstring[k]);
                              System.out.println();
                        }
                  }
                 
            }
            catch (Exception e)
            {
                  e.printStackTrace();
            }
    }
}



Pascal Triangle

import java.io.*;
import java.lang.*;


class PascalTriangle { 

    public static void main(String[] args) {

            String inpstring = "";
            InputStreamReader input = new InputStreamReader(System.in);
            BufferedReader reader = new BufferedReader(input);

            try
            {
                  System.out.print("Enter number of rows for pascal triangle:");
                  inpstring = reader.readLine();
                  int n = Integer.parseInt(inpstring, 10);

                  for (int y = 0; y < n; y++)
                  {
                        int c = 1;

                        for(int q = 0; q < n - y; q++)
                        {
                              System.out.print("   ");
                        }

                        for(int x = 0; x <= y; x++)
                        {
                              System.out.print("   ");
                              System.out.print(c); // 3 digits
                              System.out.print(" ");
                              c = c * (y - x) / (x + 1);
                        }

                        System.out.println();
                        System.out.println();
                  }
                 
                  System.out.println();
            }
            catch (Exception e)
            {
                  e.printStackTrace();
            }
    }
}

OutPut:

Enter number of rows for pascal triangle:6
                     1

                  1    1

               1    2    1

            1    3    3    1

         1    4    6    4    1

      1    5    10    10    5    1

How to Calculate the Future Investment Value of Investment.

Below java program for calculate future investment value. Here calculate s using the formula futureInvestmentValue =investmentAmount x (...