Wednesday 23 September 2015

Patterns (one of their favorites)

 Patterns (one of their favorites)

If the interviewer asks you a pattern and you don’t know how to do that you are screwed big time. Often you might make a very simple mistake which the interviewer was actually looking for. Here we’ll find out how to print this pattern
?
1
2
3
4
5
A B C D E D C B A
  A B C D C B A
    A B C B A
      A B A
        A
And here is the code.
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
//Printing pattern
#include
#include
int main(){
 int n,i,j;
 printf("Enter no of lines: ");
 scanf("%d",&n);
 for(i=0;i<n;i++){
 for(j=0;j<i;j++){ //for printing spaces
 printf(" ");
 }
 for(j=0;j<n-i;j++){ //for printing the left side
  printf("%c ",'A'+j); //the value of j is added to 'A'(ascii value=65)
  }
  for(j=n-i-2;j>=0;j--){ //for printing the right side
    printf("%c ",'A'+j);
  }
 printf("\n");
 }
 getch();
}

Leave a Reply

 
 

Labels