sa 12 cs chapter 1

6. One foot equals 12 inches. Write a function that accepts a length written in feet as an argument and returns this length written in inches. Write a second function that asks the user for a number of feet and returns this value. Write a third function that accepts a number of inches and displays this to the screen. Use these three functions to write a program that asks the user for a number of feet and tells them the corresponding number of inches.

# Code def feet_to_inches(x): return x*12 def feet(): x = int(input(‘Enter feet : ‘)) return x def disp_inches(x): print(‘Number of inches :’, x) x = feet() x = feet_to_inches(x) disp_inches(x)

6. One foot equals 12 inches. Write a function that accepts a length written in feet as an argument and returns this length written in inches. Write a second function that asks the user for a number of feet and returns this value. Write a third function that accepts a number of inches and displays this to the screen. Use these three functions to write a program that asks the user for a number of feet and tells them the corresponding number of inches. Read More »

7. Write a program that reads an integer N from the keyboard computes and displays the sum of the numbers from N to (2 * N) if N is nonnegative. If N is a negative number, then it’s the sum of the numbers from (2 * N) to N. The starting and ending points are included in the sum.

# Code n = int(input()) x = 0 if n > 0: for i in range(n, 2*n+1): x += i else: for i in range(2*n, n+1): # for negative n, we need to reverse start & end of range x += i print(x)

7. Write a program that reads an integer N from the keyboard computes and displays the sum of the numbers from N to (2 * N) if N is nonnegative. If N is a negative number, then it’s the sum of the numbers from (2 * N) to N. The starting and ending points are included in the sum. Read More »