Q:

Write a program to print the following pattern

belongs to collection: Pattern Programs

0

Algorithm

  • STEP 1: START
  • STEP 2: SET lines=8, i=1,j=1,k=1
  • STEP 3: REPEAT STEP 4 to 13 UNTIL i is less than lines
  • STEP 4: SET j=1
  • STEP 5: REPEAT STEP 6 and 7 UNTIL j is less than or equals to (lines/2)
  • STEP 6: 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 7: j = j + 1
  • STEP 8: j = j - 2
  • STEP 9: REPEAT STEP 10 and 11 UNTIL j is greater than 0
  • STEP 10: 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 11: j = j - 1
  • STEP 12: PRINT a new line
  • STEP 13: i = i + 1
  • STEP 14: EXIT

All Answers

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

C Program:

#include <stdio.h>  
int main()  
{  
    int lines=8;  
    int i,j,k;  
    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){  
                printf("%d",j);     
            }  
            else if(i>4 && j==lines-i){  
                printf("%d",j);     
            }  
            else{  
               printf(" ");   
            }              
        }  
        j=j-2;  
        while(j>0){  // this loop is used to print numbers  
            if(i==j){  
            printf("%d",j);     
            }  
            else if(i>4 && j==lines-i){  
                printf("%d",j);     
            }  
            else{  
                printf(" ");   
            }  
             
            j--;  
        }  
        printf("\n");  
    }  
   return 0;  
}  

 

Output:

Java Program: 

public class pattern{  
     public static void main(String []args){  
          
   int lines=8;  
    int i,j,k;  
    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:

 

C# Program:

using System;                     
public class Program  
{  
    public static void Main()  
    {  
    int lines=8;  
    int i,j;  
    for(i=1;i<lines;i++){// this loop is used to print lines  
        for(j=1;j<=lines/2;j++){// this loop is used to print lines  
            if(i==j){  
                Console.Write(j);     
            }  
            else if(i>4 && j==lines-i){  
                 Console.Write(j);     
            }  
            else{  
               Console.Write(" ");   
            }                
        }  
        j=j-2;  
        while(j>0){ //this loop is used to print lines  
            if(i==j){  
            Console.Write(j);   
            }  
            else if(i>4 && j==lines-i){  
                 Console.Write(j);     
            }  
            else{  
                Console.Write(" ");   
            }  
            j--;  
        }  
         Console.WriteLine();  
    }  
}  
} 

 

Output:

 

PHP Program:

$lines=8;  
$i=1;  
$j=1;  
    for($i=1;$i<$lines;$i++){// this loop is used to print lines  
        for($j=1;$j<=$lines/2;$j++){ // this loop is used to print lines  
            if($i==$j){  
                echo $j;   
            }  
            else if($i>4 && $j==$lines-$i){  
               echo $j;     
            }  
            else{  
               echo ' ';   
            }             
        }  
        $j=$j-2;  
        while($j>0){// this loop is used to print lines  
            if($i==$j){  
            echo $j;      
            }  
            else if($i>4 && $j==$lines-$i){  
                echo $j;   
            }  
            else{  
                echo " ";  
            }  
                $j--;  
        }  
        echo "<br>";  
    }  

 

Output:

 

Python Program:

lines=8  
i=1  
j=1  
while i<lines:   # this loop is used to print lines  
    j=1  
    while j<=lines/2:    #this loop is used to print lines  
        if i==j:  
            print(j, end='', flush=True)     
        elif i>4 and j==lines-i:  
            print(j, end='', flush=True)    
        else :  
            print(" ", end='', flush=True)     
        j=j+1  
    j=j-2;  
    while j>0:   #this loop is used to print lines  
        if i==j:  
            print(j, end='', flush=True)  
        elif i>4 and j==lines-i:  
            print(j, end='', flush=True)    
        else :  
            print(" ", end='', flush=True)    
        j=j-1  
    print("")  
    i=i+1  

 

Output:

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

total answers (1)

Write a program to print the following pattern... >>
<< Write a program to print the following pattern...