简体   繁体   English

如何在C中打印0个直角三角形?

[英]how can i print 0 right triangle in c?

Pattern : 模式:

0
00
000
0000
00000
000000

#include<stdio.h>
int main()
{
 int value = 6;
 for(int i = 1; i < value+1; i++);
 {
  for(int j = 1; j <= i; j++);
   printf("0");
  printf("\n");
 }
 return 0;
}

And also the 1st row consists of 1 0's, the 2nd row 2 0's, and so on, until the Nth row, which consists of N zeroes. 第一行也由1 0组成,第二行由2 0组成,依此类推,直到第N行由N个零组成。 like below pattern. 像下面的图案。

0
00
000
0000
00000
000000

Note: I have to give positive integer N and print N rows. 注意:我必须给出正整数N并打印N行。

MY ERRORS: 我的错误:

Program: In function 'main':
Program:7:21: error: 'i' undeclared (first use in this function)
Program:7:21: note: each undeclared identifier is reported only once for each function it appears in

You have a ; 你有一个; at the end of both of your for statements, this closes the for loop immediately, which means the i variable is then out of scope and unavailable for use. 在两个for语句的末尾,这将立即关闭for循环,这意味着i变量超出范围,无法使用。

In C don't declare, variables inside ( ) . 在C中不要声明( )变量。

int i;  // declare globally
//  do your stuff
for(i = 1; i < value+1; i++)

If you put ; 如果你放; colon after loop statement, the loop blocks there itself till the condition fails. 冒号after loop语句,循环本身会在那里阻塞,直到条件失败为止。 it will execute the next line. 它将执行下一行。 So remove the ; 因此删除; after for loops. for循环之后。

Try this- 尝试这个-

#include<stdio.h>
int main()
{
        int value,i,j;
        printf("Enter the number of rows\n");
        scanf("%d",&value);
        printf("-----------------\n");
        for(i = 1; i < value+1; i++)
        {
                for(j = 1; j <= i; j++)
                printf("0");
                printf("\n");
        }
        return 0;
}

First remove the semicolon & remember for control structure you can not put semicolon at the end of it, so remove semicolon , 首先删除分号并记住控制结构,您不能在其末尾放置分号,因此请删除分号,

Seceondly, if you are still getting error put your int i and j outside of for loop, 第二,如果您仍然遇到错误,请将int和int放在for循环之外,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM