-5

C Programming

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

Let’s say if user enter 9 and wants 3 numbers that precedes 9, your program should display this: • 6 7 8 9

Have no idea need some help.

Blastfurnace
  • 18,411
  • 56
  • 55
  • 70
gtago4
  • 7
  • 2

1 Answers1

0

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • But i need to ask "how many numbers he wants to display that precedes first number he enters" in the program so i can not just do num+1. – gtago4 Oct 14 '15 at 02:53
  • @gtago4, I've given you *more* than enough to learn how to do it yourself. You're unlikely to become a decent developer if you get random people on the internet to do all your work for you :-) I'd suggest reading this answer then going and attempting it yourself. If you strike problems that you can't fix on your own, post your code in another question asking for help. – paxdiablo Oct 14 '15 at 02:56