C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Write a program to print the following patternAlgorithm
Solution:C Program:#include <stdio.h> int main() { int lines=8; int i,j,k; for(i=1;i<lines;i++){// this loop is used to print the lines for(j=1;j<=lines/2;j++){// this loop is used to print numbers if(i==j){ printf("%d",j); } else if(i>4 && j==lines-i){ printf("%d",j); } else{ printf(" "); } } j=j-2; while(j>0){ // this loop is used to print numbers if(i==j){ printf("%d",j); } else if(i>4 && j==lines-i){ printf("%d",j); } else{ printf(" "); } j--; } printf("\n"); } return 0; } Output: Java Program:public class pattern{ public static void main(String []args){ int lines=8; int i,j,k; for(i=1;i<lines;i++){// this loop is used to print the lines for(j=1;j<=lines/2;j++){// this loop is used to print numbers if(i==j){ System.out.print(j); } else if(i>4 && j==lines-i){ System.out.print(j); } else{ System.out.print(" "); } } j=j-2; while(j>0){ //this loop is used to print numbers if(i==j){ System.out.print(j); } else if(i>4 && j==lines-i){ System.out.print(j); } else{ System.out.print(" "); } j--; } System.out.println(""); } } } Output: C# Program:using System; public class Program { public static void Main() { int lines=8; int i,j; for(i=1;i<lines;i++){// this loop is used to print lines for(j=1;j<=lines/2;j++){// this loop is used to print lines if(i==j){ Console.Write(j); } else if(i>4 && j==lines-i){ Console.Write(j); } else{ Console.Write(" "); } } j=j-2; while(j>0){ //this loop is used to print lines if(i==j){ Console.Write(j); } else if(i>4 && j==lines-i){ Console.Write(j); } else{ Console.Write(" "); } j--; } Console.WriteLine(); } } } Output: PHP Program:$lines=8; $i=1; $j=1; for($i=1;$i<$lines;$i++){// this loop is used to print lines for($j=1;$j<=$lines/2;$j++){ // this loop is used to print lines if($i==$j){ echo $j; } else if($i>4 && $j==$lines-$i){ echo $j; } else{ echo ' '; } } $j=$j-2; while($j>0){// this loop is used to print lines if($i==$j){ echo $j; } else if($i>4 && $j==$lines-$i){ echo $j; } else{ echo " "; } $j--; } echo "<br>"; } Output: Python Program:lines=8 i=1 j=1 while i<lines: # this loop is used to print lines j=1 while j<=lines/2: #this loop is used to print lines if i==j: print(j, end='', flush=True) elif i>4 and j==lines-i: print(j, end='', flush=True) else : print(" ", end='', flush=True) j=j+1 j=j-2; while j>0: #this loop is used to print lines if i==j: print(j, end='', flush=True) elif i>4 and j==lines-i: print(j, end='', flush=True) else : print(" ", end='', flush=True) j=j-1 print("") i=i+1 Output:
Next TopicProgram to Print the Pattern 6
|