Bitsmore360 - Top trends, Tech News, Tips and Tricks: Java Ques 101 - Program to print Pascal's Triangle in Java

Java Ques 101 - Program to print Pascal's Triangle in Java

In this article titled "Java Ques 101 - Program to print Pascal's Triangle in Java", we will learn to print a Pascal's Triangle. It is one of the most common pattern-based questions asked by the teachers and the interviewers. Many of us fail to answer such questions, due to a lack of basic knowledge of patterns and Pascal's Triangle. 


Java Ques 101 - Program to print Pascal's Triangle in Java

Java Ques 101 - Program to print Pascal's Triangle in Java



What is Pascal's Triangle?

Pascal's triangle was named after the 17th-century French mathematician Blaise Pascal. Pascal’s triangle is an arrangement of numbers in Triangular form such that it gives the coefficients in the expansion of any binomial expression, such as (x + y)n. The Chinese mathematician Jia Xian introduced a triangular representation for the coefficients. This triangle became popular in the 13th century.



Java Ques 101 - Program to print Pascal's Triangle in Java



Formation of Pascal's Triangle

  • Step1: Arrange number 1 along the edges as shown below in fig 1.1.
  • Step2: Fill the blank spaces by adding the above digits.
  • Step 3: By adding the above two number we get 1+1 = 2. Number 2 will be placed in the third row(white space).
  • Step 4: Fill the fourth row, by adding 1+2 in the third row. Place 3 and 3 in the last row.



fig 1.1 Formation of Pascal's Triangle


fig 1.2 Formation of Pascal's Triangle


We can see in above figure 1.2, the first row highlighted in pink contains number 1. It provides the coefficient for the expansion of (x+y)= 1. The second row highlighted in pink contains 1 and 1 which gives us the coefficients for(x+y)= x + y. The third row contains 1, 2,  1 which gives the coefficients for (x+y) = x2 + 2xy + y2. If we want to add a further data in the row the result will be the same.

To learn more about pattern programs check out the article Print Patterns in Java.



Java Ques 101 - Program to print Pascal's Triangle in Java


                1
            1      1
        1     2       1
    1     3       3      1




In the given question, we have to print print Pascal's Triangle in Java. It is a form of number pattern program. Before solving any pattern program, we will first see the number of rows and columns used in the program. Also see if a white space is present in the pattern.






Here:-

            Number of rows = 5
            Space = present

We will first initialize the variable n1 for number of rows. Initialize the value of  variable n1=5. Initialize the variable space for white space present. In the beginning, the value of white space  should be equal to the value of number of rows i.e space = n1. Initialize new variable with a value 1 i.e number = 1. We will use three for loops to print Pascal's triangle. First for loop i.e. outer for loop will work for number of rows. Second loop, inner for loop will work for space. Third for loop will work for printing the number using the formula number=number*(i-j)/(j+1). All the loop will execute five times.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
public class demopascal
{
public static void main(String[] args)
{
int row=5, i, j, space, number;
for(i=0; i<row; i++)
{
for(space=row; space>i; space--)
{
 System.out.print(" ");
}
number=1;
for(j=0; j<=i; j++)
{
System.out.print(number+ " ");
number = number*(i-j)/(j+1);
}
System.out.print("\n");
}
}
}


Output:-





Java Ques 101 - Program to print Pascal's Triangle in C



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
int main() {
int rows = 5;
int f = 1;
int space;
int i, j;
   
   
for (i = 0; i < rows; i++)
{
for(space = 1; space <= rows - i; space++)
printf("  ");
for (j = 0; j <= i; j++)
{
if (j == 0 || i == 0)
f = 1;
else
f = f * (i - j + 1) / j;
printf("%4d", f);
}
printf("\n");
}
return 0;
}

Let us see some of the different Pattern questions asked by the teachers and the interviewer-

  • Write a program in java to print a star pattern.
  • Write a program in java to print a number pattern.
  • Write a program in java to print a character pattern.
  • How to print * pattern in java?
  • How to print * pattern in python
  • How to print * pattern in c
  • How to print * pattern in java
  • How to print * pattern in SQL?
  • How to print * pattern in PHP?
  • How to print patterns in Python?
  • How to print patterns in Java?
  • How to print patterns in C?
  • How to print patterns in SQL?
  • Write a program to print half pyramid of numbers
  • Write a program to print inverted half pyramid of *
  • Write a program to print full Pyramid of *
  • Write a program to print Pascal's triangle
  • Write a program to print Floyd's triangle.


Did you find this article helpful? OR Have any doubt, write in the comment section. To learn more about Bitsmore360, check out the rest of our Blog, and visit our Pinterest site and follow us on Instagram.

No comments:

Post a Comment