Reputation: 488
I want to develop a program which prints a triangle shown below:
1
A B
1 2 3
A B C D
Using a for loop in C. Any idea how come up with program?
Upvotes: 0
Views: 1855
Reputation: 2024
f(int n)
{
int i , j ;
for( i = 1 ; i <= n ; i ++ )
{
j = 1 ;
while( j <= (n-i) ) { printf(" "); j++ ;}
j = 0 ;
while( j <= i )
{
if( i % 2 != 0 )
printf("%d ", j );
else
printf("%c ", j + 'A' );
printf("\n");
j ++ ;
}
}
}
Now if someone can do it in time < O(n2) That is welcome :)
Upvotes: -1
Reputation: 6918
char * pie = " 1\n A B\n 1 2 3\n A B C D\n";
for (i=0;i<1;i++) printf("%s", pie);
Upvotes: 0
Reputation: 36071
If you want to print n
lines in total:
n-1
spaces in front of itn-2
spaces in front of themn-3
spaces in front of themi
-th line consists of ___
chars and ___
spaces in front of them (please fill in missing fieldsHow to determine what to print:
Please formulate a rule that determines which lines contain which signs:
_______________________________________________________________________________
_______________________________________________________________________________
You can print numbers with: printf("%d", number)
. You can print chars with printf("%c",char)
.
You can do addition on characters as well: 'A' + 2
yields 'C'
.
Now it should be no real problem to program the program you are looking for.
Upvotes: 4