Print table of an integer number
Printing the table of an integer number like 5×1=5, 5×2=10, 5×3=15 and so on.
Learning Objectives
- Learning to use for loop indexes as part of mathematical expressions.
- Printing complex chain of output inside a for loop.
Source Code
|
|
Run Output
Code Understanding
for(int i=1;i<=10;i++)
Here i will denote the multiplier of the number. For table of a number we need to increment this upto 10. Exactly this operation is being performed by the for loop instructions.
cout<<n<<“x”<<i<<“=”<<n*i<<endl;
This is the table output line. First this outputs the number, followed by the x sign as string literal, then the multiplier value in i (which is increasing with the every loop iteration), then = string literal is being printed, then the multiplied value is printed using the expression n*i and then newline (endl) is printed.
Notes
- The output can be bettered with the use of iomanip.h library for output formatting.
- Similar code can be put in an envelope of do-while loop for continously taking the user input and printing the table of any number given as input, unless user inputs 0.
Common Errors
- careful construction of cout chain is important for proper display of the number table.
Suggested Filename(s): table-of-num.cpp
Created: 28-Dec-2017 18:18 | Updated: 15-Sep-2018 12:05|
