TheDeveloperBlog.com

Home | Contact Us

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

Program To Print Pattern 2

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

<< Back to PROGRAM

Q) Write a program to print the following pattern.

5432*

543*1

54*21

5*321

*4321

Algorithm

  • STEP 1: START
  • STEP 2: SET lines=5
    SET i=1
  • Step 3: REPEAT STEP 4 TO 8 UNTIL i is less than or equals to lines
  • STEP 4: SET j=lines
  • STEP 5: REPEAT STEP 6 and 7 UNTIL j is greater than 0
  • STEP 6: IF j!=i
    PRINT j
    ELSE
    PRINT *
  • STEP 7: j=j-1
  • STEP 8: i=i+1
  • Step 9: EXIT

Solution:

C Program:

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

Output:

5432*

543*1

54*21

5*321

*4321

Java Program:

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

Output:

5432*
543*1
54*21
5*321
*4321

C# Program:

using System;					
public class Program
{
	public static void Main()
	{
		 int i,j,lines=5;
    for(i=1;i<=lines;i++){// this loop is used to print the lines
       for(j=lines;j>=1;j--){// this loop is used to print numbers in a line
           if(j!=i)
            Console.Write(j);
            else
             Console.Write("*");
    } 
    Console.WriteLine("\n");
    }
    
	}
}

Output:

5432*

543*1

54*21

5*321

*4321

PHP Program:

<?php
  $i;
  $j;
  $lines=5;

  for($i=1;$i<=$lines;$i++){// this loop is used to print lines 
       for($j=$lines;$j>=1;$j--){// this loop used print numbers in a line
           if($j!=$i)
            echo $j;
            else
             echo '*';
    } 
    echo "<br>" ;        
    }
?>

Output:

 5432*
543*1
54*21
5*321
*4321

Python Program:

lines=5
i=1
while(i<=lines):# this loop is used to print lines
    j=lines
    while (j>=1):# this loop is used to print numbers in a line
        if j!=i:
            print(j, end='', flush=True)
        else:
            print('*', end='', flush=True)
        j=j-1
    print("")
    i=i+1;

Output:

 5432*

543*1

54*21

5*321

*4321





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