Use a Recursive function in C++ to print numbers that are either even or divisible by 5 or both,
existing in the range of 1 to 100, in Descending order, i.e. 100,98, 96, 95, 94, …, 2.
Answers
Answered by
1
Required Answer:-
Question:
- Use a recursive function in C++ to print numbers that are either even or divisible by 5 or both in the range 1 to 100 in descending order.
Solution:
Here comes the program.
#include <iostream>
using namespace std;
void recursion(int);
int main() {
cout << "Numbers are...\n";
recursion(100);
return 0;
}
void recursion(int n){
if (n >= 1) {
if(n%2==0 || n%5==0)
cout << n << " ";
recursion(n-1);
}
}
Logic:
- Recursive function is a function which calls itself during execution. We can see that recursion function is called by itself till the value of n is greater than 1. If n>=1 is true, then recursion method will be called again and again and if n becomes less than 1, then recursion stops.
Refer to the attachment for output ☑.
Attachments:
![](https://hi-static.z-dn.net/files/d4a/39b4deeb580edaa90f8ce37c3683de49.png)
![](https://hi-static.z-dn.net/files/d7f/60d7c21a02d7ff33ff3a15a6fe6b1790.jpg)
Similar questions
Chemistry,
3 months ago
Computer Science,
6 months ago
Political Science,
6 months ago
Chemistry,
1 year ago