C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Write a program to print the following pattern.In this pattern program, we are going to draw above pattern on the output screen with the help of various loops. Algorithm:
C Program:#include <stdio.h> int main() { int lines=10; int space=(lines*2)-2; for(int i=1;i<=(lines/2);i++) // this loop is used to print half lines { int flagl=0; for(int l=1;l<=i;l++) //this loops is used to print the pattern in a line { if(!flagl){ printf("*"); flagl++; } else { printf(""); printf("*"); } } for(int l=1;l<=space;l++)//this loop is used to print the space in a line { printf(""); } space=space-4; int flagr=0; for(int l=1;l<=i;l++)// this loop is used to print the * in a line { if(!flagr){ printf("*"); flagr++; } else { printf(""); printf("*"); } } printf("\n"); } space=space+4; for(int i=(lines/2);i>=1;i--)//this loop is used to print the one half of lines { int flagl=0; for(int l=1;l<=i;l++)// this loop is used to print * inside a line { if(!flagl){ printf("*"); flagl++; } else { printf(""); printf("*"); } } for(int l=1;l<=space;l++)// this loop is used to print the space in a line { printf(""); } space=space+4;// Increment space by 4 int flagr=0; for(int l=1;l<=i;l++)// this loop is used to print * in a line { if(!flagr){ printf("*"); flagr++; } else { printf(""); printf("*"); } } printf("\n"); } return 0; } Output: pattern.PHP Program:<?php $lines=10; $space=($lines*2)-2; for($i=1;$i<=($lines/2);$i++)//this line is used to print half lines { $flagl=0; for($l=1;$l<=$i;$l++)//this loop is used to print * inside a line { if(!$flagl){ echo"*"; $flagl++; } else { echo""; echo"*"; } } for($l=1;$l<=$space;$l++)//this loop is used to print space in a line { echo""; } $space=$space-4; $flagr=0; for($l=1;$l<=$i;$l++) { if(!$flagr){ echo"*"; $flagr++; } else { echo""; echo"*"; } } echo"\n"; } $space=$space+4; for($i=($lines/2);$i>=1;$i--)//this loop is used to print * inside the line { $flagl=0; for($l=1;$l<=$i;$l++)//this loop is used to print the * inside a line { if(!$flagl){ echo"*"; $flagl++; } else { echo""; echo"*"; } } for($l=1;$l<=$space;$l++)// this loop is used to print the space in a line { echo""; } $space=$space+4;//Increment space by 4 $flagr=0; for($l=1;$l<=$i;$l++)// this loop is used to print * in a line { if(!$flagr){ echo"*"; $flagr++; } else { echo""; echo"*"; } } echo"\n"; } ?> Output: Java Program:public class pattern{ public static void main(String []args){ int lines=10; int space=(lines*2)-2; for(int i=1;i<=(lines/2);i++)//this loop is used to print one half lines { boolean flagl=false; for(int l=1;l<=i;l++)//this loop is used to print the * in a line { if(!flagl){ System.out.print("*"); flagl=true; } else { System.out.print(""); System.out.print("*"); } } for(int l=1;l<=space;l++)//this loop is used to print the space in a line { System.out.print(""); } space=space-4;// Decrement space by 4 boolean flagr=false; for(int l=1;l<=i;l++) { if(!flagr){ System.out.print("*"); flagr=true; } else { System.out.print(""); System.out.print("*"); } } System.out.println("\n"); } space=space+4; for(int i=(lines/2);i>=1;i--)// this loop is used to print the one half lines { boolean flagl=false; for(int l=1;l<=i;l++)// this loop is used to print * in a line { if(!flagl){ System.out.print("*"); flagl=true; } else { System.out.print(""); System.out.print("*"); } } for(int l=1;l<=space;l++)// this loop is used to print the space in a line { System.out.print(""); } space=space+4;//Increment space by 4 boolean flagr=false; for(int l=1;l<=i;l++)// this loop is used to print * in a line { if(!flagr){ System.out.print("*"); flagr=true; } else { System.out.print(""); System.out.print("*"); } } System.out.println("\n"); } } } Output: C# Program:using System.IO; using System; class Program { static void Main() { int lines=10; int space=(lines*2)-2; for(int i=1;i<=(lines/2);i++) { bool flagl=false; for(int l=1;l<=i;l++) { if(!flagl){ Console.Write("*"); flagl=true; } else { Console.Write(" "); Console.Write("*"); } } for(int l=1;l<=space;l++) { Console.Write(" "); } space=space-4; bool flagr=false; for(int l=1;l<=i;l++) { if(!flagr){ Console.Write("*"); flagr=true; } else { Console.Write(" "); Console.Write("*"); } } Console.WriteLine(""); } space=space+4; for(int i=(lines/2);i>=1;i--) { bool flagl=false; for(int l=1;l<=i;l++) { if(!flagl){ Console.Write("*"); flagl=true; } else { Console.Write(" "); Console.Write("*"); } } for(int l=1;l<=space;l++) { Console.Write(" "); } space=space+4; bool flagr=false; for(int l=1;l<=i;l++) { if(!flagr){ Console.Write("*"); flagr=true; } else { Console.Write(" "); Console.Write("*"); } } Console.WriteLine(""); } } } Output: Python Program:lines=10 space=(lines*2)-2 i=1 while i<=(lines/2): # this loop is used to print lines flagl=0; l=1 while l<=i: #this loop is used to print * in a line if not flagl: print('*', end='', flush=True) flagl=flagl+1 else : print(' ', end='', flush=True) print('*', end='', flush=True) l=l+1 l=1 while l<=space: #this loop is used to print space in a line print(' ', end='', flush=True) l=l+1 space=space-4 flagr=0 l=1 while l<=i: #this loop is used to print * in a line if not flagr: print('*', end='', flush=True) flagr=flagr+1 else : print(' ', end='', flush=True) print('*', end='', flush=True) l=l+1 i=i+1 print("") space=space+4 i=lines/2 while i>0: # this loop is used to print * in a line flagl=0 l=1 while l<=i: # this loop is used to print * in a line if not flagl: print('*', end='', flush=True) flagl=flagl+1 else: print(' ', end='', flush=True) print('*', end='', flush=True) l=l+1 l=1 while l<=space: #this loop is used to print space in a line print(' ', end='', flush=True) l=l+1 space=space+4 flagr=0 l=1 while l<=i: # this loop is used to print * in a line if not flagr: print('*', end='', flush=True) flagr=flagr+1 else: print(' ', end='', flush=True) print('*', end='', flush=True) l=l+1 print () i=i-1 Output:
Next TopicProgram to Print the Pattern 2
|