patternprogram_424 softethics

Pattern 424 (String Pattern)

C

#include <stdio.h>
#include <string.h>

int main()
{
  char str[]="SUMIT";
  int i,j;
  int len=strlen(str);

  for (i=0; i<len; i++)
  {
    for (j=0; j<len; j++)
    {
      if (i==len/2)
        printf("%c ",str[j]);
      else if (j==len/2)
        printf("%c ",str[i]);
      else
        printf("  "); // 2ws
    }
    printf("\n");
  }
  return 0;
}

C++

#include<iostream.h>
#include<string.h>
int main()
{

    char str[]="SUMIT";
    int len=strlen(str);
	int i,j;

    for (i=0; i<len; i++)
    {
        for (j=0; j<len; j++)
        {
            if (i==len/2)
                cout<<str[j]<<" ";
            else if (j==len/2)
                cout<<str[i]<<" ";
            else
                cout<<"  "; //2ws
        }
        cout<<endl;
    }
    return 0;
}

Java

class PatternProg
{
    public static void main(String args[])
    {
        String str = "SUMIT";
        int len = str.length();
        
        for (int i = 0; i < len; i++)
        {
            for (int j = 0; j < len; j++)
            {
                if (i == len/2)
                {
                    System.out.print(str.charAt(j)+" ");
                }
                else if (j == len/2)
                {
                    System.out.print(str.charAt(i)+" ");
                }
                else
                {
                    System.out.print("  "); //2ws//2ws
                }
            }
            System.out.println();
        }

    }
}

C#

using System;

class PatternProg
{
  public static void Main()
  {
    string str = "SUMIT";
    int len = str.Length;
    for (int i = 0; i < len; i++)
    {
      for (int j = 0; j < len; j++)
      {
        if (i == len / 2)
        {
          Console.Write(str[j]);
        }
        else if (j == len / 2)
        {
          Console.Write(str[i]);
        }
        else
        {
          Console.Write(" ");
        }
      }
      Console.WriteLine();
    }
    Console.ReadKey(true);
  }
}

Python

string = "SUMIT"

for x in range(0, 5):
 for y in range(0, 5):
  if x == 2:
   print(string[y],end="")
  elif y == 2:
   print(string[x],end="")
  else:
   print(" ",end="")
 print("")
5 2 votes
Rate this Program
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments