C++ Online Compiler
Example: Floyd's Triangle Generator in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Floyd's Triangle Generator #include <iostream> int main() { // Step 1: Declare variables for number of rows and the counter. int numRows; int currentNumber = 1; // Step 2: Prompt user for input and read the number of rows. std::cout << "Enter the number of rows for Floyd's Triangle: "; std::cin >> numRows; // Step 3: Use an outer loop to iterate through each row. // The outer loop runs from row 1 up to numRows. for (int i = 1; i <= numRows; ++i) { // Step 4: Use an inner loop to print numbers for the current row. // The inner loop runs 'i' times for the i-th row. for (int j = 1; j <= i; ++j) { // Step 5: Print the current number followed by a space. std::cout << currentNumber << " "; // Step 6: Increment the current number for the next print. currentNumber++; } // Step 7: Move to the next line after printing all numbers for the current row. std::cout << std::endl; } return 0; }
Output
Clear
ADVERTISEMENTS