Some basic programs in Python

Problem 1: Finding the Largest of Three Numbers in Python

a = int(input("Enter a: "))
b = int(input("Enter b: "))
c = int(input("Enter c: "))

if a >= b and a >= c:
    print("a is the largest")
elif b >= a and b >= c:
    print("b is the largest")
else:
    print("c is the largest")


Problem 2: Find the Factorial of a Number

num = int(input("Enter a number: "))
factorial = 1
if num < 0:
    print("Factorial is not defined for negative numbers.")
elif num == 0:
    print("The factorial of 0 is 1")
else:
    for i in range(1, num + 1):
        factorial *= i
    print("The factorial of", num, "is", factorial)


Problem 3: Print the Fibonacci Series

num = int(input("Enter the number of terms: "))
fibonacci = [0, 1]
while len(fibonacci) < num:
    next_num = fibonacci[-1] + fibonacci[-2]
    fibonacci.append(next_num)
print("The Fibonacci series is:", fibonacci)


Problem 4: Print the Prime Number Series

def prime(x, y):
    prime_list = []
    for i in range(x, y):
        if i == 0 or i == 1:
            continue
        else:
            for j in range(2, int(i/2)+1):
                if i % j == 0:
                    break
            else:
                prime_list.append(i)
    return prime_list
 
starting_range = 2
ending_range = 7
lst = prime(starting_range, ending_range)
if len(lst) == 0:
    print("There are no prime numbers in this range")
else:
    print("The prime numbers in this range are: ", lst)


Problem 5: Reverse a String

string = input("Enter a string: ")
reverse_string = string[::-1]
print("The reversed string is:", reverse_string)
Next Post Previous Post
No Comment
Add Comment
comment url