C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Q) Write a program to print the following pattern.5432* 543*1 54*21 5*321 *4321 Algorithm
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
Next TopicProgram to Print the Pattern 3
|