printf

hey guys could somebody help me?
i want to create a simple program which prints this , using printf function:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

how can i script it? :s
Try something like:

printf("\t\t\t 1\n\t\t\t 121\n\t\t\t 12321\n\t\t\t 1234321\n\t\t\t123454321\n");

But i would recommend for you to use cout instead of printf, are you using C or C++?
Last edited on
hmmm i know bout that ... i meant doing that by using a special formula . i use c++ and i use borland turbo c++ compiler :)
something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>

int main()
{
	int a;
   int i;
   int j;
   for(j=1;j<=5;j++)
   {
   	for(i=1;i<=5;i++)
   	{
   		printf("%4i", i++);
   	}
   cout<<"\n";
	}
	getch();
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
#include <cstdio>

int main() {
   printf("    1    \n");
   printf("   121   \n");
   printf("  12321  \n");
   printf(" 1234321 \n");
   printf("123454321");

   return 0;
}
    1
   121
  12321
 1234321
123454321
Last edited on
use following algo (very rough.. but will give u idea)

loop for every line
{
print space 5-i times
loop to print no. from 1 to i
loop to print no. from i-1 to 1
print space 5-i times
}

hmmm Vgoel38 could you explain a lil bit more? im 16 and im a newbie in c++ ^^
Look, making the program to cout that with a formula using for loops would be much harder and use much more code than simply printing the numbers in order by themselves.

printf("\t\t\t 1\n\t\t\t 121\n\t\t\t 12321\n\t\t\t 1234321\n\t\t\t123454321\n");
is a good option, but if you want to use spaces instead of \t or one printf each line then it's your decision :)
Last edited on
@samrux : lol i know but its what our teacher wants! :D no way! :D lol
You have to use logical thinking here. Mainly learn how to do it step by step. First, start with drawing the numbers using a for loop:
1
12
123
1234
12345


Then a for loop for drawing the back end
1
121
12321
1234321
123454321


Then a for loop for drawing the spaces
    1
   121
  12321
 1234321
123454321


You come up with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <cstdio>

int main() {
   for (int i = 1; i <= 5; i ++) {
      // Draw spaces
      for (int j = 5; j > i; j --)
         printf(" ");
      // Display first set of numbers
      for (int j = 1; j < i; j ++)
         printf("%i", j);
      // Display last set of numbers
      for (int j = i; j >= 1; j --)
         printf("%i", j);
      // End the line
      printf("\n");
   }

   return 0;
}
oh thanks @Volatile Pulse :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
	
int rows = 7; //number of rows you want to print	
int mid = 2 * (rows - 1) + 1  / 2; //determine the midpoint of the pyramid

for (int i = 0; i < rows; i++) {
               int midValue = i + 1;  //the middle value for the row
	int j = 1;

	printf("%*c", mid);  //pad proper number of spaces

	for (; j <= midValue; j++) { //print upto and including midvalue
		printf("%d", j);
	}

	j -= 2;  //need to subtract 2 because j is now 2 more than the mid value

	for (; j >= 1; j--) { //print from 1 less than mid value to 1
		printf("%d", j);
	}

	printf("\n");

	mid--;
}
Topic archived. No new replies allowed.