English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Can someone give me the C code that can print a triangle reserively
for example
*
**
***
****
*****

2006-06-26 16:47:31 · 4 answers · asked by macausite 2 in Computers & Internet Programming & Design

4 answers

Thats a recursive program for you.
And I put an arbitrary limit of 9 there. You can change that for yourself.

#include

void triangle(char ch, int count) {
int temp=0;
if (count==1) {
printf("%c\n", ch);
return;
}
triangle(ch, count-1);
for(;temp printf("%c", ch);
}
printf("\n");
}

int main() {
char ch='*';
int count=0;
while((count<=0)||(count>=10)) {
printf("Enter the size of triangle (less than 10)\n");
scanf("%d", &count);
}
triangle(ch, count);
}

2006-06-26 18:45:37 · answer #1 · answered by Neil 5 · 3 1

#include

void triangle(int repetition, int length)
{
if (repetition == 0) return;
for (int count = 0; count < length; count++)
{
printf("*");
}
printf("\n");
triangle(repetition - 1, length + 1);
}

void main()
{
triangle(5, 1);
}

2006-06-28 14:09:04 · answer #2 · answered by husseinfareed 2 · 0 1

#include // standard input/output
void main(void) {
 int i, j;
 for(i = 1; i <= 5; i ++) {
  for(j = 1; j <= i; j ++) {
   printf("*");
  }
  printf("\n");}
 }
}

2006-06-26 18:23:58 · answer #3 · answered by stonemason_2 1 · 0 0

hello i am a C++ programmer
this is C++
#include
void main()
{
cout <<"*\n";
cout <<"**\n";
cout <<"***\n";
cout <<"****\n";
// and so on until you want (just keep adding *)
return 0;
}

2006-06-26 16:53:03 · answer #4 · answered by Anonymous · 0 0

fedest.com, questions and answers