Pyramid Calculations

Jan 1, 2013 at 3:49pm
Hi, i'm trying to make a program that does the required calculations to show the following:

       
       9*9+7=88
      98*9+6=888
     987*9+5=8888
    9876*9+4=88888
   98765*9+3=888888
  987654*9+2=8888888
 9876543*9+1=88888888
98765432*9+0=888888888


this is what i've made so far:

1
2
3
4
5
6
7
8
9
10
11
12
    int i=0,j=0,k=8,s=0;
    
    for(i=1;i<9;i++) {
      s=9;
      for (j=1;j<=i;j++) {
        printf("%d",s);
        s--;
        }   
      k--;

      printf("\n");
    }


it only prints the

9
98
987
9876
98765
987654
9876543
98765432


but it's impossible to calculate them, so if anyone got a solution..

Jan 1, 2013 at 4:28pm
You're printing one digit at a time. You need to calculate the first term by multiplying it by 10 and adding the third term + 1 each time thorugh the loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <cstdlib> 

int main (void)
{  int t1=9, t2=9, t3, x;
    
    for (t3=7; t3>=0; t3--) 
   {  x = t1 * t2 + t3;
       printf("%9d * %d + %d = %d\n", t1, t2, t3, x);
       t1 = t1 * 10 + (t3+1);
   }
   system ("pause");
}


Jan 1, 2013 at 4:33pm
1
2
3
4
5
6
7
for ( int i = 7, x = 0; i >= 0; i-- )
{
	const int N = 9;
	const int base = 10;
	x = base * x + N + i - 7;
	std::cout << std::setw( i ) << "" << x << '*' << N << '+' << i << '=' << x * N + i << std::endl;
}
Jan 1, 2013 at 4:34pm
The following is a small/simple equation to outline this.
a*b+c = d;

Observations:
a: Each digit value is one less than the preceding digit.
b: Always equal to 9
c: Always decremented by one per line
d: All digits are always 8. Each following line has one more digit than the previous.


Try to take it from here. I'd give you more help, but I'm not sure if you mean that you need to mathematically do the calculations, or if you just need to make a function which outputs the given text.
Jan 1, 2013 at 4:38pm
i have found it too ^^

1
2
3
4
5
6
7
8
9
int i=0, k=8, j=0, F=9, R=0;

for (i=8;i>=1;i--) {
    k--;
    for (j=k;j>=0;j--) { printf(" "); }
    R=F*9+k;
    printf("%d*9+%d=%d \n",F,k,R);
    F=F*10+i;
 }   



thank you guys :)
Last edited on Jan 1, 2013 at 4:41pm
Topic archived. No new replies allowed.