Write the Javascript Coding to enter 2 numbers , print the sequence of numbers between them in reverse order
Answers
Answer:
Below is the implementation of the above approach.
// C++ program to print all numbers between 1
// to N in reverse order
#include <bits/stdc++.h>
using namespace std;
// Recursive function to print from N to 1
void PrintReverseOrder(int N)
{
for (int i = N; i > 0; i--)
cout << i << " ";
}
// Driven Code
int main()
{
int N = 5;
PrintReverseOrder(N);
return 0;
}
Output:
5 4 3 2 1
Approach 2: We will use recursion to solve this problem.
Check for the base case. Here it is N<=0.
If base condition satisfied, return to the main function.
If base condition not satisfied, print N and call the function recursively with value (N – 1) until base condition satisfies.
Below is the implementation of the above approach.
// C++ program to print all numbers between 1
// to N in reverse order
#include <bits/stdc++.h>
using namespace std;
// Recursive function to print from N to 1
void PrintReverseOrder(int N)
{
// if N is less than 1
// then return void function
if (N <= 0) {
return;
}
else {
cout << N << " ";
// recursive call of the function
PrintReverseOrder(N - 1);
}
}
// Driven Code
int main()
{
int N = 5;
PrintReverseOrder(N);
return 0;
}