C语言 如何在c程序中使用循环制作圣诞树

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26204277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:25:51  来源:igfitidea点击:

how to make a christmas tree using loop in c program

c

提问by rhia lorica

I'm a freshmen student and we have an activity in intro pro.. We were tasked to create a Christmas tree using a loop...

我是一名新生,我们有一个介绍专业的活动......我们的任务是使用循环创建一棵圣诞树......

I have my code here:

我的代码在这里:

#include<stdio.h>
int main ()
{
    int rows,a,b,space;
    clrscr();
    printf("Enter a number of rows:");
    scanf("%d",&rows);
    space=rows-1
    for(b=space;b>=1;b--)
    {
        for(a=rows;a>=1;a--)
            space--;
        printf("");
        for(a=2*(rows-b)-1;a>=1;a--)
            printf("*",a);
        printf("\n");
        space = space-1;
    }
    getche();
    return 0;
}

This code was given to us by our professor... the program runs, but the output is wrong. Can you help me?

这段代码是我们教授给我们的……程序运行了,但是输出错误。你能帮助我吗?

when i run this program, the output was like this:

当我运行这个程序时,输出是这样的:

*
***
*****
******
*******

回答by saadtaame

You have to find a pattern. Say you want a tree with nrows. Last row is going to have 2n-1stars. Row before it will have 2n-3and so on. To print a row, first you print a number of spaces, then a number of stars. For last row, you print 0spaces and 2n-1stars. For row before it, you print 1space and 2n-3stars and so on.

你必须找到一个模式。假设您想要一棵带有n行的树。最后一排会有2n-1星星。前排它会有2n-3等等。要打印一行,首先打印一些空格,然后打印一些星星。对于最后一行,您打印0空格和2n-1星星。对于它之前的行,你打印1空间和2n-3星星等等。

for(int i = 0; i < n; i++)
{   for(int j = i + 1; j < n; j++)
       printf(" ");
    for(int j = 0; j <= 2*i; j++)
       printf("*");
    if(i < n - 1) puts("");
}

回答by user4790278

This is the shortest and simplest solution for your question:

这是您问题的最短和最简单的解决方案:

#include<stdio.h>
#include<conio.h>

void main(){
    int count;
    int i,j;
    printf("enter the numbers of line");
    scanf("%d",&count);
    for(i=1;i<=count;i++){
        for(j=1;j<=i;j++){
            printf("*");
        }
        printf("\n");
    }
    getch();
}

回答by pietertje

You forgot a space between "".

您忘记了“”之间的空格。

    for(a=rows;a>=1;a--)
        space--;
    printf("");

should be

应该

    for(a=rows;a>=1;a--)
        space--;
    printf(" ");

回答by Rizier123

The Code is a little bit to messed up for me, but this should work:

代码对我来说有点搞砸了,但这应该有效:

#include<stdio.h>

int main() {

    /*Variables*/
    int rows, starNumber, spaceNumber;
    int rowCount, spaceCount, starCount, treeTrunkCount, treeTrunkSpaceCount;

    printf("Enter Rows:\n>");
    scanf("%d",&rows);

    for(rowCount = 1; rowCount <= rows; rowCount++) {
        starNumber = rowCount * 2 - 1;
        spaceNumber = rowCount + rows - starNumber;

        for(spaceCount = 0; spaceCount < spaceNumber; spaceCount++)
            printf(" ");

        for(starCount = 0; starCount < starNumber; starCount++)
            printf("%c",'*');

        printf("\n");
    }

    for(treeTrunkCount = 0; treeTrunkCount < 3; treeTrunkCount++) {
        for(treeTrunkSpaceCount = 0; treeTrunkSpaceCount < (rows * 2 + 1)/2; treeTrunkSpaceCount++)
            printf(" ");

        printf("%c\n",'*');
    }
}

回答by Varun Varunesh

This is the simplest solution to your program..

这是对您的程序最简单的解决方案..

#include <stdio.h>

int main()
{
  int i=-1,j=0,rows;

  printf("Enter Rows:\n");
  scanf("%d",&rows);

  while(j++<rows) // Moving pointer for the first '*'
  {
    printf(" ");
  }
  printf("*"); // This prints the first '*'
  while(++i<rows)
  {
      for(j=-2;++j<rows-i;) // This loop will print Spaces before '*' on each row
          printf(" ");
      for(j=0;++j<2*i;) // This loop will print * on each row
      {
          printf("*");
      }
      printf("\n"); // This printf will take you to the next Line
    }
}

回答by Byamungu Kabiraba

#include <stdio.h>


int main() {
    int n = 50;

    for (int i = 0; i <= n; ++i) {

        for (int k = i; k < n; ++k)
            printf(" ");

        for (int j = 0; j < i; ++j)
            printf("*");

        for (int j = 1; j < i; ++j)
            printf("*");

        printf("\n");
    }

    for (int l = 1; l < n/2; ++l) {
        for (int i = 1; i < n; ++i)
            printf(" ");

        printf("[|]\n");

    }

    return 0;
}

回答by santanu Dhabal

#include<stdio.h>
main()
{
    int n,i, j, space=1;
    printf("Enter the number of rows: ");
    scanf("%d",&n);
    space=n-1;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=space;j++)
       {
            printf(" ");
        }
        space--;
        for(j=1;j<=2*i-1;j++)
        {
            printf("*");
        }
        printf("\n");
    }
    for(i=1;i<=n-3;i++)
    {
        for(j=1;j<=10;j++)
        {
            printf(" ");
        }
         for(j=1;j<=1;j++)
        {
           printf("*");
        }
         for(j=1;j<=1;j++)
        {
            printf("*");
        }
         for(j=1;j<=1;j++)
        {
            printf("*");
        }
        printf("\n");
    }
}

回答by aditya jillellamudi

#include<stdio.h>
int main()
{
    int i,j,k,l=1,a,b;
    for(i=8;i>=0;i--)
        {
            for(j=0;j<=i;j++)
                {
                printf(" ");
                }
                k=0 ;
            while(k<l)
                {
                printf("*");
                k=k+1;
                }
            l=l+2;
            printf("\n");
        }
    i=8;
    for(b=0;b<=3;b++)
        {
        for(a=0;a<=i-1;a++)
            {
            printf(" ");
            }
        printf("***"); 
        printf("\n");
        }
}