1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
bagirrra123 [75]
4 years ago
13

Write a method that accepts an integer argument and returns the sum of all the integers from 1 up to (and including) the number

passed as an argument. For example, if 3 is passed as an argument, the method will return the sum of 1+2+3, which is 6. Use recursion to calculate the sum. Test your method in main by prompting the user to enter a positive integer to sum up to. Provide input validation so that only positive values are accepted.
Computers and Technology
1 answer:
Mashutka [201]4 years ago
8 0

Here is code in C++.

#include <bits/stdc++.h>

using namespace std;

// recursive function to calculate sum from 1 to n

int calculate_sum(int n)

{

if (n <= 1)

 return n;

return n + calculate_sum(n - 1);

}

// Driver code

int main()

{

   // variable to read input

int n;

cout<<"Please Enter a number: ";

//reading input from user

cin>>n;

do{

    if(n<=0)

    {

    cout<<"please enter only positive numbers(greater than 0):"<<endl;

    cin>>n;

    }

} while(n<=0);

// calling the recursive function and printing the sum

cout << "Sum of all numbers from 1 to "<<n<<" is: "<<calculate_sum(n);

return 0;

}

Explanation:

Read input from user and assign it to variable "n".if the input is less

than or equal to 0, it will again ask user to enter a number greater than 0.

then Call function calculate_sum() with argument "n". This function will

calculate sum recursively from n to 1.when recursive function call itself for

n<=1 (base condition),it calculate the total sum.

Output:

Please Enter a number: -5

please enter only positive numbers(greater than 0):

6

Sum of all numbers from 1 to 6 is: 21

You might be interested in
According to the information presented in this​ video, a spreadsheet is effective for managing information about one thing​ (e.g
Nat2105 [25]

Answer:

According to the information presented in this​ video, a spreadsheet is effective for managing information about one thing​ (e.g., items for​ sale), but a

C. database

is better for managing information about more than one thing​ (e.g., items for sale and suppliers of​ items).

Explanation:

  • The option A is not correct as the flowchart is a step-by-step explanation of a procedure. It is visual presentation in which we explain a whole process.
  • The  option B is not correct as network is defined as the group of different computers, servers and other machines that helps the exchange of data.
  • The option c is correct as database is the means of storage of data as well as we can access this data at any time and it let you manage the data as well so we can manage a lot of information about multiple things at a time.
  • The option d is also incorrect as workflow is the visual presentation of sequence in which tasks are performed so it doesn't have to do anything wit the managing the information.
  • The option e is also incorrect as data model defines the properties of data of different things and it is used in database. It sets the rule of storing, managing and accessing the data in a database.
7 0
3 years ago
You (blank) see stop signs on highways.
BlackZzzverrR [31]

Answer:

B

Explanation:

You can't stop on a highway

7 0
4 years ago
Read 2 more answers
When computing the net cash provided by operating activities under the indirect method on the statement of cash flows, a decreas
levacccp [35]

Answer: False

Explanation:

The given statement is false, as in the indirect method there is decrease in the common stock value are not be subtracted from the net income because it is not the current liability.

In the cash flow method, the cash flow from the operating activities are prepared by using two methods that are:

  • Direct method
  • Indirect method

The cash flow indirect method, indicate the net income figure from the given income statement. It is basically used to represent the net cash with all the necessary earnings from the actual cash received.

3 0
3 years ago
30 POINTS !!!!!!!!
alex41 [277]

this should help! i read over the article a little bit and it seems to answer your question pretty well. https://www.snap.com.au/articles/the-pros-and-cons-of-digital-and-offset-printing.html

7 0
3 years ago
How do you change the top and bottom margins of an entire document
Elodia [21]
Deep depending on the document editor, you should be able to just click either the top or bottom margin to edit!
6 0
4 years ago
Other questions:
  • Which is currently the most common cellular network?<br> 4G<br> 4G LTE<br> 5G<br> 5G LTE
    13·1 answer
  • [c++] Write a recursive function takes a word string as input argument and reverse the word. Print out the results of recursive
    14·1 answer
  • What was the first video game ever invented
    11·2 answers
  • Write a method called printGrid that accepts two integers representing a number of rows and columns and prints a grid of integer
    11·1 answer
  • If $350 is the profit of an initial investment of $1000.00, what is the percentage profit?​
    11·2 answers
  • Describe how being a global citizen in the world of advanced technology can be beneficial to your success in meeting your person
    13·1 answer
  • Jared has trouble remembering when his assignments are due, and he wants to keep reminders on his computer. Which online note-ta
    10·2 answers
  • Which computer application is an example of a DBMS?
    8·1 answer
  • Hey does anyone know how to fix a computer that goes on and off. It is a Asus chromebook and putting it into recovery mode didn'
    5·2 answers
  • MOA115 Medical Records and Insurance
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!