Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
4th Edition
ISBN: 9780134444321
Author: Tony Gaddis
Publisher: PEARSON
bartleby

Concept explainers

Question
Book Icon
Chapter 12, Problem 6PE
Program Plan Intro

Sum of Numbers

Program Plan:

  • • Define the “main()” function:
    • ○ Initialize the variable “number” as “0”.
    • ○ Check the value of “number”.
      • ■ If it is less than or equal to “0”, then get the input from the user and store it to the variable “number”.
    • ○ Call the function “sumNumbers()” and pass the argument “num”.
    • ○ Display the result on the output screen.
  • • Define the “sumNumbers(num)” function:
    • ○ Check the value of “num”.
      • ■ If it is equal to “0”, then return “num”
      • ■ Otherwise, call the function “sumNumbers()” to recursively perform the addition operation.
  • • Call the “main()” function.

Blurred answer
Students have asked these similar questions
6. Sum of Numbers Design a function that accepts an integer argument and returns the sum of all the integers from 1 up to the number passed as an argument. For example, if 50 is passed as an argument, the function will return the sum of 1, 2, 3, 4, . . . 50. Use recursion to calculate the sum.
Recursive Power FunctionWrite a function that uses recursion to raise a number to a power. The function should accept two arguments: the number to be raised and the exponent. Assume that the exponent is a nonnegative integer. Demonstrate the function in a program. SAMPLE RUN #0: ./recursiveExponent         Hide Invisibles Highlight: Show Highlighted Only  2^3=8↵ 2^4=16↵ 3^3=27↵ 6^3=216↵ 7^7=823543↵ 10^9=1000000000↵
6. Sum of Numbers Design a function that accepts an integer argument and returns the sum of all the integers from 1 up to the number passed as an argument. For example, if 50 is passed as an argument, the function will return the sum of 1, 2, 3, 4, . . . 50. Use recursion to calculate the sum.   solve using Python

Chapter 12 Solutions

Starting Out with Python (4th Edition)

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning