# Recursive function to print a right triangle

Page 1 of 1

## 3 Replies - 893 Views - Last Post: 22 April 2016 - 05:09 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=392334&amp;s=be93685036821f64fa5d43905b13907f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 cboob

Reputation: 0
• Posts: 3
• Joined: 22-April 16

# Recursive function to print a right triangle

Posted 22 April 2016 - 11:04 AM

Hey! Can anyone help me with this code? *** must be in c language ***

Write a recursive function which takes the side of the triangle, and the fillCharacter and displays it. For example, if the side of the triangle is 5 and fillCharacter is ‘@”, it should display as follows:

@

@@

@@@

@@@@

@@@@@

The function header should be: void displayTriangle(int side, char fillCharacter); The main program MUST be as follows (and MUST NOT be changed at all):

#include <stdio.h>

void displayTriangle(int side, char fillCharacter);
int main(void) {
int side;
char theCharacter;

fflush(stdout);
scanf("%c", &theCharacter);

printf("Please enter the side of the triangle: ");
fflush(stdout);

scanf("%d", &side);

displayTriangle(side, theCharacter);

}

void displayTriangle(int side, char fillCharacter) {}

So the code containing the recursive function would go in the last set of curly brackets { }

Is This A Good Question/Topic? 0

## Replies To: Recursive function to print a right triangle

### #2 kaa

Reputation: 225
• Posts: 805
• Joined: 15-April 11

## Re: Recursive function to print a right triangle

Posted 22 April 2016 - 11:21 AM

... pretty much the same answer as the one I gave to your other thread applies here.

### #3 vividexstance

• Tiocfaidh ár lá

Reputation: 792
• Posts: 2,873
• Joined: 31-December 10

## Re: Recursive function to print a right triangle

Posted 22 April 2016 - 11:26 AM

I'll let another mod decide if this thread should be merged with the other, but I can say they both deal with recursion, so it might be a good idea.

To the OP, are you having a problem with recursion? If so, what about it is confusing you?

### #4 CordovaCode

Reputation: 8
• Posts: 125
• Joined: 14-March 16

## Re: Recursive function to print a right triangle

Posted 22 April 2016 - 05:09 PM

The following is not C code, but it will show you how recursion works in a very basic way. You should be able to figure out how to use most of the concepts to solve your problem.

#include <iostream>

// function prototypes, show 2 ways to use recursion
void tails_recursive(int, char);

int main() {
// create and initialize variable
int width(0);
char fill('@');

width = 5;
// call the functions to see the differences
tails_recursive(width, fill);
}

// the actual functions

void heads_recursive(int width, char fill) {
// create a condition that will stop the recursion
if(width > 0)
{
// the recursive call
std::cout << width << " : " << fill << std::endl;
}
}

void tails_recursive(int width, char fill) {
// create a condition that will stop the recursion
if(width > 0)
{
std::cout << width << " : " << fill << std::endl;
// the recursive call
tails_recursive(width - 1, fill);
}
}

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }