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
An information system includes _____, which are programs that handle the input, manage the processing logic, and provide the req
Ronch [10]

Answer:

applications

Explanation:

In an information system, applications or software are indispensable. This is because the entire data/info processing pipeline running on daily basis have been digitized. Users are heavily rely on the applications to gain and store new data, to manage and process the data and to deliver the necessary output. The applications enable the entire data processing work become more efficient, systematic and also more secure.

7 0
3 years ago
Write a Python program to convert temperatures to and from celsius, fahrenheit.
Rudiy27

Answer:

https://www.w3resource.com/python-exercises/python-conditional-exercise-2.php

3 0
3 years ago
What is the memory of a computer called
Nataliya [291]
It is called Ram in another term for memory :)
6 0
3 years ago
Read 2 more answers
________is one color shade gradually progressing to another shade of the same color or one color progressing to another color.
posledela

Answer:

Gradient fill

Explanation:

A gradient fill is a visual effect that makes a three dimensional color look by mixing one color shade gradually progressing to another shade of the same color or one color progressing to another color. for example a dark red progressively changing to light red or red progressively changing to blue.

8 0
3 years ago
Arman, a friend of your dad, has a computer running Windows 7 and wants to perform an upgrade. He has only 3GB of RAM in his com
torisob [31]

Answer:

Low memory?

So to fix the problem you could add more memory to the machine.

Explanation:

8 0
3 years ago
Other questions:
  • Can someone please help me write a code for this
    7·1 answer
  • The Security Development Life Cycle (SDLC) is a general methodology for the design and implementation of an information system.
    5·1 answer
  • A ________ is hardware or software that acts as a filter to prevent unwanted packets from entering a network.
    9·2 answers
  • You can toggle between different types of references by pressing the ____ key on your keyboard.
    15·1 answer
  • This is important I need help please
    14·1 answer
  • Your instructor has asked you to perform some research regarding a computer OS capability of distinguishing spoken words. What i
    14·1 answer
  • Consider the following partial class definitions: public class A1 { public int x; private int y; protected int z; ... } public c
    12·1 answer
  • How do you copy and paste plz let me know
    14·2 answers
  • One way to use contiguous allocation of the disk and not suffer from holes is to compact the disk every time a file is removed.
    13·1 answer
  • How Powerpoint is useful in education aspect?
    5·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!