Guest User

Untitled

a guest
Dec 11th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. /* Program allows the user to input the length and width for
  2. a multiplication table and then displays one. However, the user cannot
  3. input more than 15 rows. */
  4.  
  5. #include <iostream> // cout, cin, endl
  6. #include <iomanip> // setw, setprecision, align right
  7. #include <cstdlib> // system pause
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12. // declare variables and constants
  13. int rows; // horizontal (width) of multiple table
  14. int cols; // vertial (length) of multiplication table
  15.  
  16. // Prompt user for input
  17. // accept input from user. Accepts the width first and then length.
  18. cout << "Input the length and then the width of the multiplication table" << endl;
  19. do{
  20. cout << "Table cannot be bigger than 15 x 15 or less than 1 x 1. " << endl;
  21. cin >> cols >> rows;
  22.  
  23. if(cols < 1 || rows < 1)
  24. {
  25. cout << "Error: cannot enter 0 units for table length or width." << endl;
  26. }
  27. } while(cols > 15 || rows > 15);
  28.  
  29. // Output the table using for loops, one nested in the other.
  30.  
  31. cout << right; // Aligns everything to the right.
  32. for (int i = 1; i <= rows; i++)
  33. {
  34. for(int j = 1; j <= cols; j++)
  35. {
  36. cout << setw(4) << i * j; // sets the width of 4 between each i*j
  37. }
  38. cout << endl; // After each iteration of the nested loop, it will go to a new line.
  39. }
  40.  
  41.  
  42. // Exit the program
  43. system("pause");
  44. return 0;
  45. }
Add Comment
Please, Sign In to add comment