Question

Write a C++ program that asks the user to enter a single number from 1 to...

Write a C++ program that asks the user to enter a single number from 1 to 50 and a choice of "even" or "odd". Using a recursive function, output the sum of all the even or odd integers from 1 up to (and including) that number. Your program does not need to loop and accept more input (only one input of the number then even/odd).

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

int sum(int n, bool even) {
    if (n <= 0) {
        return 0;
    } else {
        int total = sum(n - 1, even);
        if (n % 2 == 0 && even) {
            total += n;
        } else if (n % 2 == 1 && !even) {
            total += n;
        }
        return total;
    }
}

int main() {
    int n, choice;
    cout << "Enter a number between 1 and 50: ";
    cin >> n;

    cout << "1. Sum odd numbers\n2. Sum even numbers\nEnter your choice: ";
    cin >> choice;

    cout << "Sum is " << sum(n, choice==2) << endl;
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that asks the user to enter a single number from 1 to...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ coding answer 5. Write a program that asks the user for a positive integer value....

    C++ coding answer 5. Write a program that asks the user for a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, ... 50. Input Validation: Do not accept a negative starting number.

  • use C++ please 1. Vowels and Consonants Write a program that asks the user to input...

    use C++ please 1. Vowels and Consonants Write a program that asks the user to input three different integers. Write a function called numberStyle for this program that will accept each integer (one at a time) and return the following . If the integer is even, return 1 . If the integer is odd, return -1 . If the integer is zero, return O In main, after calling the function output the appropriate message describing the integers ing the function...

  • Write a Java program that: • Asks the user to enter the number of integers that...

    Write a Java program that: • Asks the user to enter the number of integers that he need to enter; • Asked him to enter integer by integer; • Print The number of Odd numbers entered and the number of Even numbers entered. Important notes: 1. You should have to copy and paste the Java as your answer for this question. DON’T take screen shot for your Java Code. It must be editable. 2. Take a screen shot for your...

  • Write a java program that asks the user to enter 2 integers, obtains them from the...

    Write a java program that asks the user to enter 2 integers, obtains them from the user, determines if they are even or odd numbers and prints their sum, product, difference, and quotient (Division).

  • NASM ASSEMBLY WINDOWS DOSBOX: 5) Write assembly program that asks the user to enter a number...

    NASM ASSEMBLY WINDOWS DOSBOX: 5) Write assembly program that asks the user to enter a number ( the user should input any integer number, say N), then the program outputs N if N is even; or N+3 if N is odd ( note: the output is always even) Example: Enter any number: 25 Even output is: 28 Another example: Enter any number: 32 Even output is: 32.

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • 1. Write a program that asks the user to enter two integers, obtains the numbers from...

    1. Write a program that asks the user to enter two integers, obtains the numbers from the user, then prints the larger number followed by the words “is larger.” If the numbers are equal, print the message “These numbers are equal.” Use only the single-selection form of the if statement you learned in this chapter.

  • 2) Write a program in C/C++ that asks the user to enter a number then the...

    2) Write a program in C/C++ that asks the user to enter a number then the user's input value is passed to a function which increments the number by 10 and returns the result of the incrementation. Write the program to accomplish the increment using three (3) different techniques. To test each technique separately, the user should enter a different number for each of the three (3) techniques. Make sure that you compile and execute your program. Finally, provide screenshots...

  • python program 6 Write an input validation loop that asks the user to enter a number...

    python program 6 Write an input validation loop that asks the user to enter a number in the range of 100 through 1000? 7 Write an input validation loop that asks the user to enter ‘Y’, ‘y’, ‘N’, or ‘n’? 8 How many times the following loop will repeat? cnt = 0    while  cnt != 5: print(cnt) cnt = cnt + 2

  • Write a program that asks the user to type an even number or 111 to stop....

    Write a program that asks the user to type an even number or 111 to stop. When the user types an even number, display the message “Great Work”, and then ask for input again. When the user types an odd number, display the message “Try again” and ask the user to enter again. When the user enters the sentinel value of 111 the program ends I've attempted this but it went horribly wrong. we are only suppose to use while,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT