I'm trying to create an hourglass, It seems as if my spacing for the reverse for-loop is messing up, and I don't know how I would remove the extra 5 without messing up the for-loop.
My code
#include <stdio.h>
#include <math.h>
int main(){
long long number;
printf("Enter a long number\n");
printf("Enter your number = ");
scanf("%lld", &number);
for(int j=1; j<= (log10(number)+1); j++){
for(int i=1; i<= (log10(number)+1) - (j-1); i++){
printf("%d ", (number%((int)pow(10, i)))/(int)pow(10, i-1));
}
printf("\n");
printf("%*s", 2*j-1, " ");
}
for(int j=(log10(number)+1); j>=1; j--){
for(int i=(log10(number)+1)-(j-1); i>=1 ; i--){
printf("%d ", (number%((int)pow(10, i)))/(int)pow(10, i-1));
}
printf("\n");
printf("%*s", 2*j-1, " ");
}
return 0;
}
My output:
Enter a long number
Enter your number = 12345
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
5
4 5
3 4 5
2 3 4 5
1 2 3 4 5
What would be the best way for removing the extra 5 on top, and what did I do wrong with the spacing?
What I'm trying to output:
Enter a long number
Enter your number = 12345
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
4 5
3 4 5
2 3 4 5
1 2 3 4 5
You should indent your rows before writing the numbers. For the second loop, skip the first row.
Try this code:
Output