C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Write a program to print the following pattern.*000*000* 0*00*00*0 00*0*0*00 000***000 Algorithm
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
Next TopicProgram to Print the Pattern 4
|