Skip to content

Latest commit

 

History

History

week03

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Variables / Expressions / Statements

continued..

Question 1

Write a program which takes radius of a circle from user and calculate the perimeter and surface of the defined circle.

You can get the value of pi from math module (from math import pi)

Input Output
4 25.1327412287 50.2654824574
8 50.2654824574 201.06192983

Question 2

Write a program that calculates the volume of a sphere by taking its radius from user.

Input Output
3 113.097335529
6 904.778684234

Question 3

Write a program that takes a positive number from user and calculate the sum from 0 to that positive number.

You do not need to use any loops or developed coding

Input Output
10 55
5 15

Question 4

Write a program which replaces the values of two integers.

Input Output
5 10 10 5

Question 5

Write a program which takes a lowercase letter as input and print the uppercase version of the given letter.

Input Output
a A
b B

Question 6

Write a program that takes 3 integers from the user and then prints the sum of odd numbers.

INPUT OUTPUT
3 4 5 8
2 2 4 0
1 9 15 25

Question 7

There is a cash machine with an infinite supply of 5tl banknotes and 1tl coins inside it.

Write a program that takes an integer as input representing the amount of money requested by a customer. The program then prints the number of banknotes and coins given to the customer. (Machine must prefer giving away banknotes instead of coins if possible)

INPUT OUTPUT
57 11 2
269 53 4

Question 8

Guess the outcome of each statement. Then check the results.

Hint: You can multiply strings with integers. It basically performs the concatenation operation n many times.

  • print(int('6') + int('4'))
  • print('1' + '4')
  • print(int(3.9))
  • print(str(5.3) + '2.7')
  • print(int('5') * str(0.6))
  • print(float('3' + '8.6'))
  • print(3.5 + 6.7)
  • print(2 ** 3 ** 2 / 32)
  • print(str(4.4) + str(7.3))
  • print(int(6.5) + float('6.5'))
  • print(int(4.5 + 3.2))
  • print(int(3.2) + int(float(str(3.2))))

Remarks:

  • Observe that float-to-int type conversion is not a rounding operation. int(3.9) = 3. To round floats, we will use a built-in function called round(). Stay tuned!
  • As you see, we can multiply strings with integers, but not floats. What does 3.14 times "pi" mean, anyway?
  • Exponent operator ** has right-to-left associativity.

Question 9

We want to simulate a division operation. In order to do that, write a program to prompt the user for 2 integers, dividend and divisor respectively. Perform the division operation and print out the quotient and the remainder.

INPUT OUTPUT
9 2 quotient = 4 remainder = 1
156 13 quotient = 12 remainder = 0
84 16 quotient = 5 remainder = 4

Question 10

In mathematics and computer science, the floor function is the function that takes as input a real number x, and gives as output the greatest integer less than or equal to x, denoted as ⌊x⌋. The fractional part is the sawtooth function, denoted by {x} for real x and defined by the formula {x} = x - ⌊x⌋. Write a function that takes 3 positive floats from the input and prints out the fractional part of each value respectively. Do not use float-to-int type casting. There might occur some floating-point error, you do not have to deal with that.
Hint: Integer division by 1 can be helpful to find ⌊x⌋. Notice that for negative numbers, we have to perform extra steps to find ⌊x⌋.

INPUT OUTPUT
4.8 6.4 3.5 0.8 0.4 0.5
67.32 23.53 13.98 0.32 0.53 0.98
65.12 49.32 30.09 0.12 0.32 0.09