Monday 5 June 2017

C program to print X pattern


#include<stdio.h>
#include<string.h>
int main()
{
int i,j;
char a[50];
scanf("%s",a);
int t=strlen(a)-1; //set right side diagonal start value
for(i=0;i<strlen(a);i++)
{
for(j=0;j<strlen(a);j++)
{
if(i==j&&j==t){
printf("%c",a[i]);--t; // to print mid value
}
else if (i==j){
printf("%c",a[i]); // to print left to right diagonal
}
else if(j==t&&j!=i){
printf("%c",a[t]);--t;} // to print right to left diagonal
else
printf(" "); // to print spaces between spaces
}
printf("\n"); // to print on new line
}
return 0;
}
OUTPUT : 


No comments:

Post a Comment