TheDeveloperBlog.com

Home | Contact Us

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

Program To Print Pattern 5

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

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

Solution:

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:

program to print the pattern 5

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:

program to print the pattern 5

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:

program to print the pattern 5

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:

program to print the pattern 5

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:

program to print the pattern 5




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