Posts

Showing posts with the label display-pattern-in-java

Java program to display Pascal triangle

Image
Pascal triangle in java: Pascal triangle Program: import java.util.Scanner; public class  Pascal_triangle { public static void main(String[] args) { int i,j,k; Scanner sc = new Scanner(System.in); System.out.print("Enter number of rows: "); int n = sc.nextInt(); int c[][] = new int[n][n]; int space = (n-1); for(i=0;i<n;i++) { for(k=0;k<space;k++) { System.out.print(" "); } for(j=0;j<i;j++) { if(i==j) { c[i][j] = 1; System.out.print(c[i][j] + " "); } else if(j==0) { c[i][j] = 1; System.out.print(c[i][j] + " "); } else { c[i][j] = c[i-1][j-1] + c[i-1][j]; System.out.print(c[i][j] + " "); } } System.out.println(); space--; } } } Output: Enter number of rows: 7      1     1 1    1 2 1   1 3 3 1  1 4 6 4 1 1 5 10 10 5 1 "If you have any other logic to ...

Java program to display Floyd's triangle

Image
Floyd's triangle: Floyd's Triangle Program Example: import java.util.Scanner; public class Floyd { public static void main(String[] args) { int i, j, number=1; Scanner sc = new Scanner(System.in); System.out.print("Enter number of rows: "); int n = sc.nextInt(); for(i=0;i<n;i++) { for(j=0;j<i;j++) { System.out.print(number + " "); number++; } System.out.println(); } } } Output: Enter number of rows: 7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 "If you have any other logic to attain the same result please email the code to  sharkrish50@gmail.com "

Java program to display diamond pattern

Image
Diamond pattern: Diamond pattern Program Example: import java.util.Scanner; public class Diamond { public static void main(String[] args) { int i,j,k,p,q,r; Scanner sc = new Scanner(System.in); System.out.print("Enter the values: "); int n = sc.nextInt(); int spaces = n-1; for(i=0;i<n;i++) { for(j=0;j<spaces;j++) { System.out.print(" "); } for(k=0;k<=i;k++) { System.out.print("* "); } spaces--; System.out.println(); } spaces = 1; for(p=(n-1);p>0;p--) { for(q=0;q<spaces;q++) { System.out.print(" "); } for(r=0;r<p;r++) { System.out.print("* "); } System.out.println(); spaces++; } } } Output: Enter the values: 7       *      * *     * * *    * * * *   * * * * *  * * * * * * * * * * * * *  * * * * * *   * * * * *    * * * *     * * ...

Program to display full pyramid using *

Image
Program to display full pyramid using *: Program: public class Pyramid { public static void main(String[] args) { for(int i = 1; i <= 5; i++) { for(int j = 1; j <= (5 - i); j++) { System.out.print(" "); } for(int j = 1; j <= ((2*i) - 1); j++) { System.out.print("*"); } System.out.println(); } } } Output:     *    ***   *****  ******* *********