Printing a “triangle” of asterisks

匿名 (未验证) 提交于 2019-12-03 01:45:01

问题:

The program I am supposed to write is supposed to print the triangle in the following manner:

If the number of rows is 2:

       *       *** 

If the number of rows is 3:

     *     * *    ***** 

However, the following code that I did prints the correct amount of stars for the last line, but I am not so sure how I would print the space and the newline. My code for printing the bottom level stars is the following:

  void tri_func(num) {       int row; int c=1;      int j;     for ( row = 1 ; row 

回答1:

Well, if you have n lines, the first line should contain n-1 spaces and a *. The ith line (1n-i spaces, a *, 2i-3 spaces and another *, and the last line should contain 2n-1 *s. You can easily do it using loops. To print a space, use printf(" ");, and remember to print a \n in the end of every line.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!