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...

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.