Left-Shifted Odd Number Triangle in C++

What You’ll Learn
How to print an odd-number pattern that shifts left each row in C++:
13579, 3579, 579, 79, 9
We do this by stepping through odd numbers using += 2 in both the outer and inner loops.
⭐ Pattern Output
For odd numbers from 1 to 9, the pattern looks like this:
13579
3579
579
79
9Complete C++ Program
The outer loop chooses the starting odd number for each row (1, 3, 5, 7, 9). The inner loop prints from that start up to the maximum odd value (9).
#include <iostream>
using namespace std;
int main() {
int maxOdd = 9;
int i, j;
for (i = 1; i <= maxOdd; i += 2) {
for (j = i; j <= maxOdd; j += 2) {
cout << j;
}
cout << "\n";
}
return 0;
}🧠 How It Works
Choose the maximum odd number
int maxOdd = 9; sets the last number printed in the first row.
Outer loop (start value per row)
for (i = 1; i <= maxOdd; i += 2) visits only odd starts: 1, 3, 5, 7, 9.
Inner loop (print odd numbers)
for (j = i; j <= maxOdd; j += 2) prints odd numbers from the current start to maxOdd.
New line
cout << "\\n"; moves to the next row after printing each line.
Left-shifted odd triangle
Each row starts later, so the printed sequence shortens by one odd number each line.
Variation — User Input Version
Let the user enter the maximum number. If the input is even, we reduce it by 1 so the pattern ends on an odd number.
#include <iostream>
using namespace std;
int main() {
int maxOdd;
int i, j;
cout << "Enter the maximum number: ";
cin >> maxOdd;
if (!cin || maxOdd <= 0) return 0;
if (maxOdd % 2 == 0) maxOdd -= 1;
for (i = 1; i <= maxOdd; i += 2) {
for (j = i; j <= maxOdd; j += 2) {
cout << j;
}
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Print spaces between numbers for readability:
cout << j << ' '; - Start from a different first odd number (like 3) for a shifted first row
- Increase the maximum odd number (11, 13, 15...) for a bigger triangle
- Right-align by printing leading spaces before each row
- Print odd squares or other sequences instead of consecutive odds
Avoid
- Using an even maximum without converting it to odd (the last odd would be skipped)
- Forgetting
+= 2(you’ll print even numbers too) - Using
endlinside loops (unnecessary flushing) - Skipping the newline after each row
Key Takeaways
Use += 2 to iterate through only odd numbers.
The outer loop sets the row’s starting value (1, 3, 5, ...).
The inner loop prints from the start odd up to the maximum odd value.
Each row prints fewer values, creating a left-shifted triangle.
❓ Frequently Asked Questions
maxOdd to 11, 13, 15, etc. If the user enters an even number, convert it to odd with maxOdd--.Explore More C++ Number Patterns!
Odd-number patterns are great practice for stepping through sequences and controlling loop ranges.
If you want only odd numbers, you don’t need a conditional check—just start from 1 and increment by 2.
12 people found this page helpful
