The Artima Developer Community
Sponsored Link

Java Buzz Forum
Print Pascals triangle using java program

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Print Pascals triangle using java program Posted: Aug 13, 2016 11:49 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Print Pascals triangle using java program
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • Pascals triangle means arranging numbers in pascal triangle format.
  • First row starts with number 1.
  • Here is the pascal triangle with 8 rows.



  •  The sum of all numbers in each row will be double the sum of all numbers in above row
  •  The diagonals adjacent to the border diagonals of 1's contains natural numbers in order


 Program #1: Java example program to print  numbers in pascals triangle pattern.

  1. package interviewprograms.instanceofjava;

  2. import java.util.Scanner;
  3. /*
  4.  * www.instanceofjava.com
  5.  */
  6. public class PasclasTriangleProgram {

  7. public static void main(String args[]){
  8. Scanner in = new Scanner(System.in);
  9. System.out.println("Enter number of rows ");

  10. int rows= in.nextInt();

  11.  for(int i =0;i<rows;i++) {

  12.         int number = 1;
  13.   
  14.   System.out.format("%"+(rows-i)*2+"s","");

  15.  for(int j=0;j<=i;j++) {

  16.     System.out.format("%4d",number);

  17.       number = number * (i - j) / (j + 1);

  18. }

  19.   System.out.println();

  20. }

  21. }
  22. }        

Output:

  1. Enter number of rows 
  2. 8
  3.                    1
  4.                  1   1
  5.                1   2   1
  6.              1   3   3   1
  7.            1   4   6   4   1
  8.          1   5  10  10   5   1
  9.        1   6  15  20  15   6   1
  10.      1   7  21  35  35  21   7   1

1.Pattern Programs in java Part-1

2.Pattern Programs in java Part-2

3.Pattern Programs in java Part-3 

Read: Print Pascals triangle using java program

Topic: 24% off Acer Chromebook 15 CB5-571-C1DZ (15.6-Inch Full HD IPS, 4GB RAM, 16GB SSD) - Deal Alert Previous Topic   Next Topic Topic: React component with Media queries for Responsive

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use