Apply for Zend Framework Certification Training

c

< Predict the output on loop Last >




Q1 Print a right-angled triangle

Output
*
* *
* * *
* * * *
* * * * *

Solution

#include <stdio.h>
int main() {
    int i, j, n = 5;
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}
Explanation: The outer loop controls the rows, and the inner loop prints stars equal to the current row number.
Q2. Print an inverted right-angled triangle
Output
* * * * *
* * * *
* * *
* *
*

#include <stdio.h>
int main() {
    int i, j, n = 5;
    for (i = n; i >= 1; i--) {
        for (j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}
Explanation: The outer loop decreases from 5 to 1, so each new row contains one fewer star.

Q3. Print a pyramid pattern
Output
    *
   * *
  * * *
 * * * *
* * * * *

#include <stdio.h>
int main() {
    int i, j, n = 5;
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= n - i; j++) {
            printf(" ");
        }
        for (j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}
Explanation: The first inner loop prints leading spaces. The second inner loop prints stars.

Q4. Print an inverted pyramid
Output
* * * * *
 * * * *
  * * *
   * *
    *
#include <stdio.h>
int main() {
    int i, j, n = 5;
    for (i = n; i >= 1; i--) {
        for (j = 1; j <= n - i; j++) {
            printf(" ");
        }
        for (j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}
Explanation: As the number of stars decreases, the number of leading spaces increases.

Q5. Print a number triangle
Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

#include <stdio.h>
int main() {
    int i, j, n = 5;
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= i; j++) {
            printf("%d ", j);
        }
        printf("\n");
    }
    return 0;
}
Explanation: The inner loop prints numbers from 1 up to the current row number.

Q6. Print Floyd's Triangle
Output
1
2 3
4 5 6
7 8 9 10

#include <stdio.h>
int main() {
    int i, j, n = 4;
    int num = 1;
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= i; j++) {
            printf("%d ", num);
            num++;
        }
        printf("\n");
    }
    return 0;
}
Explanation: The variable num increases after every number printed, continuing the sequence across rows.

Q7. Print a diamond shape
Output
    *
   * *
  * * *
 * * * *
* * * * *
 * * * *
  * * *
   * *
    *

#include <stdio.h>
int main() {
    int i, j, n = 5;
    // Upper part
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= n - i; j++) {
            printf(" ");
        }
        for (j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }
    // Lower part
    for (i = n - 1; i >= 1; i--) {
        for (j = 1; j <= n - i; j++) {
            printf(" ");
        }
        for (j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}
Explanation: The upper part increases the number of stars from 1 to 5. The lower part decreases them from 4 to 1.

Q8. Print a square of numbers
Output
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
#include <stdio.h>
int main() {
    int i, j, n = 5;
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= n; j++) {
            printf("%d ", i);
        }
        printf("\n");
    }
    return 0;
}
Explanation: The outer loop determines the number printed in each row. The inner loop repeats that number five times.

Q9. Print a hollow square
Output
* * * * *
*       *
*       *
*       *
* * * * *

#include <stdio.h>
int main() {
    int i, j, n = 5;
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= n; j++) {
            if (i == 1 || i == n ||
                j == 1 || j == n) {
                printf("* ");
            } else {
                printf("  ");
            }
        }
        printf("\n");
    }
    return 0;
}
Explanation: A star is printed if the current position is on the first row, last row, first column, or last column. Otherwise, spaces are printed.

Q10. Print a checkerboard-style pattern
Output
* * * *
 * * * *
* * * *
 * * * *
#include <stdio.h>
int main() {
    int i, n = 4;
    for (i = 0; i < n; i++) {
        if (i % 2 == 0) {
            printf("* * * *\n");
        } else {
            printf(" * * * *\n");
        }
    }
    return 0;
}

< Predict the output on loop Last >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top