print the digits of the given number from left to right using recursion.
Answers
Answered by
5
Answer:
#include<iostream>
void func(int n)
{
if(n <=9){
std::cout<<n;
}
else
{
func(n / 10);
std::cout<<"\n"<< n % 10;
}
}
int main()
{
int n
std::cin>>n;
func(n);
return 0;
}
Explanation:
Answered by
0
Code:
#include<iostream>
void func(int n)
{
if(n <=9){
std::cout<<n;
}
else
{
func(n / 10);
std::cout<<"\n"<< n % 10;
}
}
int main()
{
int n
std::cin>>n;
func(n);
return 0;
}
Explanation:
- Recursion is the action of a function calling itself either directly or indirectly, and the associated function is known as a recursive function. A recursive algorithm can be used to tackle some issues with relative ease. Towers of Hanoi (TOH), inorder/preorder/postorder tree traversals, DFS of Graph, etc. are a few examples of these issues.
- Recursion is a fantastic approach that allows us to shorten our code and make it simpler to read and write. Compared to the iteration technique, it has a few benefits that will be covered later. Recursion is one of the finest ways to complete a work that may be described by its related subtasks. The Factorial of a number, for instance.
#SPJ2
Similar questions