Time spent here:

Saturday, 18 June 2016

PRINT THE PATTERN

Problem statement:
Print the pattern.

Input:
PROGRAM

Output:

P         M
 R      A
   O  R
     G
  O    R
 R       A
P          M

Code:

#include <stdio.h>
#include<string.h>
int main()
{
  char string[100];
   int left,right,length,i,j;
  scanf("%s",string);
  left=0;length=strlen(string);right=length-1;
  for(i=0;i<length;i++)
  {
    
      for(j=0;j<length;j++)
      {
         if(j==left && left==right)
            { printf("%c",string[j]); break;}
          else if(j==left || j==right)
              printf("%c",string[j]);
         else
              printf(" ");
      }
   printf("\n");
   if(i<length/2)
    { left++;right--;}
   else
    { left--;right++;}
  }
  
    return 0;

}

No comments:

Post a Comment