C++ Online Compiler
Example: Multiplication Table from 1 to 10 in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Multiplication Table from 1 to 10 #include <iostream> #include <iomanip> // Required for setw int main() { // Step 1: Print a header for the table std::cout << "Multiplication Table from 1 to 10:\n\n"; // Step 2: Print the header row (1 to 10) // Add extra space for the row labels std::cout << " |"; for (int col = 1; col <= 10; ++col) { std::cout << std::setw(4) << col; // setw(4) ensures consistent spacing } std::cout << "\n"; // Step 3: Print a separator line std::cout << "---|-----------------------------------------\n"; // Step 4: Use nested loops to generate the table for (int i = 1; i <= 10; ++i) { // Outer loop for the multiplier (rows) // Print the current row label (i) std::cout << std::setw(2) << i << " |"; for (int j = 1; j <= 10; ++j) { // Inner loop for the multiplicand (columns) // Calculate and print the product std::cout << std::setw(4) << (i * j); } std::cout << "\n"; // Move to the next line after each row } return 0; }
Output
Clear
ADVERTISEMENTS