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
Readability is the level of vocabulary used on the page. True or false
konstantin123 [22]

Answer:

FALSE....john has no idea what hes talking about. i knew this was false yet i looked it up and got it wrong anyway because of john its freaking false

Explanation:

8 0
3 years ago
Review 03 diagnostic and troubleshooting skills including data gathering methods and techniques.
d1i1m1o1n [39]

The kinds and ways to improve your diagnostic and troubleshooting skills are:

  • Be Relax and never panic when you encounter it.
  • Know everything about your computer.
  • Look for solutions and clues and state them down.
  • Find out the repeatability.

<h3>What is diagnostic and troubleshooting?</h3>

Diagnosing is known to be the act of finding out the root cause of any issue through an act of elimination but troubleshooting is known to be the act of fixing of the problem after diagnosis is said to have been carried out.

Therefore, The kinds and ways to improve your diagnostic and troubleshooting skills are:

  • Be Relax and never panic when you encounter it.
  • Know everything about your computer.
  • Look for solutions and clues and state them down.
  • Find out the repeatability.

Learn more about troubleshooting skills from

brainly.com/question/14983884

#SPJ1

6 0
2 years ago
How does communication promotes team building​
Musya8 [376]
Team members must communicate with one another in a way that encourages collaboration and mutual understanding. Trust, message clarity, and loyalty are all enhanced by effective communication. Trust and motivation are eroded by ambiguous, poorly structured communication.
8 0
3 years ago
Read 2 more answers
What is a distraction that you find when photographing animals at the zoo
yaroslaw [1]

D. All of the above.

<u>Reason:</u>

A. Fences can get in the way of a beautiful picture.

B: The animal's enclosure may be hard to get the perfect snap. Like a rock can be in the way of the animal. idk...lol

C. People want to see the animal as much as you do so they may block the way of a picture.

Example from Google..XD


7 0
3 years ago
A computer-aided system for the collection, storage, retrieval, analysis, and presentation of spatial data of all kinds is calle
Strike441 [17]
GIS (Geographical Information System) designed to capture, store, manipulate, analyze, manage and represent spatial data.
7 0
3 years ago
Other questions:
  • Two types of business communications enhanced by desktop publishing are
    7·2 answers
  • Jenny has to include the image of a sun on one of her slides. She needs to write relevant text about it on a dark background on
    6·1 answer
  • What additional information could you add to a sketch to provide other team members with a more accurate design drawing?
    9·1 answer
  • Which of the file names below follows standard file naming convention?
    12·2 answers
  • (Shuffle rows) Write a method that shuffles the rows in a two-dimensional int array using the following header: public static vo
    12·1 answer
  • Day 1 of seeing if anyone will buy me a psn gift card (if u do u get brainliest and thanks + alot of points
    11·1 answer
  • Write the SQL query that would search a table called Movies and return the titles of movies that were made in 1975.
    7·1 answer
  • Professional communication must be objective,
    8·1 answer
  • An international pharmaceutical company is fully compliant with local and international regulations. However, they suffered a ma
    14·1 answer
  • The value of a variable can change multiple times during the execution of a computer program.
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!