Using nested for loops I'm suppose to create a pyramid that looks like this:
Code:
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
I have everything working except the spacing between the numbers. Notice that between the single digit numbers like 1 and 2 there are three spaces, but between double digit numbers like 32 and 64 there are only two spaces, etc. Any suggestions on how to do this type of spacing, here is my code so far.
Code:
/*
My name is Jack Trocinski
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int row; // row number
int space; // white space
int column; // column number
int n; // degree of power
int i; // used in for loop
int a; // result obtained by pow
for (row = 0; row <= 7; ++row) {
column = (7 - row) * 4;
n = 0;
for (space = column; space > 0; --space) {
printf(" ");
}
for (i = 0; i <= row; ++i) {
a = pow(2, n);
printf("%d", a);
++n;
}
--n;
--n;
for (i = 0; i < row; ++i) {
a = pow(2, n);
printf("%d", a);
--n;
}
printf("\n");
}
system("pause");
return 0;
}