answersLogoWhite

0

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));

}

}

}

User Avatar

Wiki User

14y ago

What else can I help you with?

Related Questions

Write a c program to accept a numbers and generate square root cube and exponential values?

write a c program to accept a number and generate a square root cube and exponential values


Write a pascal program that compute an area of a triangle?

{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 number?

Write a program to convert a 2-digit BCD number into hexadecimal


Write a program in qbasic to make a hut using print statemant?

a triangle then a square :)


Could you write a java program that randomly generate a number plate for a car registration using the random method from Maths class eg NLF810?

yes, use for loop;;


How can you generate a palindrome from a given number?

You write the number and then follow it with the digits in reverse order.


Write a program to generate uppercase using cSharp?

string s = "asdfqwer"; s = s.ToUpper(System.Globalization.CultureInfo.CurrentCulture);


Can you write a program to generate sinx table where x varies from 0 to 180 in steps of 15?

Yes, I can.


Write a program that input a positive integer and prints a triangle using for loop?

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?


2 Write a program to convert a 2-digit BCD number into hexadecimal?

WRITE A PROGRAM TO CONVERT A 2-DIGIT bcd NUMBER INTO HEXADECIMAL


Write a c program to generate student mark details using union and find total and average and grade?

write a c program to display marks,total,average,grade using union


How do you write a qbasic program to find area and circumerence of a circle?

Write a program that calculates the area of a triangle in QBASIC