[go: up one dir, main page]

0% found this document useful (0 votes)
17 views9 pages

Lab Report 02

python
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views9 pages

Lab Report 02

python
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

DEPARTMENT OF COMPUTER & SOFTWARE ENGINEERING

COLLEGE OF E&ME, NUST, RAWALPINDI

DEPARTMENT OF MECHANICAL ENGINEERING COLLEGE


OF E&ME, NUST, RAWALPINDI

Subject: Fundamental of programming Lab Number # 02

SUBMITTED TO:

Instructor Name: Dr Asad Mansoor


Lab Engineer: Fatima Ismail

SUBMITTED BY:

Muhammad Talha
DE- 45 Syn A Mechanical Engineering
REG NO # 481402

SUBMITION DATE: 29 Feb 2024


Objectives:

• Learn conditional statements in python.


• Learn the use of if else in python.
• Learn how to make decisions in python.
• Learn how to use logical operator in python.
.

Hardware/Software required:

Hardware:
. Laptop

Software Tool:
. VS Code

Task 1: Question statement

• Write a python script that asks user to select one of the following operations and
performs the said operation on 2 inputs provided by user such that 0 for Addition, 1 for
Subtraction, 2 Multiplication and 3 for Division operations

Code:

# Take 2 numbers as input from user


num_1 = int(input("Enter the first number:"))
num_2 = int(input("Enter the second number: "))

print(" 0 is for addition \n 1 is for subtraction \n 2 is for multiplication


\n 3 is for division: ") # Ask user
# for what he wants to do with the numbers

operation = int(input("Enter the operation u want to perform in form of


digits from 0 to 3.”")) # Take decision as
# input from user

if(operation == 0): # If user chose to perform addition than this piece of


code will execute

addition =num_1 + num_2 #Doing addition


print("the addition of two numbers is: ",addition) #Printing addition

elif(operation == 1): # If user chose to perform Subtraction than this piece


of code will execute

subtraction = num_1 - num_2 # Doing Subtraction


print("the subtraction of numbers is: ",subtraction) #Printing
Subtraction
elif(operation == 2): # If user chose to perform Multiplication than this
piece of code will execute

multiplication = num_1 * num_2 # Doing Multiplication


print("the multiplication of numbers is: ",multiplication) #Printing
Multiplication

elif (operation == 3): # If user chose to perform Division than this piece
of code will execute

division= num_1 / num_2 # Doing Division


print("the division of numbers is: ",division) #Printing Division

else: # if all of the above entries are false then print Invalid entry
print("Invalid Entry ")

Outputs:

Task 2: Question statement

• Write a program that inputs temperature and value of humidity from user and display
related forecast.

Temperature Humidity Forecast


Greater than 35 50% to 60% Hot Day
Between 25 and 35 35% to 50% Pleasant Day
Less than 25 Less than 50% Cool Day

Code:

# Ask Temperature and Humidity from user as input


temp = float(input("Enter the Temperature: "))
humid = float(input("Enter the Humidity: "))

if (temp > 35) and (humid >= 50) and (humid <= 60): # If temperature is
greater than 35 and humidity is between
# 50% and 60% print it is Hot day

print("It is Hot Day")

elif (temp >= 25) and (temp <= 35) and (humid >= 50) and (humid <= 60): # If
temperature is between 25 and 35 and
# humidity is between 35% to 50% print It is Pleasant Day

print("It is Pleasant Day")

elif (temp < 25) and (humid < 50): # If temperature less than 25 and
# humidity is less than 50% print it is Cool Day
print("It is Cool Day")

else: # if all of the above entries are false then print Invalid entry
print("Invalid entry")

Output

Task 3: Question statement

• Write a program that takes three nonzero integers as input and determines and prints
whether they’re the sides of a right triangle.

CODE:
# Ask three sides as input from user

a = float(input("Enter First side: "))


b = float(input("Enter Second side: "))
c = float(input("Enter Third side: "))

# Check if it is sides of triangle by using Pythagoras theorem and then


print Entered sides are of Right Triangle

if (a²==b²+c² ) or (b²==a²+c² ) or (c²==b²+a² ):


print("Entered sides are of Right Triangle")

else: # if sides are not of right triangle then print Entered sides are
not of Right Triangle

print("Entered sides are not of Right Triangle")

Output:

Task 4: Question statement

Write a program that keeps getting and adding the number until negative number is
entered

CODE:

Code:
total_sum = 0 # Initialize the variable total to store the sum

while True: # Start an infinite loop

num = float(input("Enter a number (enter a negative number to stop): "))


# Prompt the user to enter a number

if num < 0: # Check if the entered number is negative

break # If the number is negative, exit the loop

total_sum += num # Add the entered number to the total

print("The sum of the numbers entered is:", total)

# Print the sum of all the numbers entered

Outputs:

Task 5: Question statement

• A person invests $𝟏𝟎𝟎𝟎 in a savings account yielding 𝟓% interest.


Assuming that all interest is left on deposit in the account, calculate and print the
amount of money in the account at the end of each year for 𝟏𝟎 years.
Use the following formula for determining these amounts: 𝑎 = 𝑝 1 + 𝑟 𝑛
• Where:
• p is the original investment (i.e., the principal)
• r is the annual interest rate
• n is the number of years
• a is the amount on deposit at the end of the 𝒏 𝒕𝒉 year

Code:

print("This program calculate interest and returns its value for each
year.") # Discription

# inputs
p = float(input("Enter initial investment: "))

r = float(input("Enter interest rate: "))

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

count = 1

# loop

while count<=n:

a = p*(1+r)**count

print("Amount in bank after",count,"years is",a)

count+=1 # increment

Output:

Task 6: Question statement

• Write a program to check whether a number is Armstrong or not. (Armstrong number


is a number that is equal to the sum of cubes of its digits for example: 153 = 1^3 + 5^3 +
3^3)

Code:

# Discription

print("This program tells if a given number is Armstrong number or not.")


# input

num = input("Enter a number: ")

length = len(num)

count=0

sum = 0

# loop for iteration

while count<length:

power= (int(num[count]))**length

sum = sum + power

count = count+1

# Condition to check Armstrong number

if sum == int(num):

print("Given number is an Armstrong number.")

else:

print("Number is not an Armstrong.")

Output:

Conclusion:
In this week’s tasks we learned about iterations, using while loops, which is a
strong computing tool in programming languages.

You might also like