TheDeveloperBlog.com

Home | Contact Us

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

Program To Print Pattern 9

Program To Print Pattern 9 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 on the console.

0

909

89098

7890987

678909876

56789098765

4567890987654

345678909876543

23456789098765432

1234567890987654321

Algorithm

  • STEP 1: START
  • STEP 2: SET lines=10
    SET i=1
  • STEP 3: REPEAT STEP 4 to 15 UNTIL i is less than or equals to lines
  • STEP 4: SET count =0
    SET j=1
  • STEP 5: REPEAT STEP 6 and 7 UNTIL j is less than lines
  • STEP 6: IF count is less than (i-1)
    IF j is not less than (lines-i+1)
    PRINT j AND INCREMENT count by 1
  • STEP 7: SET j=j+1
  • STEP 8: PRINT 0
  • STEP 9: SET count =0
  • STEP 10: DECREMENT j by 1
  • Step 11: REPEAT STEP 12 and 13 UNTIL j is greater than 0
  • STEP 12: IF count is less than (i-1)
    PRINT j AND INCREMENT count by 1
  • STEP 13: SET j = j - 1
  • STEP 14: PRINT new line
  • STEP 15: i = i + 1
  • STEP 16: EXIT

Solution:

C Program:

#include <stdio.h>
int main()
{
   int lines=10;
   int i,j;
   int count;
   for(i=1;i<=lines;i++){// this loop is used to print lines
       count=0;
        for(j=1;j<lines;j++){// this loop is used to print numbers in a line
            if(count<i-1){
                if(!(j<lines-i+1)){
                    printf("%d",j);
                    count++;
                }            
            }
        }
        printf("0");
        count=0;
        for(--j;j>=1;j--){// this loop is used to print j and increment count
            if(count<i-1){
            printf("%d",j);
            count++;
            }
        }
        printf("\n");
   }
    return 0;
}

Output:

0 
909 
89098 
7890987 
678909876 
56789098765 
4567890987654 
345678909876543 
23456789098765432 
1234567890987654321 

Java Program:

public class patter{
     public static void main(String []args){
         int lines=10;
   int i,j;
   int count;
   for(i=1;i<=lines;i++){// this loop is used to print lines
       count=0;
        for(j=1;j<lines;j++){// this loop is used to print numbers in a line
            if(count<i-1){
                if(!(j<lines-i+1)){
                    System.out.print(j);
                    count++;
                }
            }
        }
        System.out.print("0");
        count=0;
        for(--j;j>=1;j--){// this loop is used to print j and increment count
            if(count<i-1){
            System.out.print(j);
            count++;
            }
        }
        System.out.println("");
   }
     }
}

Output:

0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321

C# Program:

using System;					
public class Program
{
	public static void Main()
	{
		 int lines=10;
   int i,j;
   int count;
   for(i=1;i<=lines;i++){// this loop is used to print lines
       count=0;
        for(j=1;j<lines;j++){// this loop is used to print numbers in a line
            if(count<i-1){
                if(!(j<lines-i+1)){
                    Console.Write(j);
                    count++;
                }
            }
        }
         Console.Write("0");
        count=0;
        for(--j;j>=1;j--){// this loop is used to print j and increment count
            
            if(count<i-1){
             Console.Write(j);
            count++;
            }
        }
         Console.WriteLine("");
   }
	}
}

Output:

0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321

PHP Program:

$lines=10;
$i;
$j;
$count;
   for($i=1;$i<=$lines;$i++){// this loop is used to print lines
       $count=0;
        for($j=1;$j<$lines;$j++){// this loop is used to print numbers in line
            if($count<$i-1){
                if(!($j<$lines-$i+1)){
                    echo $j;
                    $count++;
                }
            
            }
        }
       echo '0';
        $count=0;
        for(--$j;$j>=1;$j--){//this loop is used to print j and increment count
            
            if($count<$i-1){
            echo $j;
            $count++;
            }
        }
        echo "<br>";
   }

Output:

0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321

Python Program:

lines=10
i=1
j=1
count=0
while i<=lines:	#this loop is used to print lines
    
    count=0
    j=1
    while j<lines:	# this loop is used to priont numbers in a line
        if count<i-1:
            if j<lines-i+1:
                count=count
            else :
                print(j, end='', flush=True)
                count=count+1
        j=j+1
    print("0", end='', flush=True)
    count=0
    j=j-1
    while j>=1:	#this loop is used to print j and increment count
        if count<i-1:
            print(j, end='', flush=True)
            count=count+1
        j=j-1
    print("")
    i=i+1   

Output:

0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321





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