Skip to content

Instantly share code, notes, and snippets.

@bvancil
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bvancil/6a391cb957274ec2747a to your computer and use it in GitHub Desktop.
Save bvancil/6a391cb957274ec2747a to your computer and use it in GitHub Desktop.
/*
Multiplication Table
*/
#include <iostream>
#include <iomanip> // for setw
#include <cmath>
using namespace std;
int main()
{
int largestHorizontalFactor = 20;
int largestVerticalFactor = 50;
int largestProductInColumn;
int largestNumberOfDigits;
for(int verticalFactor=1; verticalFactor<=largestVerticalFactor; verticalFactor++) {
for(int horizontalFactor=1; horizontalFactor<=largestHorizontalFactor; horizontalFactor++) {
// The first two lines are just for calculating spacing.
largestProductInColumn = largestVerticalFactor * horizontalFactor;
largestNumberOfDigits = int(floor(log10(largestProductInColumn)))+1;
// This prints the product. "setw" sets the width of the output and right-justifies it.
cout << setw(largestNumberOfDigits+1) << (verticalFactor*horizontalFactor);
}
cout << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment