Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- amazon interview v1.c
- Enter n = 6
- *
- *
- * i=1 spaces=0
- *
- * i=2
- ****** spaces=0
- -----*
- -----*
- -----* i=3 spaces=5 spaces=1*(n-1)=k*(n-1) k=1
- -----*
- -----* i=4
- -----****** spaces=5 spaces=1*(n-1)=k*(n-1) k=1
- ----------*
- ----------*
- ----------* i=5 spaces=10 spaces=2*(n-1)=k*(n-1) k=2
- ----------*
- ----------* i=6
- ----------****** spaces=10 spaces=2*(n-1)=k*(n-1) k=2
- This is stairs.
- Algorithm, if input is n=6:
- i
- 1: (odd), go down n-1=5 times with 0 spaces before (*)
- 2: (even), go right n=6 times (*)
- 3: (odd), go down n-1=5 times with 5 spaces before (*)
- 4: (even), go right n=6 times (*)
- 5: (odd), go down n-1=5 times with 10 spaces before (*)
- 6: (even), go right n=6 times (*)
- */
- #include<stdio.h>
- int main()
- {
- /*
- i iterator for horizontal and vertical lines
- j iterator for '*'
- s iterator for spaces '-'
- n number of horizontal and vertical lines
- k multiplication factor to calculate spaces
- spaces number of spaces to be printed
- */
- int i, j, s, n, k=0, spaces=0;
- printf("Enter n = ");
- scanf("%d",&n);
- if(n==1) // n==1 is special case, exceptional case
- {
- printf("*"); // do that and
- return 0; // exit
- }
- // now is n > 1
- for(i=1; i<=n; i++)
- {
- if ( i%2 == 0 ) // if i is even, print horizontal line
- {
- if(i>2)
- {
- for(s=0; s<spaces; s++) // put spaces before (*)
- {
- printf("-");
- }
- }
- for(j=0; j<n; j++) // go right n times(*)
- {
- printf("*");
- }
- printf("\n"); // new line
- }
- else // if i is odd, print vertical line
- {
- if(i>2)
- {
- k++;
- spaces=k*(n-1); // calculate how meny spaces before (*)
- }
- for(j=0; j<n-1; j++) // go down n-1 times with spaces before (*)
- {
- for(s=0; s<spaces; s++) // put spaces before (*)
- {
- printf("-");
- }
- printf("*\n");
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement