Advertisement
dmilicev

amazon interview v1.c

Oct 25th, 2019
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.86 KB | None | 0 0
  1. /*
  2.  
  3.     amazon interview v1.c
  4.  
  5. Enter n = 6
  6. *
  7. *
  8. * i=1       spaces=0
  9. *
  10. * i=2
  11. ******      spaces=0
  12. -----*
  13. -----*
  14. -----* i=3      spaces=5        spaces=1*(n-1)=k*(n-1)  k=1
  15. -----*
  16. -----* i=4
  17. -----******     spaces=5        spaces=1*(n-1)=k*(n-1)  k=1
  18. ----------*
  19. ----------*
  20. ----------* i=5     spaces=10   spaces=2*(n-1)=k*(n-1)  k=2
  21. ----------*
  22. ----------* i=6
  23. ----------******    spaces=10   spaces=2*(n-1)=k*(n-1)  k=2
  24.  
  25. This is stairs.
  26. Algorithm, if input is n=6:
  27.  
  28. i
  29. 1: (odd), go down n-1=5 times with  0 spaces before (*)
  30. 2: (even), go right n=6 times (*)
  31. 3: (odd), go down n-1=5 times with  5 spaces before (*)
  32. 4: (even), go right n=6 times (*)
  33. 5: (odd), go down n-1=5 times with 10 spaces before (*)
  34. 6: (even), go right n=6 times (*)
  35.  
  36. */
  37.  
  38.  
  39. #include<stdio.h>
  40.  
  41. int main()
  42. {
  43. /*
  44.     i   iterator for horizontal and vertical lines
  45.     j   iterator for '*'
  46.     s   iterator for spaces '-'
  47.     n   number of horizontal and vertical lines
  48.     k   multiplication factor to calculate spaces
  49.     spaces  number of spaces to be printed
  50. */
  51.     int i, j, s, n, k=0, spaces=0;
  52.  
  53.     printf("Enter n = ");
  54.     scanf("%d",&n);
  55.  
  56.     if(n==1)    // n==1 is special case, exceptional case
  57.     {
  58.         printf("*");    // do that and
  59.         return 0;       // exit
  60.  
  61.     }
  62.  
  63.                 // now is n > 1
  64.     for(i=1; i<=n; i++)
  65.     {
  66.         if ( i%2 == 0 ) // if i is even, print horizontal line
  67.         {
  68.             if(i>2)
  69.             {
  70.                 for(s=0; s<spaces; s++) // put spaces before (*)
  71.                 {
  72.                     printf("-");
  73.                 }
  74.             }
  75.  
  76.             for(j=0; j<n; j++)  // go right n times(*)
  77.             {
  78.                 printf("*");
  79.             }
  80.             printf("\n");           // new line
  81.         }
  82.         else            // if i is odd, print vertical line
  83.         {
  84.             if(i>2)
  85.             {
  86.                 k++;
  87.                 spaces=k*(n-1); // calculate how meny spaces before (*)
  88.             }
  89.  
  90.             for(j=0; j<n-1; j++)    // go down n-1 times with spaces before (*)
  91.             {
  92.                 for(s=0; s<spaces; s++) // put spaces before (*)
  93.                 {
  94.                     printf("-");
  95.                 }
  96.  
  97.                 printf("*\n");
  98.             }
  99.         }
  100.     }
  101.  
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement