TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program To Print Pattern 1

Program To Print Pattern 1 on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph, pattern, string etc.

<< Back to PROGRAM

Write a program to print the following pattern.

program to print the pattern 1

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

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. program to print the pattern 1

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:

program to print the pattern 1

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:

program to print the pattern 1

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:

program to print the pattern 1

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:

program to print the pattern 1




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf