I wrote a code in Java to generate a Pascal's Triangle. I have two laptops at home, one with Windows Vista Home Basic and the other with Windows 8.1 SingleLanguage, both having the same version of java loaded. The below mentioned code works perfectly fine in Win Vista but gives a logical error when I try to run the same code on Win 8.1. What could be a possible reason for this?
This is the draft of the code that I compiled lately :
import java.util.*;
class PascalsTriangle
{
int ROW,max=0;
void main ()
{
Scanner sc = new Scanner ( System.in );
System.out.println(" Input the number of rows in the Pascals Triangle to be generated. ");
ROW = sc.nextInt();
int[][] pascal = new int [ROW+1][];
pascal[1] = new int [1+2];
pascal[1][1] = 1;
int i,k=0,j,len=0;
String str="";
for( i=2;i<=ROW;i++ )
{
pascal[i] = new int [i+2];
for( j=i;j<pascal[i].length-1;j++ )
{
pascal[i][j] = pascal[i-1][j-1] + pascal[i-1][j];
str = Integer.toString(pascal[i][j]);
len = str.length();
if( len>max )
{
max = len;
}
}
}
for( i=1;i<=ROW;i++ )
{
for( k=ROW;k>i;k-- )
{
System.out.format( "%-" + max + "s", " " );
}
for( j=i;j<pascal[i].length-1;j++ )
{
System.out.format( "%-" + (max+max) + "s", pascal[i][j] );
}
System.out.println();
}
}