Detailed Print – CSKC
Sign In

Search

siteicon
Code Learning#CPP#1992 Back

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

		 

Click to open popup

Run Output

Print a table of : 13
13x1=13
13x2=26
13x3=39
13x4=52
13x5=65
13x6=78
13x7=91
13x8=104
13x9=117
13x10=130

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