Write a short program that takes an integer value (call it n) from the user and prints out a triangle composed of O and X characters.
For example, if the user enters 3 as the input, your program should print out:
O
OX
O X O
If the user enters 5 as the input, your program should print out:
O
O X
O X O
O X O X
O X O X O

Sorry I couldn't give you some code that I have use to attempt this question. But I have no idea how to do this. Thanks a lot in advance :)

Dani AI

Generated

The goal is a left‑aligned triangle where row i contains i characters separated by single spaces, alternating O and X and always starting with O. was right to point at nested loops — an outer loop for rows and an inner loop for columns is the simplest structure.

Two common mistakes visible in 's attempt: the inner for has a stray semicolon (for(...);) which makes its body empty, and the code prints n+1-row (a number) instead of characters. Removing the semicolon and placing the cout inside the inner loop fixes control flow; use a parity check (or toggle a char) to pick O vs X, and avoid system("pause") for portability.

Try this minimal, correct example:

#include <iostream>

int main() {
    int n;
    std::cout << "Enter an integer: ";
    if (!(std::cin >> n) || n <= 0) return 0;
    for (int row = 1; row <= n; ++row) {
        for (int col = 1; col <= row; ++col) {
            char ch = (col % 2 == 1) ? 'O' : 'X';
            std::cout << ch << (col == row ? '\n' : ' ');
        }
    }
    return 0;
}

Notes: the col % 2 test gives O on odd columns and X on even ones; the conditional after the character avoids a trailing space. An alternate micro-optimization is to flip a char c = 'O' each column (c = (c == 'O') ? 'X' : 'O') instead of using %.

Recommended Answers

All 5 Replies

yeah i know how to do this actually
Sorry i couldn't give you some code that i have use to attempt this question. But i have no idea why I should get yoour a grade when i do all the work.
You're welcome in advance :)
(nested)

yeah i know how to do this actually
Sorry i couldn't give you some code that i have use to attempt this question. But i have no idea why I should get yoour a grade when i do all the work.
You're welcome in advance :)
(nested)

Sorry I didn't mention, its just a practice exercise. All our marked worked is done in class to prevent problems like the one you just implied :P

lol
well i dont think anyone here is going to give the code so i hope you're right
but you need to use nested for loops

lol
well i dont think anyone here is going to give the code so i hope you're right
but you need to use nested for loops

I don't really want someone to post a whole, that would take forever lol I just need some guidance.

Okay I've managed to do this but I need to know how to get it to print off the result in "X"'s and "O"'s

#include <iostream>
using namespace std;

int main(){
int n;
cout<<"Enter an integer"; 
cin>>n;
for(int row=1;row<=n;row++){
     for(int col=1;col<=row;col++);
		  cout<<n+1-row;
          
     cout<<endl;
}


system ("pause");
	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.