Last active
August 29, 2015 14:02
-
-
Save bvancil/6a391cb957274ec2747a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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