Save the following file as PascalTriangle.java:
public class PascalTriangle {
public static void main(String args[]) {
int x = 6;
int triangle[][] = new int[x][x];
for (int i = 0; i < x; i++) {
for (int j = 0; j < x; j++) {
triangle[i][j] = 0;
}
}
for(int i = 0; i < x; i++) {
triangle[i][0] = 1 ;
}
for (int i = 1; i < x; i++) {
for (int j = 1; j < x; j++) {
triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j];
}
}
for (int i = 0; i < x; i++) {
for(int j=0;j<=i;j++) {
System.out.print(triangle[i][j]+ " ");
}
System.out.println();
}
}
}
answer 2:
import java.util.*;
class pascal
{
protected static void main()throws Exception
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the limit: ");
byte a=sc.nextByte();
sc.close();
String l[]=new String[a];
for(byte i=0;i<a;i++)
l[i]="";
l[0]="1";
l[1]="1 1";
for(byte i=2;i<a;i++)
{
for(byte j=0;j<(i+1);j++)
{
if(j==0j==i)
l[i]+="1 ";
else
{
StringTokenizer st=new StringTokenizer(l[i-1]);
byte k=(byte)st.countTokens(),n=0;
String w[]=new String[k];
for(byte f=0;f<w.length;f++)
w[f]="";
for(byte f=0;f<l[i-1].length();f++)
{
if(l[i-1].charAt(f)==' '){
n++;
continue;
}
w[n]+=l[i-1].charAt(f);
}
n=0;
int x=Integer.parseInt(w[j-1])+Integer.parseInt(w[j]);
l[i]+=(String.valueOf(x)+" ");
}
}
}
for(byte i=0;i<l.length;i++)
{
System.out.println();
for(byte j=a;j>=i;j--)
System.out.print(" ");
for(byte j=0;j<l[i].length();j++)
System.out.print(l[i].charAt(j));
}
}
}
a triangle then a square :)
yes, use for loop;;
string s = "asdfqwer"; s = s.ToUpper(System.Globalization.CultureInfo.CurrentCulture);
write a program that reads in the size of the side of square and then pints a hollow square of that size out of asterisks and blanks?
write a c program to display marks,total,average,grade using union
write a c program to accept a number and generate a square root cube and exponential values
{A program to compute the area of a triangle} {by Ogboin W. Meshach} Var; b,h:real; BEGIN Writeln('Triangle'); Write('Base: '); Readln(base); Write('Height: '); Readln(height); area:=0.5*base*height; Writeln('Area: ', area :0:2); End.
Write a program to convert a 2-digit BCD number into hexadecimal
a triangle then a square :)
yes, use for loop;;
You write the number and then follow it with the digits in reverse order.
string s = "asdfqwer"; s = s.ToUpper(System.Globalization.CultureInfo.CurrentCulture);
Yes, I can.
write a program that reads in the size of the side of square and then pints a hollow square of that size out of asterisks and blanks?
WRITE A PROGRAM TO CONVERT A 2-DIGIT bcd NUMBER INTO HEXADECIMAL
write a c program to display marks,total,average,grade using union
Write a program that calculates the area of a triangle in QBASIC