Problem statement
Write a program to print the numbers/string in pattern form
Solution
#include<stdlib.h>
int main()
{
char s[100];
int i,j,len,k;
scanf("%s",s);
len=strlen(s);
for(i=0,k=len-1;i<len;i++,k--)
{
for(j=0;j<len;j++)
{
if(i==j)
{
printf("%c",s[i]);
}
else if((i+j)==len-1)
{
printf("%c",s[k]);
}
else
{
printf(" ");
}
}
printf("\n");
}
}
Input
Program
Output
p m
r a
o r
g
o r
r a
p m
Note: Give the input string/numbers of odd length.
Program Explanation
- This program is commonly asked in zoho interview rounds.
- Here we will see how to print the pattern as per the output form asked in zoho.
- Declare the header files for accessing the standard input and output functions.
- Declare the necessary integer and character variables
- Get the input through standard input function of string format specifier because it will suit for both number and alphabet character's for performing desired operations.
- Calculate the length of the string and then using nested for loop perform the operations to display the input characters as per the desired output.