TheDeveloperBlog.com

Home | Contact Us

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

Program To Print Pattern 3

Program To Print Pattern 3 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.

*000*000*

0*00*00*0

00*0*0*00

000***000

Algorithm

  • STEP 1: START
  • STEP 2: SET lines=4
  • STEP 3: Initialize i and j
  • STEP 4: SET i =1
  • STEP 5: REPEAT STEP 6 to 15 UNTIL i less than or equals to line
  • STEP 6: SET j=1
  • STEP 7: REPET STEP 8 and 9 UNTIL j is less than or equals to 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 is greater than 0
  • 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: EXIT

Solution:

C Program:

#include <stdio.h>
int main()
{
  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)
            printf("*");
            else
          printf("0");
      }
      j--;
      printf("*");
      while(j>=1){// this loop is used to print * in a line
          if(i==j)
          printf("*");
          else
          printf("0");
          j--;
      }
    printf("\n");
  }
    return 0;
}

Output:

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

Java 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

C# Program:

using System;
					
public class Program
{
	public static void Main()
	{
	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 lines
          if(i==j)
            Console.Write("*");
            else
          Console.Write("0");
      }
      j--;
      Console.Write("*");
      while(j>=1){// this loop is used to print lines
          if(i==j)
          Console.Write("*");
          else
          Console.Write("0");
          j--;
      }
    Console.WriteLine("");
  }
	}
}

Output:

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

PHP Program:

<?php
$lines=4;
$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 lines
          if($i==$j)
            echo "*";
            else
          echo "0";
      }
      $j--;
      echo "*";
      while($j>=1){// this loop is used to print lines
          if($i==$j)
          echo "*";
          else
          echo "0";
          $j--;
      }
    echo "<br>";
  }
?>

Output:

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

Python Program:

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

Output:

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





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