Posts

Showing posts from November, 2013

Java - String Rotate

import java.io.*;

class StringRotate {   
publicstaticvoid main(String[] args) {
String inpstring = ""; InputStreamReader input = newInputStreamReader(System.in); BufferedReader reader = newBufferedReader(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++)                   {

Pascal Triangle

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

class PascalTriangle { 
publicstaticvoid main(String[] args) {
String inpstring = ""; InputStreamReader input = newInputStreamReader(System.in); BufferedReader reader = newBufferedReader(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(); }