Q:

Java program to print the following pattern

belongs to collection: Java Pattern programs

0

Algorithm:

  • STEP 1: START
  • STEP 2: SET lines=8
  • STEP 3: DEFINE i, j
  • STEP 4: SET i=1.REPEAT STEP 5 to 14 UNTIL i<lines< li=""></lines<>
  • STEP 5: SET j=1
  • STEP 6: REPEAT STEP 7 and 8 UNTIL j <=(lines/2)
  • STEP 7: IF j is equals to i PRINT j
                  ELSE IF i is greater than 4 and j equals (lines-i) PRINT j
                  ELSE PRINT " "
  • STEP 8: j = j + 1
  • STEP 9: j = j - 2
  • STEP 10: REPEAT STEP 11 and 12 UNTIL j > 0
  • STEP 11: IF j is equals to i PRINT j
                  ELSE IF i is greater than 4 and j equals (lines-i) then PRINT j
                  ELSE PRINT " "
  • STEP 12: j = j - 1
  • STEP 13: PRINT a new line
  • STEP 14: i = i + 1
  • STEP 15: 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=8;  
    int i,j;  
    for(i=1;i<lines;i++){// this loop is used to print the lines  
        for(j=1;j<=lines/2;j++){// this loop is used to print numbers  
            if(i==j){  
                System.out.print(j);  
            }  
            else if(i>4 && j==lines-i){  
                System.out.print(j);  
            }  
            else{  
               System.out.print(" ");  
            }  
        }  
        j=j-2;  
        while(j>0){ //this loop is used to print numbers  
            if(i==j){  
            System.out.print(j);  
            }  
            else if(i>4 && j==lines-i){  
                System.out.print(j);  
            }  
            else{  
                System.out.print(" ");  
            }  
  
            j--;  
        }  
        System.out.println("");  
    }  
     }  
}  

Output:

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...