Program 2
Print Sequence
Find the logical error in the below code.
expected output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Incorrect Code
void main() { int n=5; for (int i = n; i >= 1; --i) { for (int j = 1; j <= i; ++j) { printf("%d ", j); } printf("\n"); } return 0; }
Correct Code
void main() { int n=5; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= i; ++j) { printf("%d ", j); } printf("\n"); } return 0; }
Login/Signup to comment