c - 数字在用户输入的数字之前

标签 c

Ask user to enter a random number between 1 and 100. Then ask how many numbers s/he wants to display that precedes first number s/he enters.

if user enter 9 and wants 3 numbers that precedes 9, your program should display this:

6 7 8 9

我做不完

#include <stdio.h>
#include <stdlib.h>


int main()
{
   int endnum, pre;


    printf("Enter a random number between 1 and 100: ");
    scanf("%d", &endnum);

    printf("how many numbers he wants to display that precedes first number you entered: ");
    scanf("%d", &pre);

    num = endnum - pre;
    printf (%d, num+1)
    num = num + 1
    while (num <= endnum)


    return 0;
}

最佳答案

你很接近。你有 preendnum。您只想从 pre 循环到 endnum(含),然后打印出它们中的每一个。

如果愿意,您可以使用 while 循环,但对我来说,这种情况更适合使用 for 循环。像这样的东西:

for (num = endnum - pre; num <= endnum; ++num)
{
    printf("%d ", num);
}

其中 num 预先声明为 int

关于c - 数字在用户输入的数字之前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33116176/

相关文章:

c - 从函数中为外部变量赋值

c - 尽可能快地在C中提取两个相似(或不同)字符串之间的字符串

c - 采样率和可变长度输入样本与固定大小的 FFT 输入有何关系?

c - 是否可以像普通数组一样获取函数指针数组的大小?

将 8 字节 char 数组转换为 long

c - 函数尾声何时执行?

c - 自由函数激活断点

c - 用C语言读取dat文件

c++ - mbed 中的 Hello World MQTT 程序

c - 如何按顺序显示平衡二叉树的下一个元素? (为了)