我想打印1到10个数字,如果我输入1,然后1到100,如果2,1到1000为3

时间:2014-07-10 04:10:19

标签: c

void main()
{
    int r,y,j;
    clrscr();
    printf("Enter your number choice please:");
    scanf("%d",&y);
    r = pow(10,y);
    printf("the multiplier is: %d",r);
    for(j=1;j<r;j++)
    {
        printf("\n The number is %d",j);
        r=r-1;
    }
    getch();
}

输出是:

Enter your number choice please:1
"The multiplier is:10
 The number is 1
 The number is 2
 The number is 3
 The number is 4
 The number is 5

我的输出没有到10.输出逻辑中的错误是什么?

4 个答案:

答案 0 :(得分:7)

您需要从r=r-1;循环中移除for

for(j=1 ; j<=r ; j++) // notice j<=r, instead of j<r
{
    printf("\n The number is %d",j);
}

如果您将其保留,则可以使用j减少您希望获得的最大值。

答案 1 :(得分:2)

for(j = 1; j <= r; j++)  // Note the '=' sign to include r.
{
    printf("\n The number is %d",j);
    // r=r-1;  // Remove this line
}

答案 2 :(得分:0)

根据我的理解,您可以使用c library pow()数学函数来执行此操作:

//scan user input into int variable userInput 

for(int i=1; i <= pow(10, userInput);i++){
     printf("%d ,", i);
}

答案 3 :(得分:0)

代码中的两个修改:

for(j=1;j<r;j++)   ----> j <=r
{
    printf("\n The number is %d",j);
    r=r-1;   -->comment this line
}