# Thread: Basic recursion question

1. ## Basic recursion question

Without using any loops, I need to create a function that will use recursion to print all the multiples of a given number up to any limit given (the limit will be given to the function by the programmer). e.g. given 7, will print 7, 14, 21, 28...(until the end)

Here is the code I have:
Code:
```#include <stdio.h>

int main()
{
int a, b, c;

printf("Enter in an integer and a number you wish to stop at:\n");
scanf("%d%d", &a, &b);

c = a;
c < b;
c += a;

printf("Your numbers are: %d.\n", c);

return 0;
}```
the problem is when I run the code the program only outputs the next number and not the original number or the numbers leading up to the number I want the program to end at.

Any help would be greatly appreciated.

2. You're way off base. Read a tutorial on recursion: Cprogramming.com Tutorial: Recursion.

A quick note on your program
Code:
```    c = a;  // assign value in a to c
c < b;  // compare to check if c is less than b, this has no effect
c += a;  // increment c by value in a```

3. Originally Posted by anduril462
You're way off base. Read a tutorial on recursion: Cprogramming.com Tutorial: Recursion.

A quick note on your program
Code:
```    c = a;  // assign value in a to c
c < b;  // compare to check if c is less than b, this has no effect
c += a;  // increment c by value in a```
So I just read the tutorial and it was no help. PLEASE EXPLAIN!!

4. Read your class notes and text book, and search the web for more examples. There are many, far better tutorials out there than I can provide in a 2 minute break from work. Make an effort to write recursive code. Come back with a program that has a second function (other than main), and make sure that calls itself. Then, we'll start working on your base case and recursive steps, but you have to make an effort first.

5. I've read 10 recursion tutorials and they are all based on the same examples. Does anyone know how I can change my code to output all the numbers not just to 1st?

I am looking for my program to do the same thing as http://cboard.cprogramming.com/c-pro...-question.html without using a loop.

6. Originally Posted by benrogers
So I just read the tutorial and it was no help. PLEASE EXPLAIN!!
Really? You read that tutorial and this part didn't help at all

Code:
```void count_to_ten ( int count )
{
/* we only keep counting if we have a value less than ten
if ( count < 10 )
{
count_to_ten( count + 1 );
}
}
int main()
{
count_to_ten ( 0 );
}```

7. Originally Posted by homer_3
Really? You read that tutorial and this part didn't help at all

Code:
```void count_to_ten ( int count )
{
/* we only keep counting if we have a value less than ten
if ( count < 10 )
{
count_to_ten( count + 1 );
}
}
int main()
{
count_to_ten ( 0 );
}```
Yes what does
Code:
`void count_to_ten ( int count )`
even mean?

8. Think about the logic that your recursive function will need. In pseudo code:

Code:
```void recurse(int theNumber, int aCounter, int limit)
if(theNumber * aCounter < limit)
and call recurse function, again.
end if
end function```
If you start doing "number = number * counter", you'll be off into the weeds, very fast.

It means "count to ten() is a function that takes the parameters (variables), that are listed between ( and ).

9. Originally Posted by benrogers
Yes what does
Code:
`void count_to_ten ( int count )`
even mean?
If you don't know what that means, then you're not ready for recursion yet. Sorry, but you must have a good understanding of functions before you can do recusion. Read some function tutorials: Cprogramming.com Tutorial: Functions. Search the web for more. Master functions, then work on recursion.

Popular pages Recent additions