Q:

Write a program to print the following pattern

belongs to collection: Pattern Programs

0

In this pattern program, we are going to draw above pattern on the output screen with the help of various loops.

Algorithm:

  • STEP 1: START
  • STEP 2: SET lines= 10
  • STEP 3: SET space=(lines/2)-2
  • STEP 4: SET i=1 REPEAT STEP 5 to STEP 17 UNTIL i is less than or equals to lines.
  • STEP 5: SET flag1=0
    SET l=1
  • STEP 6: REPEAT STEP 7 and 8 UNTIL l is less than or equals to i
  • STEP 7: IF flag1 is not true
    PRINT '*' and INCREMENT flag1 by 1
    ELSE PRINT BLANK SPACE "" WITH "*"
  • STEP 8: SET l=l+1
  • STEP 9: SET l=1
  • STEP 10: REPEAT STEP 11 UNTIL l is less than or equals to space
  • STEP 11: PRINT "" and SET l=l+1
  • STEP 12: DECREMENT space by 4.
  • STEP 13: SET l=1 and SET flag=1
  • STEP 14: REPEAT STEP 15 UNTIL l is less than or equals to i
  • STEP 15: IF flag is not true PRINT * and increment flag by 1.
    ELSE PRINT BLANK SPACE "" WITH * .
  • STEP 16: PRINT new line
  • STEP 17: SET i=i+1
  • STEP 18: INCREMENT space by 4
  • STEP 19: SET i=lines/2
  • STEP 20: REPEAT STEP 21 to STEP 25 UNTIL i is greater than 1
  • STEP 21: SET flag=0 SET l=1
  • STEP 22: REPEAT STEP 23 and 24 UNTIL l is less than or equals to i
  • STEP 23: IF flag is not true PRINT *
    ELSE PRINT "" + *
  • STEP 24: SET l=l+1
  • STEP 25: PRINT new line, SET i=i-1
  • STEP 26: 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=10;  
int space=(lines*2)-2;  
for(int i=1;i<=(lines/2);i++) // this loop is used to print half lines  
{  
int flagl=0;  
for(int l=1;l<=i;l++)    //this loops is used to print the pattern in a line  
{  
if(!flagl){  
printf("*");  
flagl++;  
}  
else  
{  
printf("");  
printf("*");  
}  
}  
for(int l=1;l<=space;l++)//this loop is used to print the space in a line  
{  
printf("");  
}  
space=space-4;  
int flagr=0;  
for(int l=1;l<=i;l++)// this loop is used to print the * in a line  
{  
if(!flagr){  
printf("*");  
flagr++;  
}  
else  
{  
printf("");  
printf("*");  
}  
}  
printf("\n");  
}   
space=space+4;  
for(int i=(lines/2);i>=1;i--)//this loop is used to print the one half of lines  
{   
int flagl=0;  
for(int l=1;l<=i;l++)// this loop is used to print * inside a line  
{  
if(!flagl){  
printf("*");  
flagl++;  
}  
else  
{  
printf("");  
printf("*");  
}  
}  
for(int l=1;l<=space;l++)// this loop is used to print the space in a line  
{  
printf("");  
}  
space=space+4;// Increment space by 4  
int flagr=0;  
for(int l=1;l<=i;l++)// this loop is used to print * in a line  
{  
if(!flagr){  
printf("*");  
flagr++;  
}  
else  
{  
printf("");  
printf("*");  
}     
}  
printf("\n");  
}  
return 0;  
}  

Output:

pattern

 

PHP Program:

<?php  
$lines=10;  
$space=($lines*2)-2;  
for($i=1;$i<=($lines/2);$i++)//this line is used to print half lines  
{  
  
$flagl=0;  
for($l=1;$l<=$i;$l++)//this loop is used to print * inside a line  
{  
if(!$flagl){  
echo"*";  
$flagl++;  
}  
else  
{  
echo"";  
echo"*";  
}  
}  
for($l=1;$l<=$space;$l++)//this loop is used to print space in a line  
{  
echo"";  
}  
$space=$space-4;  
$flagr=0;  
for($l=1;$l<=$i;$l++)  
{  
if(!$flagr){  
echo"*";  
$flagr++;  
}  
else  
{  
echo"";  
echo"*";  
}  
  
}  
echo"\n";  
}  
$space=$space+4;  
for($i=($lines/2);$i>=1;$i--)//this loop is used to print * inside the line  
{  
  
$flagl=0;  
for($l=1;$l<=$i;$l++)//this loop is used to print the * inside a line  
{  
if(!$flagl){  
echo"*";  
$flagl++;  
}  
else  
{  
echo"";  
echo"*";  
}  
}  
for($l=1;$l<=$space;$l++)// this loop is used to print the space in a line  
{  
echo"";  
}  
$space=$space+4;//Increment space by 4  
$flagr=0;  
for($l=1;$l<=$i;$l++)// this loop is used to print * in a line  
{  
if(!$flagr){  
echo"*";  
$flagr++;  
}  
else  
{  
echo"";  
echo"*";  
}  
}  
echo"\n";  
}  
?>  

 

Output:

Java Program:

public class pattern{  
public static void main(String []args){  
int lines=10;  
int space=(lines*2)-2;  
for(int i=1;i<=(lines/2);i++)//this loop is used to print one half lines  
{              
boolean flagl=false;  
for(int l=1;l<=i;l++)//this loop is used to print the * in a line  
{  
if(!flagl){  
System.out.print("*");  
flagl=true;  
}  
else  
{  
System.out.print("");  
System.out.print("*");  
}  
}    
for(int l=1;l<=space;l++)//this loop is used to print the space in a line  
{  
System.out.print("");  
}  
space=space-4;// Decrement space by 4  
boolean flagr=false;  
for(int l=1;l<=i;l++)  
{  
if(!flagr){  
System.out.print("*");  
flagr=true;  
}  
else  
{  
System.out.print("");  
System.out.print("*");  
}  
  
}  
System.out.println("\n");  
}  
space=space+4;  
for(int i=(lines/2);i>=1;i--)// this loop is used to print the one half lines  
{  
boolean flagl=false;  
for(int l=1;l<=i;l++)// this loop is used to print * in a line  
{  
if(!flagl){  
System.out.print("*");  
flagl=true;  
}  
else  
{  
System.out.print("");  
System.out.print("*");  
}  
}  
  
for(int l=1;l<=space;l++)// this loop is used to print the space in a line  
{  
System.out.print("");  
}  
space=space+4;//Increment space by 4  
boolean flagr=false;  
for(int l=1;l<=i;l++)// this loop is used to print * in a line  
{  
if(!flagr){  
System.out.print("*");  
flagr=true;  
}  
else  
{  
System.out.print("");  
System.out.print("*");  
}        
}  
System.out.println("\n");  
}  
}  
}  

 

Output:

C# Program:

using System.IO;  
using System;  
class Program  
{  
    static void Main()  
    {  
          
        int lines=10;  
    int space=(lines*2)-2;  
    for(int i=1;i<=(lines/2);i++)  
        {  
            bool flagl=false;  
           for(int l=1;l<=i;l++)  
                {  
                if(!flagl){  
                Console.Write("*");  
                flagl=true;  
                }  
                else  
                {  
                    Console.Write(" ");  
                    Console.Write("*");  
                }  
                }  
                  
           for(int l=1;l<=space;l++)  
            {   
                Console.Write(" ");  
            }  
                space=space-4;  
                bool flagr=false;  
                for(int l=1;l<=i;l++)  
                {  
                if(!flagr){  
                Console.Write("*");  
                flagr=true;  
                }  
                else  
                {  
                    Console.Write(" ");  
                    Console.Write("*");  
                }  
            }  
            Console.WriteLine("");  
        }  
        space=space+4;  
          for(int i=(lines/2);i>=1;i--)  
       {  
            bool flagl=false;  
           for(int l=1;l<=i;l++)  
                {  
                if(!flagl){  
                Console.Write("*");  
                flagl=true;  
                }  
                else  
                {  
                    Console.Write(" ");  
                    Console.Write("*");  
                }  
             }    
             for(int l=1;l<=space;l++)  
            {  
                Console.Write(" ");  
            }  
                space=space+4;  
                bool flagr=false;  
                for(int l=1;l<=i;l++)  
                {  
                if(!flagr){  
                Console.Write("*");  
                flagr=true;  
                }  
                else  
                {  
                    Console.Write(" ");  
                    Console.Write("*");  
                }  
            }  
            Console.WriteLine("");  
        }  
          
    }  
}  

 

Output:

Python Program:

lines=10  
space=(lines*2)-2  
i=1  
while i<=(lines/2):  # this loop is used to print lines  
    flagl=0;  
    l=1  
    while l<=i:  #this loop is used to print * in a line  
        if not flagl:  
            print('*', end='', flush=True)  
            flagl=flagl+1  
        else :  
            print(' ', end='', flush=True)  
            print('*', end='', flush=True)  
              
        l=l+1  
    l=1  
    while l<=space:  #this loop is used to print space in a line  
        print(' ', end='', flush=True)  
        l=l+1  
    space=space-4  
    flagr=0  
    l=1  
    while l<=i:  #this loop is used to print * in a line  
        if not flagr:  
            print('*', end='', flush=True)  
            flagr=flagr+1  
        else :  
            print(' ', end='', flush=True)  
            print('*', end='', flush=True)  
        l=l+1  
              
                  
    i=i+1  
    print("")  
      
          
          
          
space=space+4  
i=lines/2  
while i>0:   # this loop is used to print * in a line  
    flagl=0  
    l=1  
    while l<=i:  # this loop is used to print * in a line  
        if not flagl:  
            print('*', end='', flush=True)  
            flagl=flagl+1  
        else:  
            print(' ', end='', flush=True)  
            print('*', end='', flush=True)  
          
        l=l+1  
      
    l=1  
    while l<=space:  #this loop is used to print space in a line  
        print(' ', end='', flush=True)  
        l=l+1  
      
    space=space+4  
    flagr=0  
    l=1  
    while l<=i:  # this loop is used to print * in a line  
        if not flagr:  
            print('*', end='', flush=True)  
            flagr=flagr+1  
        else:  
            print(' ', end='', flush=True)  
            print('*', end='', flush=True)  
        l=l+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... >>