Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Python Programsfirst18

Uploaded by

s1049bhavya12250
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Python Programsfirst18

Uploaded by

s1049bhavya12250
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Print Hello World

print("Hello World!")

Hello World!

2.Print School Address City

school=input("Enter your school name: ")


address=input("Enter your school address: ")
city=input("Enter your City: ")
print(school,"-",address,"-",city)

SPS - Bhopal - Pune 2.

3.Sum of two numbers.

num1=1.5

num2=6.3

sum=num1+num2

print("sum of two given numbers is: ",sum)

4.Program to find sum and average of three numbers

a=int(input("Enter the 1st number: "))

b=int(input("Enter the 2nd number: "))

c=int(input("Enter the 3rd number: "))

sum=a+b+c

avg=sum/3

print("Sum of all 3 numbers is: ",sum)

print("Average of all 3 numbers is: ",avg)


5. Program to find the area and perimeter of circle
PI = 3.14

R = float(input("Enter radius of the circle: "))

area = (PI*R*R)

perimeter = (2*PI*R)

print("The area of circle is", area)

print("The perimeter of circle is", perimeter)


6. Python find square and cube of given number

n = input("Enter the value :--> ")

n = int(n)

square = n ** 2

cube = n ** 3

print("\nSquare of the number :-->", square)

print("Cube of the number :-->", cube)


7. Python Program to find area and perimeter of rectangle

l=int(input("Length : "))
w=int(input("Width : "))
area=l*w
perimeter=2*(l+w)
print("Area of Rectangle : ",area)
print("Perimeter of Rectangle : ",perimeter)

8. Python Programs to find total,average and percentage of five subjects

english = float(input("Please enter English Marks: "))

math = float(input("Please enter Math score: "))

computers = float(input("Please enter Computer Marks: "))

physics = float(input("Please enter Physics Marks: "))

chemistry = float(input("Please enter Chemistry Marks: "))

total = english + math + computers + physics + chemistry

average = total / 5

percentage = (total / 500) * 100

print("\nTotal Marks = ",total)

print("Average Marks = ",average)

print("Marks Percentage = ",percentage)


9.Python Programs to find the area of triangle

a = float(input('Enter first side: '))


b = float(input('Enter second side: '))
c = float(input('Enter third side: '))
s = (a + b + c) / 2
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is : ',area)
10. Python Program find the largest number among three numbers using if-
else

# Input three numbers

num1 = float(input("Enter the first number: "))

num2 = float(input("Enter the second number: "))

num3 = float(input("Enter the third number: "))

# Compare numbers using if-else

if num1 >= num2 and num1 >= num3:

largest = num1

elif num2 >= num1 and num2 >= num3:

largest = num2

else:

largest = num3

print("The largest number is:", largest)

program to check whether a given number is even or odd:


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

if number % 2 == 0:

print(f"The number {number} is even.")

else:

print(f"The number {number} is odd.")

12.python program to calculate an electricity bill based on usage with different rate slabs using if-else:
units = float(input("Enter electricity usage in units: "))

if units <= 50:

bill = units * 1.5

elif units <= 150:

bill = 50 * 1.5 + (units - 50) * 2.5

elif units <= 250:

bill = 50 * 1.5 + 100 * 2.5 + (units - 150) * 4

else:

bill = 50 * 1.5 + 100 * 2.5 + 100 * 4 + (units - 250) * 6

# Add fixed charge

bill += 50 # Fixed charge for maintenance

print(f"Electricity bill: ₹{bill:.2f}")

13.Python program to print natural numbers up to a given number nnn using a loop:

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

print("Natural numbers up to", n, "are:")

for i in range(1, n + 1):

print(i, end=" ")

14.Python program to calculate the sum of natural numbers up to n using a loop:


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

sum_n = 0

for i in range(1, n + 1):

sum_n += i # Add each number to sum_n

print(f"The sum of natural numbers up to {n} is: {sum_n}")

15.Python program to print the multiplication table of a given number


using a loop:
num = int(input("Enter a number to print its multiplication table: "))

print(f"Multiplication table of {num}:")

for i in range(1, 11):

print(f"{num} * {i} = {num * i}")

16.Python program to print a triangle pattern of stars (*)

n = int(input("Enter the number of rows for the triangle: "))

for i in range(1, n + 1):

print("*" * i) # Print i stars in each row

17.Python program to find the sum and average of even and odd numbers from a set of n
numbers:
n = int(input("Enter the number of elements: "))

# Initialize variables for even and odd sums and counts

even_sum = 0

odd_sum = 0

even_count = 0

odd_count = 0
# Input n numbers and classify as even or odd

for i in range(n):

num = int(input(f"Enter number {i+1}: "))

if num % 2 == 0:

even_sum += num

even_count += 1

else:

odd_sum += num

odd_count += 1

# Calculate the average of even and odd numbers

even_avg = even_sum / even_count if even_count > 0 else 0

odd_avg = odd_sum / odd_count if odd_count > 0 else 0

# Display the results

print(f"Sum of even numbers: {even_sum}")

print(f"Average of even numbers: {even_avg:.2f}")

print(f"Sum of odd numbers: {odd_sum}")

print(f"Average of odd numbers: {odd_avg:.2f}")


18.Function to calculate factorial

def factorial(n):

if n == 0 or n == 1: # Base case: factorial of 0 or 1 is 1

return 1

else:

return n * factorial(n - 1) # Recursive call

num = int(input("Enter a number to find its factorial: "))

result = factorial(num)

print(f"The factorial of {num} is {result}.")

You might also like