# Recursive function that prints odd numbers

Page 1 of 1

## 7 Replies - 39040 Views - Last Post: 08 October 2010 - 11:22 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=194151&amp;s=0afa8693f2056b115ba56f340be7e0d5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 izic

Reputation: 0
• Posts: 223
• Joined: 15-January 09

# Recursive function that prints odd numbers

Posted 08 October 2010 - 07:48 PM

I'm trying to figure out how to get this function to work. Basically, I'll enter a number(eg. 9) and using recursion my program should print the odd numbers which would be 1,3,5,7, and 9. Right now, my program complies, runs, and then crashes. Based on my code, can someone help me and tell me where I'm going wrong?

```#include <iostream>
using namespace std;

int printoddnum(int n);

int main()
{
int n;

cout<<"Enter a number"<<endl;
cin>>n;

printoddnum(n);

system("pause");
return 0;
}
int printoddnum(int n)
{
int i = 0;

while(i <= n)
{
printoddnum(n + 1);
if (n % 2 != 0)
{
return n;
cout<<n<<endl;
}

i++;
}

}
```

This post has been edited by izic: 08 October 2010 - 07:58 PM

Is This A Good Question/Topic? 0

## Replies To: Recursive function that prints odd numbers

### #2 Crunch

• D.I.C Lover

Reputation: 139
• Posts: 1,222
• Joined: 28-July 09

## Re: Recursive function that prints odd numbers

Posted 08 October 2010 - 07:53 PM

### #3 ishkabible

• spelling expret

Reputation: 1747
• Posts: 5,898
• Joined: 03-August 09

## Re: Recursive function that prints odd numbers

Posted 08 October 2010 - 08:02 PM

your returning before printing for one. two this is mixing recursion and loops in a way that results in infinitive calls. i is always 0 and n keeps getting bigger witch with each recursion, so nothing ever gets past the recursive call, that is why it crashes. i would do this by taking a number checking weather or not it was odd by using the same check you used. if it was even then subtract 1 (also check for 0 as it will be even as well and we don't want to print any further). then i would print this number and pass it minus 2.

*check if n is 0, if it is return.
*check if n is even, if so subtract 1.
*print n
*call function again but - 2
*return form function

This post has been edited by ishkabible: 08 October 2010 - 08:07 PM

### #4 izic

Reputation: 0
• Posts: 223
• Joined: 15-January 09

## Re: Recursive function that prints odd numbers

Posted 08 October 2010 - 10:52 PM

You're advice is helping so far but I'm still running into a big issue. My loop won't terminate.
```int printoddnum(int n)
{
if(n == 0)
return 0;

if (n % 2 != 0)
{
cout<<n;
printoddnum(n - 2);
return n;
}
if (n % 2 == 0)
{
cout<<n;
printoddnum(n - 1);
return n;
}
}
```

### #5 aaa111

• D.I.C Regular

Reputation: 88
• Posts: 284
• Joined: 21-February 07

## Re: Recursive function that prints odd numbers

Posted 08 October 2010 - 11:07 PM

Why would it terminate:
```  if(n == 0)
return 0;

```

As it is not becoming true by any means.Why??
```if (n % 2 != 0)
{
cout<<n;
printoddnum(n - 2);
return n;
}
```

Also you don't have to print the number when it is even and you could decrease n by 1 in each case.

This post has been edited by aaa111: 08 October 2010 - 11:16 PM

### #6 n8wxs

• --... ...-- -.. . -. ---.. .-- -..- ...

Reputation: 972
• Posts: 3,878
• Joined: 07-January 08

## Re: Recursive function that prints odd numbers

Posted 08 October 2010 - 11:07 PM

You are finding negative numbers:

Quote

Enter a number
9
97531-1-3-5-7-9-11-13-15-17...

Change

```int printoddnum(int n)
{
if(n == 0)
return 0;
...

```

to

```int printoddnum(int n)
{
if(n <= 0)
return 0;
...

```

This post has been edited by n8wxs: 08 October 2010 - 11:09 PM

### #7 izic

Reputation: 0
• Posts: 223
• Joined: 15-January 09

## Re: Recursive function that prints odd numbers

Posted 08 October 2010 - 11:15 PM

Thanks alot everyone. I greatly appreciate your help and I will leave positive feedback. Also, where can I find some really good recursion tutorials/exercises?

### #8 aaa111

• D.I.C Regular

Reputation: 88
• Posts: 284
• Joined: 21-February 07

## Re: Recursive function that prints odd numbers

Posted 08 October 2010 - 11:22 PM