In 1-3 sentences, describe how you would "Print all the odd numbers from 0 to x", Recursively using C++
#include <iostream> using namespace std; void printOdds(int n){ // checking n is greater than 0 if(n>0){ // makig recursive call with n-1 printOdds(n-1); // checking n is odd if(n%2==1){ // printing value of n cout<<n<<endl; } } } int main(){ int n; cout<<"Enter an integer: "; cin>>n; cout<<"Odd numbers between 0 and "<<n<<" are:"<<endl; printOdds(n); return 0; }
Get Answers For Free
Most questions answered within 1 hours.