Question

In 1-3 sentences, describe how you would "Print all the odd numbers from 0 to x",...

In 1-3 sentences, describe how you would "Print all the odd numbers from 0 to x", Recursively using C++

Homework Answers

Answer #1
#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;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In 1-3 sentences, describe how you would make a "Function that can test if an arbitrary...
In 1-3 sentences, describe how you would make a "Function that can test if an arbitrary string is a palindrome if stripped of punctuation", Recursively using C++
1- Write a program to print only the even numbers from 100 to 200 2- Write...
1- Write a program to print only the even numbers from 100 to 200 2- Write a program to print the odd numbers from 22 – 99 3- Write a program to calculate howe many even number from 10 to 120 1- Write a program to find only the even numbers for 12 different numbers 2- Write a program to find the odd numbers for 10 different numbers 3- Write a program to calculate howe many even number and how...
how many ways can you make 4 digit odd number from 0 1 2 3 4...
how many ways can you make 4 digit odd number from 0 1 2 3 4 5 without repeated numbers
Prime Numbers) Write a program to calculate and print a list of all prime numbers from...
Prime Numbers) Write a program to calculate and print a list of all prime numbers from 1 to 100. C programming
Uses a while loop to print the numbers from 3 - 19. Uses a do-while loop...
Uses a while loop to print the numbers from 3 - 19. Uses a do-while loop to print the numbers from 42 - 56. Uses a for loop to print the numbers from 87 - 95. Asks the user for 2 numbers. Uses a loop to print all numbers between the given numbers, inclusive. Note: Consider that your user's second number can be lower! (see example below) Note: Also consider that your user might give you two of the same...
Using a for loop and range command, print the sequence of numbers from 1 to 20...
Using a for loop and range command, print the sequence of numbers from 1 to 20 (skipping two numbers for each element); that is, your code should print out the numbers 1,4, 7, 10, … (max should not exceed 20). Use x as the variable name
In c++, use nested loops to print a matrix with all the even numbers from 1...
In c++, use nested loops to print a matrix with all the even numbers from 1 to 80 displayed in 5 rows and 8 columns.
How can i make this lunix code print 3 numbers in reverse it must be in...
How can i make this lunix code print 3 numbers in reverse it must be in printStars format and no loops Here is the code i have but can figure out how to reverse the numbers #include<stdio.h> void printStars(int n) { if (n>0){ printf(""); printStars(n-1); } } int main() { int n; int b; int c; printf("Enter 3 numbers to reverse "); scanf("%d",&n,&b,&c); printf("your reversed numbers are %d",n); printStars(n); return 0;
How would you prove that for every natural number n, the product of any n odd...
How would you prove that for every natural number n, the product of any n odd numbers is odd, using mathematical induction?
C programming language. Write a program to print reverse of all the numbers from m to...
C programming language. Write a program to print reverse of all the numbers from m to n using functions with arguments and no return type. The inputs in the program will be m and n variables and need to take these variables as arguments.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT