using while loop to do the recursion