answersLogoWhite

0


Best Answer

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

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

public static void main(String[] args) {

int i, j;

for (i=1;i<=5;i++){

for (j=1;j<=i;j++){

System.out.print(j);

}

System.out.println("");

}

}

it'll generate Right Angled Triangle... !!

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

import java.io.*;

public class TriPascal {

/** Creates a new instance of TriPascal */

public TriPascal() {

}

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

int nFilas = Integer.parseInt(args[0]);

int nCols = nFilas * 2 - 1 ;

int arrPascal[][] = new int[nFilas][nCols];

int cont = 0;

for(int i=0;i<nFilas;i++){

for(int j=0;j<nCols;j++){

arrPascal[i][j]=0;

}

}

arrPascal[0][nFilas-1]=1;

for(int i=1;i<nFilas;i++){

for(int j=(nFilas-1)-i;j<nCols;j=j+2){

if(cont<=i){

if(j==0j==nCols-1){

arrPascal[i][j] = 1;

}else{

arrPascal[i][j] = arrPascal[i-1][j-1] + arrPascal[i-1][j+1];

}

}

cont++;

}

cont = 0;

}

System.out.println("filas=" + nFilas + "-Columnas=" + nCols);

for(int i=0;i<nFilas;i++){

for(int j=0;j<nCols;j++){

System.out.print(arrPascal[i][j]);

}

System.out.println("");

}

}

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

using System;

namespace ConsoleApplication2

{

public class Triangle

{

public static void Main()

{

int n, i, j, c;

Console.Write("Enter an integer bound > ");

n = Convert.ToInt32(Console.ReadLine());

for (i = 0; i < n; i++)

{

c = 1;

for (j = 0; j <= i; j++)

{

Console.Write(c + " ");

c = (c * (i - j)) / (j + 1);

}

Console.WriteLine();

}

}

}

}

This we can develop using another algorith::Check this out

Posted by Nishant, Excellon software, Nagpur

using System;

namespace PascalTraingle

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine("Enter the level of Pascal Traingle");

int level = Convert.ToInt16(Console.ReadLine());

int[][] PascalTraingle = new int[level][];

PascalTraingle[0] = new int[1];

PascalTraingle[0][0] = 1;

PascalTraingle[1] = new int[2];

PascalTraingle[1][0] = 1;

PascalTraingle[1][1] = 1;

Console.WriteLine("{0}", PascalTraingle[0][0]);

Console.WriteLine("{0} {1}", PascalTraingle[1][0], PascalTraingle[1][1]);

for (int startLevel = 2; startLevel < level; startLevel++)

{

int arrsize = startLevel + 1;

PascalTraingle[startLevel] = new int[arrsize];

Console.Write("{0} ", PascalTraingle[1][0] = 1);

PascalTraingle[startLevel][0] = 1;

for (int i = 1; i <= startLevel-1; i++)

{

int result = PascalTraingle[startLevel - 1][i - 1] + PascalTraingle[startLevel - 1][i];

PascalTraingle[startLevel][i] = result;

Console.Write("{0} ", result);

}

Console.Write("{0} ", PascalTraingle[1][1]);

PascalTraingle[startLevel][arrsize-1] = 1;

Console.WriteLine();

}

Console.Read();

}

}

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

public class series1

{

public static void main ()

{

int n = 0 ;

int m = 0 ;

{

for ( n = 2 ; n < 6 ; n++ )

{

for ( m = 1 ; m < n ; m++)

{

System.out.print(m);

System.out.print(" ");

}

System.out.println();

}

}

}

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

import java.io.*;

public class star

{ public static void main(String args[])

{

int b=4;

for(int i=1;i<=9;i=i+2)

{ for(int j=1;j<=b;j++)

{ System.out.print(" ");

}

for(int k=1;k<=i;k++)

{ System.out.print("*");

}

System.out.println();

b--;

}

}

}

*

**

***

****

*****

******

*******

********

*********

-------------------------------------

check here --- > http://itschoolstudents.blogspot.com/

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

import java.io.*;

import java.lang.*;

class PascalTriangle {

public static void main(String[] args) {

String inpstring = "";

InputStreamReader input = new InputStreamReader(System.in);

BufferedReader reader = new BufferedReader(input);

try

{

System.out.print("Enter number of rows for pascal triangle:");

inpstring = reader.readLine();

int n = Integer.parseInt(inpstring, 10);

for (int y = n; y >=0; y--)

{

int c = 1;

for(int q = 0; q < n-y; q++)

{

System.out.print(" ");

}

for(int x =y,p=0; x >=0; x--,p++)

{

System.out.print(" ");

System.out.print(c); // 3 digits

System.out.print(" ");

c = c * (y - p) / (p + 1);

}

System.out.println();

System.out.println();

}

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

The Java program must first measure each side of the triangle. Then it must add the lengths of the three sides together to find the perimeter.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write java program to generate the number triangle?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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


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


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

a triangle then a square :)


How can you generate a palindrome from a given number?

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


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 to generate uppercase using cSharp?

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


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


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


How do I write a program to do the following... Write a VBA code to take the TippingBucketData.txt file and process it to generate an output file.?

Without knowing the contents of the TippingBucketData.txt file nor how to process it, it would be impossible to say how such a program would be written.