0% found this document useful (0 votes)
4 views6 pages

Number and Geometry Calculations

The document contains multiple Python code snippets for basic programming tasks, including checking if a number is positive, negative, or zero, calculating the area and perimeter of a parallelogram, determining if a number is even or odd, finding the factorial of a number, checking if a year is a leap year, and finding the largest among three numbers. Each snippet includes user input and outputs the result based on the calculations or conditions specified. The examples demonstrate fundamental programming concepts and logic.

Uploaded by

hiralal141164
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)
4 views6 pages

Number and Geometry Calculations

The document contains multiple Python code snippets for basic programming tasks, including checking if a number is positive, negative, or zero, calculating the area and perimeter of a parallelogram, determining if a number is even or odd, finding the factorial of a number, checking if a year is a leap year, and finding the largest among three numbers. Each snippet includes user input and outputs the result based on the calculations or conditions specified. The examples demonstrate fundamental programming concepts and logic.

Uploaded by

hiralal141164
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

Check if a number is positive, negative, or zero

a = float(input('Enter the number: '))


if a >= 0:
if a == 0:
print('The number is zero')
else:
print('The number is a positive number')
else:
print('The number is a negative number')

Output:
Enter the number: 5
The number is a positive number
Calculate the area and perimeter of a parallelogram

b = float(input('Enter the base of parallelogram: '))


w = float(input('Enter the width of parallelogram: '))
h = float(input('Enter the height of parallelogram: '))
Area = b * h
Perimeter = 2 * (b + w)
print('The area of parallelogram is:', Area)
print('The perimeter of parallelogram is:', Perimeter)

Output:
Enter the base of parallelogram: 4
Enter the width of parallelogram: 5
Enter the height of parallelogram: 6
The area of parallelogram is: 24
The perimeter of parallelogram is: 18
Check if a number is even or odd

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


if num % 2 == 0:
print("The number is even")
else:
print("The number is odd")

Output:
Enter a number: 7
The number is odd
Find the factorial of a number

def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)

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


print("Factorial of", num, "is", factorial(num))

Output:
Enter a number: 5
Factorial of 5 is 120
Check if a year is a leap year

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


if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(year, "is a leap year")
else:
print(year, "is not a leap year")

Output:
Enter a year: 2024
2024 is a leap year
Find the largest among three numbers

a = float(input("Enter first number: "))


b = float(input("Enter second number: "))
c = float(input("Enter third number: "))
if (a >= b) and (a >= c):
largest = a
elif (b >= a) and (b >= c):
largest = b
else:
largest = c
print("The largest number is", largest)

Output:
Enter first number: 5
Enter second number: 8
Enter third number: 3
The largest number is 8

You might also like