Multiplication Table

By Sonny on Apr 24, 2010

A very basic multiplication table former. It asks you to enter a number, then displays a 5x5 grid-like multiplication table with the base number you entered. It starts from 1. Useful for teaching younger kids their times tables, or people who aren't so good with multiplication.
You can use it from the command line, I would recommend maximizing your screen when using big numbers.
Command line usage:
Windows: ()
Linux: (<filepath)>)./

Note: the grid may break. I could correct it, but as this isn't a big code, it would be pointless. It's legible, that's all that matters.

#include <iostream>
#include <stdlib.h>

using std::cout;
using std::cin;
using std::endl;

int main(int argc, char *argv[])
{
  const int rows = 5, cols = 5;
  int n, s = 1;

  if (argc == 2)
  {
    char *d;
    n = strtol(argv[1],&d,10);
  }
  else {
    cout << "Enter a number: ";
    cin >> n;
    cout << "\n";
  }
  for (int i = 1;i <= cols;i++)
  {
    for (int c = 1; c <= rows;s++)
    {
      cout << n << " x " << s << " = " << n*s << " \t";
      c++;
    }
    cout << endl;
  }
  return 0;
}

Comments

Sign in to comment.
Hawkee   -  Apr 24, 2010

There we go, much better!

 Respond  
Sonny   -  Apr 24, 2010

Fixed. The problem was I defined s as a const, when in fact I needed to increment it in the "rows" for loop.

 Respond  
Hawkee   -  Apr 24, 2010

I got a compile-time error:

mult.cpp: In function ‘int main(int, char**)’:
mult.cpp:26: error: increment of read-only variable ‘s’

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.