Apply for Zend Framework Certification Training

c

< Loops in C Pattern using loop in c >




Level 1: Basic Interview Traps
Q1. Increment operator
#include <stdio.h>

int main() {
    int i;
    for (i = 0; i < 5; i++) {
        printf("%d ", i);
    }
    return 0;
}

Show answerOutput
0 1 2 3 4
Explanation: The loop starts at 0 and stops when i becomes 5.

Q2. Decrement operator
#include <stdio.h>

int main() {
    int i;
    for (i = 5; i > 0; i--) {
        printf("%d ", i);
    }
    return 0;
}

Show answerOutput: 5 4 3 2 1
Explanation: The condition i > 0 becomes false when i reaches 0.

Q3. Increment by 2
#include <stdio.h>

int main() {
    int i;
    for (i = 1; i <= 10; i += 2) {
        printf("%d ", i);
    }
    return 0;
}


Show answerOutput: 1 3 5 7 9
Explanation: i += 2 adds 2 after each iteration.

Q4. Empty initialization and update expressions
#include <stdio.h>

int main() {
    int i = 1;
    for (; i <= 3;) {
        printf("%d ", i);
        i++;
    }
    return 0;
}

Show answerOutput: 1 2 3
Explanation: All three expressions in a for loop may be omitted or placed elsewhere, provided the logic is handled correctly. Here, initialization and increment happen outside the loop header.

Q5. Multiple variables in a for loop
#include <stdio.h>

int main() {
    int i, j;
    for (i = 1, j = 5; i <= 3; i++, j--) {
        printf("%d %d\n", i, j);
    }
    return 0;
}

Show answerOutput
1 5
2 4
3 3
Explanation: The comma operator allows both variables to be initialized and updated in the same loop header.

Level 2: Tricky Loop Logic
Q6. Post-increment inside printf()
#include <stdio.h>

int main() {
    int i = 1;
    for (; i <= 3;) {
        printf("%d ", i++);
    }
    return 0;
}

Show answerOutput: 1 2 3
Explanation: i++ prints the current value first and then increments it.

Q7. Pre-increment in the loop condition
#include <stdio.h>

int main() {
    int i = 0;
    for (; ++i < 4;) {
        printf("%d ", i);
    }
    return 0;
}

Show answerOutput: 1 2 3
Explanation: ++i increments i before checking whether it is less than 4.

Q8. continue statement
#include <stdio.h>

int main() {
    int i;
    for (i = 1; i <= 5; i++) {
        if (i == 3)
            continue;
        printf("%d ", i);
    }
    return 0;
}

Show answerQ9. break statement
#include <stdio.h>

int main() {
    int i;
    for (i = 1; i <= 10; i++) {
        if (i == 5)
            break;
        printf("%d ", i);
    }
    return 0;
}


Show answerQ10. Nested loops
#include <stdio.h>

int main() {
    int i, j;
    for (i = 1; i <= 3; i++) {
        for (j = 1; j <= 2; j++) {
            printf("%d%d ", i, j);
        }
    }
    return 0;
}

Show answerOutput: 11 12 21 22 31 32
Explanation: For each value of i, the inner loop runs twice.

Level 3: Advanced Interview Questions
Q11. Changing the loop variable inside the body
#include <stdio.h>

int main() {
    int i;
    for (i = 1; i <= 5; i++) {
        printf("%d ", i);
        i++;
    }
    return 0;
}

Show answerOutput: 1 3 5
Explanation: i is incremented once inside the body and once by the loop update expression. It therefore increases by 2 per iteration.

Q12. Multiplication in the update expression
#include <stdio.h>

int main() {
    int i;
    for (i = 1; i < 20; i *= 2) {
        printf("%d ", i);
    }
    return 0;
}

Show answerOutput: 1 2 4 8 16
Explanation: The value doubles after every iteration. The next value is 32, which fails the condition i < 20.

Q13. Nested loops with break
#include <stdio.h>

int main() {
    int i, j;
    for (i = 1; i <= 3; i++) {
        for (j = 1; j <= 3; j++) {
            if (j == 2)
                break;
            printf("%d%d ", i, j);
        }
    }
    return 0;
}

Show answerQ14. Sum calculation
#include <stdio.h>

int main() {
    int i, sum = 0;
    for (i = 1; i <= 5; i++) {
        sum += i;
        printf("%d ", sum);
    }
    return 0;
}

Show answerQ15. Comma operator in the condition
#include <stdio.h>

int main() {
    int i, j;
    for (i = 0, j = 5; i < 3, j > 0; i++, j--) {
        printf("%d %d\n", i, j);
    }
    return 0;
}

Show answerOutput
0 5
1 4
2 3
3 2
4 1

< Loops in C Pattern using loop in c >



Ask a question



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


Back to Top