Last active
February 19, 2024 11:31
-
-
Save GReturn/2e3e3f9500b32398cf1bb2503c24124e to your computer and use it in GitHub Desktop.
Print a pyramid - Recursive Implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void printSpaces(int spaceIndex) | |
{ | |
if(spaceIndex == 0) return; | |
printf(" "); | |
return printSpaces(spaceIndex-1); | |
} | |
void printStars(int starIndex) | |
{ | |
if(starIndex == 0) return; | |
printf("*"); | |
return printStars(starIndex-1); | |
} | |
void printRow(int row, int starCount) | |
{ | |
if(row==0) return; | |
printSpaces(row-1); | |
printStars(starCount); | |
printf("\n"); | |
return printRow(row-1, starCount+2); | |
} | |
void printPyramid(int height) | |
{ | |
return printRow(height, 1); | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
printPyramid(9); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment