Group
Introduction to C++
Objective
1. Declare an integer variable to store the number entered by the user.
2. Prompt the user to input the number whose multiplication table will be generated.
3. Use a `for` loop to multiply the number from 1 to 10.
4. In each iteration, display the product in a formatted way.
5. Ensure the output is clear and readable for the user.
Print the multiplication table of a number.
Example C++ Exercise
Show C++ Code
#include <iostream> // Include the iostream library for input and output operations
using namespace std; // Use the standard namespace to avoid using std:: prefix
// Main function - starting point of the program
int main() {
int number; // Declare an integer variable to store the user input
// Ask the user to enter a number for which the multiplication table will be generated
cout << "Enter a number to print its multiplication table: ";
cin >> number; // Read the input number from the user
// Use a for loop to print the multiplication table from 1 to 10
for (int i = 1; i <= 10; ++i) {
// Display the current step of the multiplication table
cout << number << " x " << i << " = " << number * i << endl;
}
return 0; // Return 0 to indicate the program ended successfully
}
Output
Enter a number to print its multiplication table: 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50