Python Programming: An Introduction to Computer Science, 3rd Ed.
Python Programming: An Introduction to Computer Science, 3rd Ed.
3rd Edition
ISBN: 9781590282755
Author: John Zelle
Publisher: Franklin, Beedle & Associates
bartleby

Concept explainers

bartleby

Videos

Expert Solution & Answer
Book Icon
Chapter 8, Problem 3D

Explanation of Solution

a.

Sum of “n” numbers using “while” loop:

Program:

#set the values

s = 0

i = 0

#get the input from the user

n = eval (input ("Enter the n value: "))

#check "i" is greater than or equal to "n"

while i <= n:

  #sum all the numbers

  s = s + i

  #increment the value

  ...

Explanation of Solution

b.

Sum first “n” odd numbers using “while” loop:

Program:

#set the values

s = 0

i = 1

#get the input from the user

n = eval (input ("Enter the n value: "))

#check "i" is greater than or equal to "n"

while i <= n:

  #sum all the numbers

  s = s + i

  #increment the value

 ...

Explanation of Solution

c.

Sum of “n” numbers using “while” loop:

Program:

#set the values

s = 0

#check the condition

while True:

  #get the input from the user

  n= eval(input("Enter a value: "))

  #check "n" is equal to 999

  if n == 999:

    #break the statement

    break

�...

Explanation of Solution

d.

Displaying the number of times a whole number “n” can be divided by 2 using “while” loop:

Program:

#get the input from the user

n = int(input("Enter number: "))

#set the value

count = 0

#check "n" is greater than 1

while(n>1):

  #divide the "n" by 2

  n = n // 2

  #incremen...

Blurred answer
Students have asked these similar questions
Write a while loop fragment that calculates the following values:a) Sum of the first n counting numbers: 1 + 2 + 3 + ... + nb) Sum of the first n odd numbers: 1 + 3 + 5 + ... + 2n - 1c) Sum of a series of numbers entered by the user until the value 999 isentered. Note: 999 should not be part of the sum.d) The number of times a whole number n can be divided by 2 (usinginteger division) before reaching 1 (i.e., log2n).
Integer userNum is read from input. Write a while loop that sums all integers read from input until the integer 1 is read. Integer 1 should not be included in the sum. Ex: If the input is 33 -4 -10 -35 1, then the output is: -16 1 #include 2 using namespace std; 3 4 int main() { 5 6 7 8 9 10 11 12 13 14 15 16 17 int userNum; int result; result = 0; cin >> userNum; while(userNum>0) { } result+=userNum; cin>>userNum; cout << result << endl;
Part B:  For Loop Program - using Java Write a program to compute the sum of the first N Fibonacci numbers, where N is an integer that is input by the user.  Once it computes the sum, the program will prompt the user for a new value of N.  The program will exit if the user enters a non-integer number or string (such as “quit”) instead of an integer.    Additionally: For both the above problems, the first four numbers of the Fibonacci sequence are 0, 1, 1, and 2. Part B must use at least one For loop, but may also use while loops.
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
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
Control Structure in Data Structure - Data Structures - Computer Science Class 12; Author: Ekeeda;https://www.youtube.com/watch?v=9FTw2pXLhv4;License: Standard YouTube License, CC-BY