0% found this document useful (0 votes)
0 views

Basic_Python_Problems

The document contains several basic Python problems with corresponding functions, including calculating the sum of a sum-series of natural numbers, finding the greatest of three numbers, summing odd and even numbers in an array, checking if a string is a palindrome, calculating the mean of array elements, determining if a year is a leap year, finding the factorial of a number, and checking if a number is prime. Each problem is accompanied by a function definition and example input/output statements. The code is structured to allow user interaction through input prompts.

Uploaded by

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

Basic_Python_Problems

The document contains several basic Python problems with corresponding functions, including calculating the sum of a sum-series of natural numbers, finding the greatest of three numbers, summing odd and even numbers in an array, checking if a string is a palindrome, calculating the mean of array elements, determining if a year is a leap year, finding the factorial of a number, and checking if a number is prime. Each problem is accompanied by a function definition and example input/output statements. The code is structured to allow user interaction through input prompts.

Uploaded by

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

Basic Python Problems

Sum of Sum-Series of First N Natural Numbers


# Function to find sum of sum-series of first N natural numbers
def sum_of_series(n):
total_sum = 0
for i in range(1, n + 1):
total_sum += sum(range(1, i + 1))
return total_sum

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


print("Sum of sum-series:", sum_of_series(n))

Greatest of Three Numbers


# Function to find greatest of three numbers
def greatest(a, b, c):
if a >= b and a >= c:
return a
elif b >= a and b >= c:
return b
else:
return c

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


b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
print("Greatest number:", greatest(a, b, c))

Sum of Odd and Even Numbers in an Array


# Function to find sum of odd and even numbers separately
def sum_odd_even(arr):
odd_sum = 0
even_sum = 0
for num in arr:
if num % 2 == 0:
even_sum += num
else:
odd_sum += num
return odd_sum, even_sum
arr = list(map(int, input("Enter array elements separated by space: ").split()))
odds, evens = sum_odd_even(arr)
print("Sum of odd numbers:", odds)
print("Sum of even numbers:", evens)

Check if a String is Palindrome


# Function to check if a string is palindrome
def is_palindrome(s):
return s == s[::-1]

s = input("Enter a string: ")


if is_palindrome(s):
print("Palindrome")
else:
print("Not a palindrome")

Find Mean of Elements in an Array


# Function to find mean of elements in an array
def mean(arr):
if len(arr) == 0:
return 0
return sum(arr) / len(arr)

arr = list(map(int, input("Enter array elements separated by space: ").split()))


print("Mean of elements:", mean(arr))

Check if a Year is Leap Year


# Function to check if a year is a leap year
def is_leap_year(year):
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
return True
return False

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


if is_leap_year(year):
print("Leap Year")
else:
print("Not a Leap Year")
Find Factorial of a Number
# Function to find factorial of a number
def factorial(n):
if n < 0:
return "Invalid input, factorial is not defined for negative numbers"
if n == 0 or n == 1:
return 1
fact = 1
for i in range(2, n + 1):
fact *= i
return fact

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


print("Factorial:", factorial(n))

Check if a Number is Prime


# Function to check if a number is prime
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True

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


if is_prime(n):
print("Prime Number")
else:
print("Not a Prime Number")

You might also like