print 5 table in c++
#include <iostream>
int main() {
int num = 5;
for (int i = 1; i <= 10; i++) {
std::cout << num << " " << i << " = " << num i << std::endl;
}
return 0;
}
The above code snippet demonstrates how to print the 5 times table in C++. Let's go through the steps involved:
Step 1: Including the necessary header file
#include <iostream>
This line includes the <iostream>
header file, which provides input and output functionalities in C++.
Step 2: Defining the main function
int main() {
This line defines the main function, which serves as the entry point of the program.
Step 3: Declaring and initializing the num variable
int num = 5;
This line declares an integer variable called num
and assigns it a value of 5. This variable will be used to represent the number for which the times table is printed.
Step 4: Starting the for loop
for (int i = 1; i <= 10; i++) {
This line starts a for loop that iterates from 1 to 10. The loop variable i
is initialized to 1, and the loop continues as long as i
is less than or equal to 10. On each iteration, i
is incremented by 1.
Step 5: Printing the times table
std::cout << num << " " << i << " = " << num i << std::endl;
This line uses the std::cout
object to print the multiplication statement for each iteration of the loop. The value of num
represents the number for which the times table is printed, i
represents the current multiplication factor, and num * i
calculates the product. The std::endl
is used to insert a newline character after each line of the times table.
Step 6: Ending the for loop
}
This line marks the end of the for loop.
Step 7: Returning from the main function
return 0;
This line returns 0 from the main function, indicating successful termination of the program.
By executing the above code, you will see the 5 times table printed on the console:
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50