{}
CODE VISUALIZER
Master DSA, Python and C with step-by-step code visualization.
See it in action
CODE VISUALIZER
Master DSA, Python and C with step-by-step code visualization.
See it in action
run-icon
Main.java
import java.util.*; public class butterflypattern { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("enter no. of row="); int n=sc.nextInt(); // upper part of pattern for(int i=1; i<=2*n-1; i++){ System.out.print("*"+" "); } System.out.println(); n--; for(int i=1; i<=n; i++){ //1st part for star for(int j=1; j<=n+1-i; j++){ System.out.print("*"+" "); } // 2nd part for space for(int j=1; j<=2*i-1; j++){ System.out.print(" "+" "); } // 3rd part for star for(int j=1; j<=n+1-i; j++){ System.out.print("*"+" "); } System.out.println(); } for(int i=n; i>=1; i--){ //1st part for star for(int j=1; j<=n+1-i; j++){ System.out.print("*"+" "); } // 2nd part for space for(int j=1; j<=2*i-1; j++){ System.out.print(" "+" "); } // 3rd part for star for(int j=1; j<=n+1-i; j++){ System.out.print("*"+" "); } System.out.println(); } n++; for(int i=1; i<=2*n-1; i++){ System.out.print("*"+" "); } } }
Output