[This is on Edhesive (coding and programming)]
2.2 Code Practice
Question 1:
Ask the user to input an integer. Print out the next three consecutive numbers.

Question 2:
Write a program that accepts three decimal numbers as input and outputs their sum.

Respuesta :

The code is written using Python language as it is one of the programming languages introduced by the Edhesive courses.

Answer (Question 1):

  1. num = int(input("Input an integer: "))
  2. for i in range(1, 4):
  3.    print(num + i)

Explanation (Question 1):

Line 1 :

Prompt user for input an integer.  In Python we can easily use input method to ask user for an input.

However the default data type of the input is a string and therefore we need to enclose our input value within int() to convert the value from string to integer.

Line 3 - 4:

Create a for-loop to loop though the code in Line 4 for three times using the range() function. The range(1, 4) will return a list of numbers, [1, 2, 3] .

In each round of the loops, one number from the list will be taken sequentially and total up with the input number, num and display using print() function.

Answer (Question 2):

  1. counter = 3
  2. sum = 0
  3. while (counter > 0):
  4.    num = float(input("Enter a decimal number: "))
  5.    sum += num
  6.    counter = counter - 1
  7. print("The sum is " + str(sum))

Explanation (Question 2):

Line 1 - 2 :

Create a counter variable and a sum variable and initialize them with 3 and 0, respectively.

Line 4:

Create a while loop and set a condition while counter is bigger than zero, the loop should keep running the code from Line 5-7.

Line 5:

Use input function again to prompt user for a decimal number. This time we need to use float function to convert the input value from string to decimal data type.

Line 6:

Every time when a decimal number is input, the number is added to the sum variable.

Line 7:

Decrement the counter variable by 1. This step is important to ensure the while loop will only be repeated for three times and each loop will only accept one input value from user.

Line 9

Display the sum.

fichoh

Using Python 3, the codes for the required programs are :

  • number = int(input())

  • for i in range(1,4):
  • print(number + i)

  • counter = 3
  • sum = 0
  • while counter > 0 :
  • num = eval(input('enter 3 decimal values'))
  • counter-=1
  • sum = sum + num

  • print(sum)

The first program :

number = int(input())

#accept integer input from user

for i in range(1,4):

#Start a for loop to give a value of (1, 2, 3)

print(number + i)

#each iteration is added to the value of the integer given by the user.

Second program :

counter = 3

#a counter that monitors the number of inputs given

sum = 0

#initialize sum to 0

while counter > 0 :

#checks the Number of inputs given

num = eval(input('enter 3 decimal values'))

#allows user to enter values

counter-=1

#counter decreases by 1 after each value inputted

sum = sum + num

#values are summed up

print(sum)

#outputs the result.

Learn more : https://brainly.com/question/13534392

ACCESS MORE