For asking the user a question, you can use something like printf
or puts
.
To request numbers from the user, scanf
is probably the best approach for the level you’re working at.
By way of example, here’s a complete program that asks the user for a number then gives them the next number:
#include <stdio.h>
int main (void) {
int num;
printf ("Enter a number: ");
if (scanf ("%d", &num) != 1) {
puts ("That wasn't a valid number");
return 1;
}
printf ("The next number is %d\n", num + 1);
return 0;
}
Analysing that code, and what it does when it runs, should be enough to get you started.
For your specific project, the following pseudo-code should help:
print "Enter the ending number: "
input endnum
print "Enter the count of preceding numbers: "
input count
num = endnum - count
do:
print num
num = num + 1
while num <= endnum
That’s the algorithm you can use, I won’t provide it as C code since you’ll become a better coder if you do that yourself. In any case, those pseudo-code lines all pretty much have a one-to-one mapping with C statements so it should be relatively easy to get something going.
2
solved display that precedes the number entered. (C programming) [closed]