how do i display something like this using for loop?

0
2 4 6 8 10 12 14
18 24 26 28 30 32

thank you for helping...

Dani AI

Generated

The sequence posted by is clearly even numbers arranged on three lines, but the third line shown has irregular gaps (14 → 18 and 18 → 24), so it may be a typo or an intentionally irregular list. 's modulus idea will correctly test evenness, and / correctly show that stepping by 2 is the natural way to generate evens — but to reproduce the exact layout a loop must also control where line breaks appear.

One flexible approach is to generate consecutive even numbers and split them into rows according to an array of counts (easy to adapt if row lengths change):

#include <iostream>
#include <vector>

int main() {
    std::vector<int> counts = {1, 7, 6}; // numbers per row
    int value = 0;
    for (size_t r = 0; r < counts.size(); ++r) {
        for (int j = 0; j < counts[r]; ++j) {
            if (j) std::cout << ' ';
            std::cout << value;
            value += 2;
        }
        std::cout << '\n';
    }
    return 0;
}

If the goal is to print the exact, possibly nonconsecutive numbers shown in the post, store each row explicitly and print those rows (this is simplest and less error-prone for irregular patterns):

#include <iostream>
#include <vector>

int main() {
    std::vector<std::vector<int>> rows = {
        {0},
        {2,4,6,8,10,12,14},
        {18,24,26,28,30,32}
    };
    for (const auto &row : rows) {
        for (size_t i = 0; i < row.size(); ++i) {
            if (i) std::cout << ' ';
            std::cout << row[i];
        }
        std::cout << '\n';
    }
}

Notes: avoid trailing spaces by printing a space before elements except the first; ensure the sum of counts matches the intended number of printed elements; use stepping by 2 rather than modulus when generating evens (simpler and clearer); explicit lists are fine when the pattern is irregular.

Recommended Answers

All 3 Replies

You need to use the mod operator.

example :

2 % 2 = 0
4 % 2 = 0
12 % 2 = 0
114 % 2 = 0

as you can see even numbers mod 2 will equal 0. So use this as your
test case in your for loop. and if true then print that number else do nothing.

just increment by 2 instead of one and print every number..

for(i=0; i<x; i=i+2)

just increment by 2 instead of one and print every number..

for(i=0; i<x; i=i+2)

Like Campbel said, the following will do just that:

#include <iostream>                 // General Header
#define POINT 10                    // Max point to go to for the 'for loop'
int main(){                         // Main
    for (int i=0; i<=POINT; i+=2)   /*
                                     * The 'for loop', intialized a var called "i",
                                     * sets it to zero, check if its lower or equal to POINT,
                                     * if this is true, then runs the loop (the single line below,
                                     * then adds two to "i", and checks the statement "i<=POINT" once again,
                                     * and repeats this.
                                     */
        std::cout << i << " ";      // Writes out the value of "i" and a space  
    std::cin.get();                 // Pauses the program, while waiting for input
    return 0;                       // Terminates the program
    }

Pure code:

#include <iostream>                 
#define POINT 10                    
int main(){                         
    for (int i=0; i<=POINT; i+=2)   
        std::cout << i << " ";       
    std::cin.get();                 
    return 0;                       
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.