Oct 08, 2009 14:19
import java.util.*;
public class Pascal{
public static void main(String[] args){
int n = Integer.parseInt(args[0]);
char[] ch =new char[10];
for(int i=0;i<10;i++)
ch[i] = ' ';
int count = 0;
Random rand = new Random();
while(count int ind = rand.nextInt();
ind = Math.abs(ind);
int num = ind % 10;
char pch = ch[num];
if(ind % 5 == 0 && ch[num] == ' ')
ch[num] = '*';
else if(ind % 4 == 0 && ch[num] == ' ')
ch[num] = '^';
else if(ind % 3 == 0 && ch[num] == ' ')
ch[num] = '+';
else if(ind % 2 == 0 && ch[num] == ' ')
ch[num] = '!';
else if(ind % 1 == 0 && ch[num] == ' ')
ch[num] = '!';
if(ch[num] != ' '&& pch == ' ')
count++;
}
for(int i=0;i String out="";
for(int k=0;k<(n-i);k++)
out+= " ";
for(int j=0; j < (i+1); j++){
Float f = new Float((fac(i)/(fac(j)*fac(i-j)))+"");
int nn = f.intValue()%10;
out += ch[nn]+" ";
}
System.out.println(out);
}
for(int i=n-1;i>=0;i--){
String out="";
for(int k=0;k<(n-i);k++)
out+= " ";
for(int j=0; j < (i+1); j++){
Float f = new Float((fac(i)/(fac(j)*fac(i-j)))+"");
int nn = f.intValue()%10;
out += ch[nn]+" ";
}
System.out.println(out);
}
}
static float fac(int n){
float f=1;
for(int i=1;i<=n;i++)
f*=i;
return f;
}
}
Uses:
java Pascal
e.g. java Pascal 15 5
e.g. java Pascal 20 4
e.g. java Pascal 25 6
*first parameter is the size
*2nd parameter is the random factor should be less then 9
Run this program multipal times and see different Rangoli. try with different parameter(s).
code,
pascal,
rangoli,
java