Pattern 421 (String Pattern)

Pattern 421 (String Pattern) post thumbnail image

C

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

int main()
{
  char arr[] = "SoftEthics";
  int n = 5;
  int x = 0;
  int i,j,k;


  for(i = 0; i < n; i++)
  {
    for(j = 0; j < i; j++)
    {
      printf("  "); // 2ws
    }
    for(k = 0; k < n - i; k++)
    {
      printf("%c ",arr[x++]);

      if (x == strlen(arr))
        x = 0;
    }
    printf("\n");
  }
  return 0;
}

C++

#include <iostream.h>
#include <string.h>

int main()
{
  char arr[] = "SoftEthics";
  int n = 5;
  int x = 0;
  

  
  for(int i = 0; i < n; i++)
  {
    for(int j = 0; j < i; j++)
    {
      cout<<"  "; //2ws
    }
    for(int k = 0; k < n - i; k++)
    {
      cout<<arr[x++]<<" ";

      if (x == strlen(arr))
        x = 0;
    }
    cout<<endl;
  }
  return 0;
}

Java

class PatternProg
{
	public static void main(String args[])
	{
	  String arr = "SoftEthics";
	  int n = 5;
	  int x = 0;

	  for (int i = 0; i < n; i++)
	  {
		for (int j = 0; j < i; j++)
		{
		  System.out.print("  "); //2ws
		}
		for (int k = 0; k < n - i; k++)
		{
		  System.out.print(arr.charAt(x++)+" ");

		  if (x == arr.length())
		  {
			x = 0;
		  }
		}
		System.out.println();
	  }
	  
	}
}

C#

using System;

class PatternProg
{
  public static void Main()
  {
    string arr = "SoftEthics";
    int n = 5;
    int x = 0;

    for (int i = 0; i < n; i++)
    {
      for (int j = 0; j < i; j++)
      {
        Console.Write("  "); // 2ws
      }
      for (int k = 0; k < n - i; k++)
      {
        Console.Write(arr[x++] + " ");

        if (x == arr.Length)
        {
          x = 0;
        }
      }
      Console.WriteLine();
    }


    Console.ReadKey(true);
  }
}

Python

string = "SUMIT"
n = 5
d = 0

for x in range(0, n):
    for y in range(0, x):
        print("  ", end="")#2ws
    for z in range(0, n - x):
        print(string[d]+" ", end="")
        d += 1

        if d == len(string):
           d = 0
    print()
5 1 vote
Rate this Program
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Related Patterns