Q:

Java program to print the following pattern

belongs to collection: Java Pattern programs

0

*000*000*
0*00*00*0
00*0*0*00
000***000

Algorithm:

  • STEP 1: START
  • STEP 2: SET lines=4
  • STEP 3: Define i, j
  • STEP 4: SET i =1
  • STEP 5: REPEAT STEP 6 to 15 UNTIL i <= lines
  • STEP 6: SET j=1
  • STEP 7: REPET STEP 8 and 9 UNTIL j <= lines
  • STEP 8: IF i is equal to j PRINT * ELSE PRINT 0
  • STEP 9: SET j = j + 1
  • STEP 10: DECREMENT j by 1 and PRINT *
  • STEP 11: REPEAT STEP 12 and 13 UNTIL j >=1
  • STEP 12: IF i is equals to j PRINT * ELSE PRINT 0
  • STEP 13: SET j = j - 1
  • STEP 14: PRINT a new line
  • STEP 15: SET i = i + 1
  • STEP 16: END

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

public class pattern  
{    
     public static void main(String[] args){    
    int lines=4;    
    int i,j;    
    for(i=1;i<=lines;i++){// this loop is used to print lines    
      for(j=1;j<=lines;j++){// this loop is used to print * in a line    
          if(i==j)    
             System.out.print("*");    
            else    
           System.out.print("0");    
      }    
      j--;    
       System.out.print("*");    
      while(j>=1){// this loop is used to print * in a line    
          if(i==j)    
           System.out.print("*");    
          else    
           System.out.print("0");    
          j--;    
      }    
    System.out.println("");    
  }    
         }    
}    

Output:

*000*000*
0*00*00*0
00*0*0*00
000***000

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java program to print the following pattern... >>
<< Java program to print the following pattern...