简体   繁体   English

为什么我的输出显示两倍于我输入的限制?

[英]Why is my output showing twice the limit I have entered?

Write a C program to print all even numbers between 1 to 100. - using while loop编写一个 C 程序来打印 1 到 100 之间的所有偶数。 - 使用 while 循环

#include<stdio.h>

int main(int argc, char const *argv[])
{
    int a=0,Even,Go;

    printf("Enter the Value:\n");
    scanf("%d",&Go);
    
    while(a<=Go)
    {
        Even=a*2;
        printf("%d\t",Even);
        a++;
    }


    return 0;
}

OutPut:输出:

Enter the Value:
20
0       2       4       6       8       10      12      14      16      18      20      22      24      26       28      30      32      34      36      38      40      %     

As pointed out in the comments, your while condition is not based on the numbers that you're printing.正如评论中所指出的,您的while条件不是基于您正在打印的数字。 You're testing the number and then doubling it when printing.您正在测试数字,然后在打印时将其加倍。

Instead, you can simply increment by 2 so you test the same numbers you're printing.相反,您可以简单地增加 2,以便测试您正在打印的相同数字。

Since the numbers should be between 1 and Go , you should initialize a to 2 rather than 0 .由于数字应该介于 1 和Go之间,因此您应该将a初始化为2而不是0

#include<stdio.h>

int main(int argc, char const *argv[])
{
    int a=2,Even,Go;

    printf("Enter the Value:\n");
    scanf("%d",&Go);
    
    while(a<=Go)
    {
        printf("%d\t",a);
        a += 2;
    }

    return 0;
}

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

相关问题 我的程序没有 output 我输入的第一个值在我的数组中搜索。 为什么? - My program doesn't output the first value I entered to search in my array. Why? 为什么我的程序的output 0.000000 应该是我在提示时输入的值。 下面是代码 - why is the output of my program 0.000000 when it should be the value i entered when prompted. below is the code 为什么我的代码块上没有显示输出? - Why there is no output showing on my codeblocks? 我已经输入glutPostRedisplay(); 但是它不刷新窗口和/或输出? - I have entered glutPostRedisplay(); but it does not refresh the window and/or the output? 为什么我的输出数字不会返回输入的输入? - Why is it that my output numbers do not return the inputs entered? 我必须输入两次才能让我的 getchar() 工作 - I have to enter twice for my getchar() to work 为什么我必须两次打“ Enter”键? - Why do I have to hit “Enter” Twice? 为什么我的程序没有显示任何 output? 0 个错误,0 个警告但没有 output? 我正在使用 Dev C++ 编译器 - Why is my program not showing any output? 0 errors, 0 warnings but no output? I'm using Dev C++ compiler 为什么 %d 在我的 c 程序 output 中显示 0? - Why %d is showing 0 in my c program output? 为什么我的 append 函数没有显示输出? - Why my append function is not showing output?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM