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;
}
最佳答案
你很接近。你有 pre
和 endnum
。您只想从 pre
循环到 endnum
(含),然后打印出它们中的每一个。
如果愿意,您可以使用 while
循环,但对我来说,这种情况更适合使用 for
循环。像这样的东西:
for (num = endnum - pre; num <= endnum; ++num)
{
printf("%d ", num);
}
其中 num
预先声明为 int
。
关于c - 数字在用户输入的数字之前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33116176/