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:
Similar questions
Math,
2 months ago
English,
2 months ago
Computer Science,
5 months ago
Political Science,
5 months ago
Chemistry,
11 months ago