Untitled
unknown
plain_text
8 days ago
7.9 kB
2
Indexable
Never
import java.util.*; public class advancepattern { public static void pattern_hollowrec(int row , int column){ //loop for rows for(int i = 1; i<=row; i++){ //loop for column for(int j = 1; j<=column; j++ ){ if(i==1 || i==row || j==1 || j==column){ System.out.print("* "); }else{ System.out.print(" "); } } System.out.println(); } } public static void pattern_iverted_and_rotated_half_pyramid(int n){ //loop for row for(int i = 1; i<=n; i++){ //for space for(int j = 1; j<=n-i; j++){ System.out.print(" "); } // for starts for(int j = 1; j<=i; j++){ System.out.print("* "); } System.out.println(); } } public static void pattern_iverted_and_rotated_half_pyramid_with_number(int n){ for(int i = 0; i<=n-1; i++){ for(int j = 1; j<=n-i; j++){ System.out.print(j + " "); } System.out.println(); } } public static void pattern_Floyd_triangle(int n){ int c = 1; for(int i = 1 ; i<=n; i++){ for(int j = 1; j<=i; j++){ System.out.print(c + " "); c++; } System.out.println(); } } public static void pattern_01_triangle(int n) { for(int i = 1 ; i<=n; i++){ for(int j = 1 ; j<=i; j++){ // int c = i+j; if((i+j)%2==0){ System.out.print("1 "); } else{ System.out.print("0 "); } } System.out.println(); } } public static void butterfly(int n){ for(int i = 1 ; i<=n; i++){ for(int j = 1 ; j<=i; j++){ System.out.print("* "); } for(int j = 1 ; j<=2*(n-i); j++){ System.out.print(" "); } for(int j = 1 ; j<=i; j++){ System.out.print("* "); } System.out.println(); } for(int i = n ; i>=1; i--){ for(int j = 1 ; j<=i; j++){ System.out.print("* "); } for(int j = 1 ; j<=2*(n-i); j++){ System.out.print(" "); } for(int j = 1 ; j<=i; j++){ System.out.print("* "); } System.out.println(); } } public static void rhombus(int n){ for(int i = 1 ; i<=n; i++){ for(int j = 1 ; j<=(n-i); j++){ System.out.print(" "); } for(int j = 1 ; j<=n; j++){ System.out.print("* "); } System.out.println(); } } public static void emptyrhombus(int n){ for(int i = 1 ; i<=n; i++){ for(int j = 1 ; j<=(n-i); j++){ System.out.print(" "); } for(int j = 1; j<=n; j++ ){ if(i==1 || i==n || j==1 || j==n){ System.out.print("* "); }else{ System.out.print(" "); } } System.out.println(); } } public static void Diamond(int n){ for(int i = 1 ; i<=n; i++){ for(int j = 1 ; j<=(n-i); j++){ System.out.print(" "); } for(int j = 1; j<=((2*i)-1); j++ ){ System.out.print(j + " "); } System.out.println(); } for(int i = n ; i>=1; i--){ for(int j = 1 ; j<=(n-i); j++){ System.out.print(" "); } for(int j = 1; j<=((2*i)-1); j++){ System.out.print(j + " "); } System.out.println(); } } public static void Diamond2(int n){ int totalnoline = (2*n)-1; int space = n-1; int star = 1; int currentline = 1; while(currentline <= totalnoline ){ for(int i = 1 ; i<=space; i++){ System.out.print(" "); } int number= 1; for(int i = 1; i<=star ; i++){ System.out.print(number + " "); if(i<=star/2){ number++; }else{ number--; } } // for(int i = star; i>=1; i--){ // System.out.println(i + " "); // } System.out.println(); if(currentline < n){ space--; star=star+2; }else{ space++; star=star-2; } currentline++; } // if(star==(2*n)+1){ // star = (2*n)-1; // space++; // } // while(currentline >= 1 ){ // for(int i = 1 ; i<=space; i++){ // System.out.print(" "); // } // for(int i = 1; i<=star ; i++){ // System.out.print("* "); // } // System.out.println(); // currentline--; // space++; // star=star-2; // } } public static void butterfly2(int n){ int totalnoline = 2*n; int space = (2*n)-2; int star = 1; int currentline = 1; while(currentline <= totalnoline ){ //print star for(int i = 1; i<=star; i++){ System.out.print("* "); } //print space for(int i = 1 ; i<=space; i++){ System.out.print(" "); } //print star for(int i = 1; i<=star ; i++){ System.out.print("* "); } //next line System.out.println(); if(currentline < n){ space=space-2; star++; } else{ space=space+2; star--; } currentline++; } // if(star==(n+1)){ // star = n; // space= space+2; // } // while(currentline >= 1 ){ // for(int i = 1; i<=star; i++){ // System.out.print("* "); // } // for(int i = 1 ; i<=space; i++){ // System.out.print(" "); // } // for(int i = 1; i<=star ; i++){ // System.out.print("* "); // } // System.out.println(); // currentline--; // space=space+2; // star--; // } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("enter no. of rows"); int i = sc.nextInt(); // System.out.println("enter no. of column"); // int j = sc.nextInt(); // pattern_hollowrec(i, j); // pattern_iverted_and_rotated_half_pyramid(i); // pattern_iverted_and_rotated_half_pyramid_with_number(i); // pattern_Floyd_triangle(i); // pattern_01_triangle(i); // rhombus(i); // emptyrhombus(i); // Diamond(i); Diamond2(i); // butterfly(i); // butterfly2(i); sc.close(); } }
Leave a Comment