C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Write a program to print the following pattern
Algorithm
Solution:C Program:
#include <stdio.h>
int main()
{
int matrix[5][5];
int i,j,k,l;
int direction=1;
for(i=0;i<5;i++){
for(j=0;j<5;j++){
matrix[i][j]=0;
}
}
for(i=1,j=0,k=0;i<=16;i++){
matrix[j][k]=i;
switch(direction){
case 1:if(k+1<5){
if(matrix[j][k+1]==0){
k++;
continue;
}
else{
j++;
direction=2;
continue;
}
}
else{
j++;
direction=2;
continue;
}
break;
case 2:if(j+1<5){
if(matrix[j+1][k]==0){
j++;
continue;
}
else{
direction=3;
k--;
continue;
}
}
else{
direction=3;
k--;
continue;
}
break;
case 3:if(k-1>=0){
if(matrix[j][k-1]==0){
k--;
continue;
}
else{
direction=4;
j--;
continue;
}
}
else{
direction=4;
j--;
continue;
}
break;
case 4:if(j-1>=0){
if(matrix[j-1][k]==0){
j--;
continue;
}
else{
k++;
direction=1;
continue;
}
}
else{
k++;
direction=1;
continue;
}
break;
}
}
for(i=0;i<5;i++){
for(j=0;j<5;j++){
if(matrix[i][j]==0){
printf("\t");
}
else{
printf("%d\t",matrix[i][j]);
}
}
printf("\n");
}
return 0;
}
Output:
Java Program:
public class pattern{
public static void main(String []args){
int matrix[][]=new int[5][5];
int i,j,k,l;
int direction=1;
for(i=0;i<5;i++){
for(j=0;j<5;j++){
matrix[i][j]=0;
}
}
for(i=1,j=0,k=0;i<=16;i++){
matrix[j][k]=i;
switch(direction){
case 1:if(k+1<5){
if(matrix[j][k+1]==0){
k++;
continue;
}
else{
j++;
direction=2;
continue;
}
}
else{
j++;
direction=2;
continue;
}
case 2:if(j+1<5){
if(matrix[j+1][k]==0){
j++;
continue;
}
else{
direction=3;
k--;
continue;
}
}
else{
direction=3;
k--;
continue;
}
case 3:if(k-1>=0){
if(matrix[j][k-1]==0){
k--;
continue;
}
else{
direction=4;
j--;
continue;
}
}
else{
direction=4;
j--;
continue;
}
case 4:if(j-1>=0){
if(matrix[j-1][k]==0){
j--;
continue;
}
else{
k++;
direction=1;
continue;
}
}
else{
k++;
direction=1;
continue;
}
}
}
for(i=0;i<5;i++){
for(j=0;j<5;j++){
if(matrix[i][j]==0){
System.out.print("\t");
}
else{
System.out.print(matrix[i][j]+"\t");
}
}
System.out.println("");
}
}
}
Output:
C# Program:
using System.IO;
using System;
public class Program
{
public static void Main(String[] args)
{
int[,] matrix=new int[5,5];
int i,j,k;
int direction=1;
for(i=0;i<5;i++){
for(j=0;j<5;j++){
matrix[i,j]=0;
}
}
for(i=1,j=0,k=0;i<=16;i++){
matrix[j,k]=i;
switch(direction){
case 1:if(k+1<5){
if(matrix[j,k+1]==0){
k++;
continue;
}
else{
j++;
direction=2;
continue;
}
}
else{
j++;
direction=2;
continue;
}
case 2:if(j+1<5){
if(matrix[j+1,k]==0){
j++;
continue;
}
else{
direction=3;
k--;
continue;
}
}
else{
direction=3;
k--;
continue;
}
case 3:if(k-1>=0){
if(matrix[j,k-1]==0){
k--;
continue;
}
else{
direction=4;
j--;
continue;
}
}
else{
direction=4;
j--;
continue;
}
case 4:if(j-1>=0){
if(matrix[j-1,k]==0){
j--;
continue;
}
else{
k++;
direction=1;
continue;
}
}
else{
k++;
direction=1;
continue;
}
}
}
for(i=0;i<5;i++){
for(j=0;j<5;j++){
if(matrix[i,j]==0){
Console.Write(" \t");
}
else{
Console.Write(matrix[i,j]+"\t");
}
}
Console.WriteLine("\n");
}
}
}
Output:
PHP Program:
<?php
$i;
$j;
$k;
$l;
$direction=1;
$matrix=array();
for($i=0;$i<5;$i++){
for($j=0;$j<5;$j++){
$matrix[$i][$j]=0;
}
}
echo "Matrix before snake=";
echo "\n";
for($i=0;$i<5;$i++){
for($j=0;$j<5;$j++){
echo $matrix[$i][$j];
echo " ";
}
echo "\n";
}
for($i=1,$j=0,$k=0;$i<=16;$i++){
$matrix[$j][$k]=$i;
switch($direction){
case 1:if($k+1<5){
if($matrix[$j][$k+1]==0){
$k++;
continue;
}
else{
$j++;
$direction=2;
continue;
}
}
else{
$j++;
$direction=2;
continue;
}
break;
case 2:if($j+1<5){
if($matrix[$j+1][$k]==0){
$j++;
continue;
}
else{
$direction=3;
$k--;
continue;
}
}
else{
$direction=3;
$k--;
continue;
}
break;
case 3:if($k-1>=0){
if($matrix[$j][$k-1]==0){
$k--;
continue;
}
else{
$direction=4;
$j--;
continue;
}
}
else{
$direction=4;
$j--;
continue;
}
break;
case 4:if($j-1>=0){
if($matrix[$j-1][$k]==0){
$j--;
continue;
}
else{
$k++;
$direction=1;
continue;
}
}
else{
$k++;
$direction=1;
continue;
}
break;
}
}
echo "Matrix after snake";
echo "\n";
for($i=0;$i<5;$i++){
for($j=0;$j<5;$j++){
if($matrix[$i][$j]==0){
echo " ";
}
else{
echo $matrix[$i][$j];
}
echo " ";
if ($matrix[$i][$j]<10){
echo " ";
}
}
echo "\n";
}
Output: ![]()
Python Program:
i=0
j=0
k=1
l=1
direction=1
matrix=[[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],]
print("Matrix before snake=")
i=0
while i<5:
j=0
while j<5:
print(matrix[i][j], end='', flush=True)
print(" ", end='', flush=True)
if matrix[i][j]<10:
print(" ", end='', flush=True)
j=j+1
print("")
i=i+1
i=1
j=0
k=0
while i<=16:
matrix[j][k]=i
if direction==1:
if k+1<5:
if matrix[j][k+1]==0:
k=k+1
else:
j=j+1
direction=2
else:
j=j+1
direction=2
elif direction==2:
if j+1<5:
if matrix[j+1][k]==0:
j=j+1
else:
direction=3
k=k-1
else:
direction=3
k=k-1
elif direction==3:
if k-1>=0:
if matrix[j][k-1]==0:
k=k-1
else:
direction=4
j=j-1
else:
direction=4
j=j-1
elif direction==4:
if j-1>=0:
if matrix[j-1][k]==0:
j=j-1
else:
k=k+1
direction=1
else:
k=k+1
direction=1
i=i+1
print("Matrix after snake=")
i=0
while i<5:
j=0
while j<5:
if matrix[i][j]==0:
print(" ", end='', flush=True)
else:
print(matrix[i][j], end='', flush=True)
print(" ", end='', flush=True)
if matrix[i][j]<10:
print(" ", end='', flush=True)
j=j+1
print("")
i=i+1
Output:
Next TopicProgram to Print the Pattern 7
|