Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
im absolute beginner in programming world.python is my first programming language ..i solved it without recursion but how can i code it using recursion its become difficult for me

What I have tried:

i tried witout recursion and i solved it
Posted
Updated 4-Aug-20 6:41am
Comments
Patrice T 17-Dec-18 14:11pm    
You should show what you have done so far.
Just show that you have done something :)

You know, a recursive function calls itself until a stop condition is met. so, in your case you need a function that:
  • Checks the stop condition (all numbers printed out) and returns if it is met.
  • Prints the current number.
  • Calls itself with an incremented argument in order to print the next number.

Something similar to (pseudocode)
even(n, N)
  if n == N then return // stops the recursive process
  print(2*n)
  even(n+1, N) // recursive call

Then you may call it this way
even(0,5)
 
Share this answer
 
First learn what recursion is. You can go that by learning what recursion is.

Recursion is like a loop, but it's different.
If you think about classic exam,ples of recursion, there is factorial:
N! = N * (N - 1)! while N > 1
That's obviously recursive, because the definition of a factorial value is expressed in terms of a factorial of a lesser value (until you run out of values)

So the first big step in implementing a recursive function in any language to to express the operation in terms of a recursive algorithm. I.e. express "N even numbers" as recursive pseudo code. Then you can start implementing it.

Give it a try: this isn't complicated, but it's something you need to get your head around for yourself - being given a solution doesn't teach you anything about how to do it next time!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900