How do I make this in C with for loop


Ad

Advertisements

Joined
Jan 11, 2023
Messages
2
Reaction score
1
The simplest and most readable solution would be to do that with two nested for loops; the outer loop iterating over the sequence's starting value, and the inner loop iterating over the individual digits in in the sequence.
 
Joined
Jan 30, 2023
Messages
108
Reaction score
8
C:
#include <stdio.h>

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

Advertisements

Joined
Feb 1, 2023
Messages
4
Reaction score
0
#include <iostream>
using namespace std;

int main() {
for (int i = 1; i <= 5; i++) {
for (int j = i; j < i + (2 * (i - 1) + 1); j++) {
cout << j << " ";
}
cout << endl;
}
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top