[2021] CSE-110 Programming Language I Lab Assignment-1 Full PDF

Title [2021] CSE-110 Programming Language I Lab Assignment-1 Full
Course Computer Programming II
Institution BRAC University
Pages 12
File Size 209.2 KB
File Type PDF
Total Downloads 7
Total Views 120

Summary

Brac university - CSE 110 Lab Assignment 1...


Description

CSE110 Lab Assignment 1 This assignment is to guide you to solve basic programming problems in Python. Since this is the first assignment, the first problem is solved for you with explanations. Some problems are partially solved to help you. Go through them and try to understand each statment. If you can understand the problems and the solutions done for you, you can solve the rest of the tasks by yourself. [MUST MAINTAIN VARIABLE NAMING CONVENTIONS FOR ALL THE TASKS]

Write Write the the Python Python code code of of the the following following problems: problems: Task 1 Write Python code of a program that reads two numbers from the user, and prints their sum, product, and difference. ==========================================================

hint: Subtract the second number from the first one ========================================================== Example01: Input:\ 4\ 5 Output:\ Sum = 9\ Product = 20\ Difference = -1 ========================================================== Example02: Input:\ 30\ 2 Output:\ Sum = 32\ Product = 60\ Difference = 28 ========================================================== For your explanation, the first question's code is done below. Try relating the block of code with the lesson you have learned and understand the significance of each line. You're most welcome to play around with the code. This will strengthen your understanding. In [2]:

### Take input of 2 numbers from the user var_1 = input('Please Enter Your First Number: ') var_2 = input('Please Enter Your Second Number: ') #Since input() function converts everything to String, #for performing any kind of mathematical operation you need to convert them to int. #For this conversion, we need to use int() function # First, let's clarify whether the inputs are actually Strings or not. print(type(var_1)) print(type(var_2)) # Convert Strings to integer using the int() function var_3 = int(var_1) var_4 = int(var_2) #===============================================================

#input taking and conversion can be done in a single sentence #var_1 = int(input('Please Enter Your First Number')) #var_2 = int(input('Please Enter Your Second Number')) #=============================================================== # Perform Addition total = var_3 + var_4 # Perform Multiplication product = var_3 * var_4 # Perform Substraction difference = var_3 - var_4 # Print all the calculated results print("Sum =", total) print("Product =", product) print("Difference =", difference)

Sum = 32 Product = 60 Difference = 28

Task 2 Write Python code of a program that reads the radius of a circle and prints its circumference and area. ==========================================================

hint(1): import math and then use math.pi for getting the value of pi.\ For details read from https://docs.python.org/3/library/math.html hints(2): You can import math and use math function for making squares math.pow(number, power) Or you can simply write using power opeartor. For example: S**2. ========================================================== Example01: Input: 4 Output:\ Area is 50.26548245743669\ Circumference is 25.132741228718345 ========================================================== Example02: Input: 3.5 Output:\ Area is 38.48451000647496\ Circumference is 21.991148575128552 ========================================================== For your explanation, the the first part of the question(area calculation) is done below. In [2]: import math

#taking input from the user, then converting it to float. #Since radius can be a floating point value radius

= float(input("please enter the radius value:" ))

# squares can be made using this 3 ways, as written in hints. # all 3 ways, generates the same result of area

area = math.pi * radius **2 print("Area result is:", area) area = math.pi * math.pow(radius, 2) print("Area result is:", area) area = math.pi * radius * radius print("Area result is:", area)

#============================================================== # TODO # calculate circumference circumference = 2 * math.pi * radius print("Circumference result is : ", circumference) #============================================================== please enter the radius value:4 Area result is: 50.26548245743669 Area result is: 50.26548245743669 Area result is: 50.26548245743669 Circumference result is : 25.132741228718345

Task 3 Write Python code of a program that reads two numbers from the user. Your program should then print "First is greater" if the first number is greater, "Second is greater" if the second number is greater, and "The numbers are equal" otherwise. ========================================================== Example: Input:\ -4\ -4 Output:\ The numbers are equal ========================================================== Example: Input:\ -40\ -4 Output:\ Second is greater In [2]:

#Todo num1 = int(input("Enter Your First Number: ")) num2 = int(input("Enter Your Second Number: ")) if num1 > num2: print("First is greater") elif num2 > num1: print("Second is greater") else: else print("The numbers are equal") Enter Your First Number: -40 Enter Your Second Number: -4 Second is greater

Task 4 Write Python code of a program that reads two numbers, subtracts the smaller number from the larger one, and prints the result.

hint(1): First check which number is greater ========================================================== Example: Input:\ -40\ -4 Output:\ 36 ========================================================== Example: Input:\ 6\ 2 Output:\ 4 ========================================================== Example: Input:\ 5\ 5 Output:\ 0 In [2]:

#Todo num1 = int(input("Enter First Number: ")) num2 = int(input("Enter Second Number: ")) if num1 > num2: print(num1 - num2) elif num1 == num2: print(0) else: else print(num2 - num1) 0

Task 5 Write Python code of a program that reads a number, and prints "The number is even" or "The number is odd", depending on whether the number is even or odd.

hint(1): use the modulus (%) operator hint(2): You can consider the number to be an integer ========================================================== Example: Input:\ 5 Output:\ The number is odd ========================================================== Example: Input:\ -44 Output:\ The number is even In [13]:

#Todo

num = int(input("Enter Your Number: ")) if num % 2 == 0: print("The Number is Even") else: else print("The Number is Odd") Enter Your Number: 1 The Number is Odd

Task 6 Write Python code of a program that reads an integer, and prints the integer if it is a multiple of either 2 OR 5. For example, 2, 4, 5, 6, 8, 10, 12, 14, 15, 16, 18, 20, 22 … ==========================================================

hint(1): use the modulus (%) operator for checking the divisibility hint(2): You can consider the number to be an integer ========================================================== Example01: Input:\ 5 Output:\ 5 ========================================================== Example02: Input:\ 10 Output:\ 10 ========================================================== Example03: Input:\ 3 Output:\ Not a multiple In [25]:

#Todo num = int(input("Enter a number: ")) if num % 2 == 0 or num % 5 == 0: print(num) else: else print("Not a multiple") Enter a number: 11 Not a multiple

Task 7 Write Python code of a program that reads an integer, and prints the integer it is a multiple of either 2 or 5 but not both. For example, 2, 4, 5, 6, 8, 12, 14, 15, 16, 18, 22 … ==========================================================

hint(1): use the modulus (%) operator for checking the divisibility hint(2): You can consider the number to be an integer ========================================================== Example01: Input:\ 5 Output:\ 5 ========================================================== Example02: Input:\ 10 Output:\ multiple of 2 and 5 both ========================================================== Example03: Input:\ 44 Output:\ 44 In [30]:

#Todo num = int(input("Enter a number: ")) if num % 2 == 0 and num % 5 != 0 or num % 2 != 0 and num % 5 == 0: print(num) else: else print("multiple of 2 and 5 both") Enter a number: 10 multiple of 2 and 5 both

Task 8 Write Python code of a program that reads an integer, and prints the integer if it is a multiple of 2 and 5. ========================================================== For example, 10, 20, 30, 40, 50 …

hint(1): use the modulus (%) operator for checking the divisibility hint(2): You can consider the number to be an integer ========================================================== Example01: Input:\ 5 Output:\ Not multiple of 2 and 5 both ========================================================== Example02: Input:\ 40 Output:\ 40

========================================================== Example03: Input:\ 50 Output:\ 50 ========================================================== Example04: Input:\ 44 Output:\ Not multiple of 2 and 5 both In [51]:

#Todo num = int(input("Enter a number: ")) if num % 2 == 0 and num % 5 == 0: print(num) else: else print("Not multiple of 2 and 5 both") Enter a number: 5 Not multiple of 2 and 5 both

Task 9 Write Python code of a program that reads an integer, and prints the integer if it is a multiple of NEITHER 2 NOR 5. For example, 1, 3, 7, 9, 11, 13, 17, 19, 21, 23, 27, 29, 31, 33, 37, 39 … ==========================================================

hint(1): use the modulus (%) operator for checking the divisibility hint(2): You can consider the number to be an integer ========================================================== Example01: Input:\ 3 Output:\ 3 ========================================================== Example02: Input:\ 19 Output:\ 19 ========================================================== Example03: Input:\ 5 Output:\ No ========================================================== Example04:

Input:\ 12 Output:\ No In [63]:

#Todo num = int(input("Enter a number: ")) if num % 2 != 0 and num % 5 != 0: print(num) else: else print("No") Enter a number: 14 No

Task 10 Write Python code of a program that reads an integer, and prints the integer if it is NOT NOT aa multiple multiple of of 22 OR NOT a multiple multiple of of 5. 5. For example, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22 ==========================================================

hint(1): use the modulus (%) operator for checking the divisibility hint(2): You can consider the number to be an integer ========================================================== Example01: Input:\ 3 Output:\ 3 ========================================================== Example02: Input:\ 15 Output:\ 15 ========================================================== Example03: Input:\ 20 Output:\ No ========================================================== Example04: Input:\ 14 Output:\ 14 In [68]:

#Todo num = int(input("Enter a number: "))

if num % 2 != 0 or num % 5 != 0: print(num) else: else print("No") Enter a number: 10 No

Task 11 Write Python code of a program that reads a student’s mark for a single subject, and prints out the corresponding grade for that mark. The mark ranges and corresponding grades are shown in the table below. You need to make sure that the marks are valid. For example, a student cannot receive -5 or 110. So the valid marks range is 0 to 100.

hint(1): You can consider the number to be an integer hint(2): This problem can be solved in two ways: top-down (starts from A) and bottom-up (starts from F) Marks Grage 90 or above

A

80-89

B

70-79

C

60-69

D

50-59

E

Below 50

F

In [9]:

#### Todo mark = int(input("Enter your Mark: ")) if mark < 0 or mark > 100: print("Not a valid mark") else: else if mark >= 0 and mark < 50: print("F") elif mark >=50 and mark < 60: print("E") elif mark >=60 and mark < 70: print("D") elif mark >=70 and mark < 80: print("C") elif mark >=80 and mark < 90: print("B") elif mark >= 90 and mark 168: print("Impossible to work more than 168 hours weekly") elif hours in range(0, 168): if hours 40: extra_hours = hours - 40 salary = 8000.00 + extra_hours * 300 print(salary) Enter worked hour: 100 26000.0

Task 13 Write Python code of a program that finds the number of hours, minutes, and seconds in a given number of seconds. ==========================================================

hint(1): The user input will be an integer value hint(2): 1 hour = 60 mins = 3600 seconds\ 1min = 60 seconds ========================================================== Example01:\ Example01: Given seconds: 10000\ Output: Hours: 2 Minutes: 46 Seconds: 40 ========================================================== Example02:\ Example02: Given seconds: 500\ Output: Hours: 0 Minutes: 8 Seconds: 20 In [46]:

# todo

# todo input_sec = int(input("Enter your seconds: ")) hours = input_sec // 3600 remaining_sec = input_sec % 3600 mins = remaining_sec // 60 remaining_sec = remaining_sec % 60 print("Hours:",hours, " Minutes:", mins, " Seconds:",remaining_sec) Enter your seconds: 10000 Hours: 2 Minutes: 46 Seconds: 40

Task 14 Suppose the following expressions are used to calculate the values of L for different values of S:

L = 3000 − 125S 2 if S < 100 L=

12000 if S 4+S 2 /14900

≥ 100

Write a Python code of a program that reads a value of S and then calculates the value of L. ==========================================================

hint(1): You can import math and use math function for making squares math.pow(number, power) Or you can simply write S**2. hint(2): The value of S(user input) will be an integer ========================================================== Example01:\ Example01: Input: 120\ Output: 2416.2162162162163 ========================================================== Example02:\ Example02: Input: 3\ Output: 1875 In [60]:

#Todo import math input_s = int(input("Enter your Value")) input_pow = int(math.pow(input_s, 2)) if input_s < 100: print(3000 - 125 * input_pow) elif input_s >= 100: print(12000 / (4 + input_pow / 14900)) Enter your Value120 2416.2162162162163

Task 15 Take an hour from the user as input and tell it is time for which meal. • The user will input the number in a 24-hour format. So, 14 means 2 pm, 3 means 3 am, 18 means 6 pm, etc.\ • Valid inputs are 0 to 23. Inputs less than 0 or more than 23 are invalid in 24-hour clock.\ • Assume, Input will be whole numbers. For example, 3.5 will NOT be given as input. ========================================================== Inputs: Message to be printed\ 4 to 6: Breakfast\ 12 to 13: Lunch\ 16 to 17: Snacks\ 19 to 20: Dinner\ For all other valid inputs, say "Patience is a virtue"\ For all other invalid inputs, say "Wrong time" ==========================================================

========================================================== For example,\ If the user enters 4, your program should print the message "Breakfast".\ If the user enters 5, your program should print the message "Breakfast".\ If the user enters 6, your program should print the message "Breakfast".\ If the user enters 0, your program should print the message "Patience is a virtue".\ If the user enters 1, your program should print the message "Patience is a virtue".\ If the user enters 18, your program should print the message "Patience is a virtue".\ If the user enters 23, your program should print the message "Patience is a virtue".\ If the user enters 24, your program should print the message "Wrong Time".\ If the user enters -1, your program should print the message "Wrong Time".\ If the user enters 27, your program should print the message "Wrong time". ========================================================== Hints:\ You can use nested conditionals (if-else) or chained conditions (if-elif-else) to solve this problem. In [100]:

#Todo input_number = int(input("Enter a number: ")) if input_number >= 0 and input_number < 24: if input_number >=4 and input_number =12 and input_number =16 and input_number =19 and input_number...


Similar Free PDFs