0% found this document useful (0 votes)
5 views10 pages

Python Assignment Program

Uploaded by

lonelyhillstar0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
5 views10 pages

Python Assignment Program

Uploaded by

lonelyhillstar0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

Python Assignment Program

1. Input Initial velocity (u), acceleration (f) and time duration (t) from the
user and find the final velocity (v) where v = u + f t.
Answer:-

#Initial all the required variables

u = float(input("Enter the Initial Velocity: ")) # U is refers as Initial Velocity

f = float(input("Enter the Accelaration: ")) # F is refers as the Acceleration

t = float(input("Enter time (in seconds): ")) # T is refers as Time

#finding the final Velocity

v = u+f*t;

print("Final Velocity: ",v)

output

Enter the Initial Velocity: 25


Enter the Accelaration: 10
Enter time (in seconds): 5
Final Velocity: 75.0
2. Input radius ( r ) and height (h) of a cylinder from the user and find the
total surface area and volume where surface area = 2πr(r+h) and
volume = πr2h.
Answer:-

importing math Library

import math

#Initialize all Values

r = float(input("Enter radius of the Cylinder: ")); #radius


h = float(input("Enter Height of the Cylinder: ")); #height

pi = math.pi

#Total Surface area of the Cylinder

total_Surface_Area = 2*pi*r*(r+h)

print("Total Surface area of Cylinder: ",total_Surface_Area)

#Volume of the Cylinder

volume = pi*r*r*h

print("Volume of the Cylinder: ",volume)

output

Enter radius of the Cylinder: 5


Enter Height of the Cylinder: 4
Total Surface area of Cylinder: 282.7433388230814
Volume of the Cylinder: 314.1592653589793
3. Input an integer from the user and check it is odd or even.
Answer:-
#Checking the given number is odd or even

#initialize all the nessary values


num = int(input("Enter a Number: "))

#setting condition for odd or even

if num%2==0:
print("Given Number ",num," is Even")
elif num == 0:
print("Number is Zero")
else:
print("Given Number ",num," is Odd")
output:-
Enter a Number: 5
Given Number 5 is Odd

4.Input 3 sides from the user and check whether triangle can be formed or not. If
yes find the perimeter and area of the triangle.
Answer:-
# Input three values from the user
side_a = float(input("Enter the length of side a: "))
side_b = float(input("Enter the length of side b: "))
side_c = float(input("Enter the length of side c: "))

# Check if the input values form a triangle


if side_a + side_b > side_c and side_b + side_c > side_a and side_c + side_a >
side_b:
# Calculate and print the perimeter
perimeter = side_a + side_b + side_c
print(f"The input values form a triangle with perimeter {perimeter}.")
else:
print("The input values do not form a triangle.")

output:-
Enter the length of side a: 10
Enter the length of side b: 10
Enter the length of side c: 10
The input values form a triangle with perimeter 30.0.

5.Input a year from the user and check whether it is leap year or not.
Answer:-

# Input a year from the user


year = int(input("Enter a year: "))

# Check if the year is a leap year


if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
output:-
Enter a year: 2023
2023 is not a leap year.

6.Input a natural number (n) and display the sum of all the natural numbers till n.
Answer:-

# Input a natural number from the user


n = int(input("Enter a natural number (n): "))

# Ensure that the entered number is positive


if n <= 0:
print("Please enter a positive natural number.")
else:
# Calculate the sum of natural numbers up to n
sum_of_numbers = (n * (n + 1)) // 2

# Print the result


print(f"The sum of all natural numbers up to {n} is: {sum_of_numbers}")
output:-
Enter a natural number (n): 20
The sum of all natural numbers up to 20 is: 210

output:-

7.Input a positive integer and find its factorial. n! = 1x2x3x…x n


Answer:-

# Input a non-negative integer from the user


n = int(input("Enter a non-negative integer (n): "))

# Check if the entered number is non-negative


if n < 0:
print("Please enter a non-negative integer.")
else:
# Initialize the factorial variable
factorial_result = 1
# Calculate the factorial of the entered number
for i in range(1, n + 1):
factorial_result *= i

# Print the result


print(f"The factorial of {n} is: {factorial_result}")
output:-
Enter a non-negative integer (n): 3
The factorial of 3 is: 6

8.Input a positive integer and display all the series: 1, 4, 7, 10, ….., n
Answer:-

# Input a positive integer from the user


n = int(input("Enter a positive integer (n): "))

# Check if the entered number is positive


if n <= 0:
print("Please enter a positive integer.")
else:
# Initialize a variable to store the series
series = []

# Generate the series 1, 4, 10, ..., n


for i in range(1, n + 1):
series_value = i * (i + 1) // 2
series.append(series_value)

# Print the series


print("The series is:", ", ".join(map(str, series)))
output:-
Enter a positive integer (n): 6
The series is: 1, 3, 6, 10, 15, 21

9.Input a positive integer and find the sum of all its digits.
Answer:-

# Input a number from the user


number = int(input("Enter a number: "))
# Ensure that the entered number is non-negative
if number < 0:
print("Please enter a non-negative number.")
else:
# Initialize a variable to store the sum of digits
sum_of_digits = 0

# Calculate the sum of digits


while number > 0:
digit = number % 10
sum_of_digits += digit
number //= 10

# Print the result


print(f"The sum of the digits is: {sum_of_digits}")
output:-
Enter a number: 50
The sum of the digits is: 5

10.Input 2 integers and display the numbers within the given range which are
divisible by 3 or 5.
Answer:-

# Input two integers from the user


start_num = int(input("Enter the starting integer: "))
end_num = int(input("Enter the ending integer: "))

# Ensure that the starting integer is less than or equal to the ending integer
if start_num > end_num:
print("Invalid input. The starting integer should be less than or equal to the ending
integer.")
else:
# Display numbers within the range that are divisible by 3 or 5
print(f"Numbers divisible by 3 or 5 within the range {start_num} to {end_num}:")
for num in range(start_num, end_num + 1):
if num % 3 == 0 or num % 5 == 0:
print(num)
output:-
Enter the starting integer: 4
Enter the ending integer: 10
Numbers divisible by 3 or 5 within the range 4 to 10:
5
6
9
10

11.Input a string check whether in upper case. If yes, convert to lower case.
Answer:-

# Input a string from the user


input_string = input("Enter a string: ")

# Check if the string is in uppercase


if input_string.isupper():
# Convert the string to lowercase
converted_string = input_string.lower()
print(f"The string in lowercase is: {converted_string}")
else:
print("The entered string is not in uppercase.")
output:-
Enter a string: SUMAN
The string in lowercase is: suman

12.Input two strings s1, s2. Check whether s1+s2=s2+s1


Answer:-

# Input two strings from the user


s1 = input("Enter the first string (s1): ")
s2 = input("Enter the second string (s2): ")

# Check if s1 + s2 is equal to s2 + s1
if s1 + s2 == s2 + s1:
print("s1 + s2 is equal to s2 + s1.")
else:
print("s1 + s2 is not equal to s2 + s1.")
ouput:-
Enter the first string (s1): suman
Enter the second string (s2): shah
s1 + s2 is not equal to s2 + s1.

13.Input a list, check how many are odd numbers, how many are even numbers.
Answer:-

# Input a list of numbers from the user


numbers = input("Enter a list of numbers separated by spaces: ").split()

# Convert the input values to integers


numbers = [int(num) for num in numbers]

# Initialize counters for odd and even numbers


odd_count = 0
even_count = 0

# Check each number and update the counters


for num in numbers:
if num % 2 == 0:
even_count += 1
else:
odd_count += 1

# Print the results


print(f"The list contains {even_count} even number(s) and {odd_count} odd
number(s).")
output:-
Enter a list of numbers separated by spaces: 2 3 4 5 6
The list contains 3 even number(s) and 2 odd number(s).

14.Input a list find sum of all numerical values present in it.


Answer:-

# Input a list of values from the user


values = input("Enter a list of values separated by spaces: ").split()

# Initialize a variable to store the sum of numerical values


sum_numerical_values = 0

# Iterate through the values and calculate the sum of numerical values
for value in values:
# Check if the value is a numerical value (integer or float)
if value.replace(".", "", 1).isdigit(): # Removing decimal point for float check
sum_numerical_values += float(value)

# Print the result


print(f"The sum of numerical values in the list is: {sum_numerical_values}")
output:-
Enter a list of values separated by spaces: 4 5 6 7 8 9
The sum of numerical values in the list is: 39.0

15.Input a list of integers. If integer is even divide it by 2. If integer is odd


multiply by 2. Display both input and output lists.
Answer:-
# Input a list of integers from the user
input_list = input("Enter a list of integers separated by spaces: ").split()

# Convert the input values to integers


input_list = [int(num) for num in input_list]

# Perform the specified operations and create the output list


output_list = []
for num in input_list:
if num % 2 == 0:
# If the number is even, divide it by 2
output_list.append(num // 2)
else:
# If the number is odd, multiply it by 2
output_list.append(num * 2)

# Print the input and output lists


print("Input List:", input_list)
print("Output List:", output_list)
output:-
Enter a list of integers separated by spaces: 2 3 4 5 6 7 8
Input List: [2, 3, 4, 5, 6, 7, 8]
Output List: [1, 6, 2, 10, 3, 14, 4]

You might also like