Python Problem Solving

Jayed Hossain Jibon Jayed Hossain Jibon Jayed Hossain Jibon Jayed Hossain Jibon Jayed Hossain Jibon Jayed Hossain Jibon

Programiz | PYnative | Geeksforgeeks | w3resource | HackerRank | LeetCode

01. Print Hello world!
Solution
print("Hello World!")
02. Add Two Numbers
Input: num1 = 5, num2 = 10
Output: 15
Explanation: 5 + 10 = 15

Complexity Analysis
    Time Complexity: O(1)
    Auxiliary Space: O(1)
Solution
num1 = 5;
num2 = 10;
sum = num1 + num2;
print(sum) // Output: 15
03. Square Root
Input: num1 = 5, num2 = 10
Output: 15

Complexity Analysis
    Time Complexity: O(1)
    Auxiliary Space: O(1)
Solution
user_input = int(input("Enter your number : "))
output = user_input ** 0.5
print(output)
04. Area of triangle
formula : area = 0.5 * base * height.

Complexity Analysis
    Time Complexity: O(1)
    Auxiliary Space: O(1)
Solution
base = float(input("Enter the base length of the triangle: "))
height = float(input("Enter the height of the triangle: "))
area = 0.5 * base * height
print("The area of the triangle is:", area)
05. Quadratic Equation
Complexity Analysis
    Time Complexity: O(1)
    Auxiliary Space: O(1)
Solution
06. Swap Variable
input  : a = 5
         b = 6
output : a = 6
         b = 5
Complexity Analysis
    Time Complexity: O(1)
    Auxiliary Space: O(1)
Solution
a = 5
b = 6
c = a + b
a = c - a
b = c - b
print(a)
print(b)
# or
a, b = b, a 
print(a)
print(b)
07. Generate a Random Number
output : Random number 
Complexity Analysis
    Time Complexity: O(1)
    Auxiliary Space: O(1)
Solution
import random
x = random.randint(1, 100)
print(x)
08. Positive Negative or
input  : 10
output : Positive
Complexity Analysis
    Time Complexity: O(1)
    Auxiliary Space: O(1)
Solution
number = 10
if number > 0:
    print("Positive")
elif number < 0:
        print("Negative")
elif number == 0:
     print("0")
08. Positive Negative or
input  : 10
output : Positive
Complexity Analysis
    Time Complexity: O(1)
    Auxiliary Space: O(1)
Solution
number = 10
if number > 0:
        print("Positive")
elif number < 0:
         print("Negative")
elif number == 0:
         print("0")
09. Even or odd Number
input  : 10
output : Even number
Complexity Analysis
    Time Complexity: O(1)
    Auxiliary Space: O(1)
Solution
number = 10
if number % 2 == 0:
    print("Even number")
else:
     print("Odd number")
10. Check Leap Year
input  : 2024
output : Leap Year
Complexity Analysis
    Time Complexity: O(1)
    Auxiliary Space: O(1)
Solution
year = int(input("Enter your your : "))
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
    print("Leap Year")
else:
    print("Not leap year ")
11. Find the Largest Among Three Numbers
input  : a = 5
         b = 6
         c = 10
output : C is large number
Complexity Analysis
    Time Complexity: O(1)
    Auxiliary Space: O(1)
Solution
a =  int(input("Enter your 1st number : "))
b = int(input("Enter your 2nd number : "))
c = int(input("Enter your 3rd number : "))
if a > b:
    print("A is largest number")
elif b > c:
    print("B is largest number ")
elif a < c:
    print("C is largest number")
12. Check Prime Number
input  : 13
output : Its Prime 
Definition :
A prime number can be defined as a natural number greater than 1
whose only factors are 1 and the number itself.
Solution
is_prime = int(input("Enter your number : "))
for i in range(2, is_prime):
    if is_prime % i == 0:
        print("Not a prime number ")
        break
else:
    print("Its Prime")
13. Prime numbers within an interval
input  : 900, 1000
output : Its Prime 
Solution
lower = int(input("Enter your lower number : "))
upper = int(input("Enter your upper number : "))
for num in range(lower, upper + 1):
   if num > 1:
       for i in range(2, num):
           if (num % i) == 0:
               break
       else:
           print(num)
14. Factorial of a Number
input  : 5
output : 120
Solution
num = 5
factorial = 1
for i in range(1,num + 1):
    factorial = factorial*i
print("The factorial of",num,"is",factorial)
15. Display the multiplication Table
Solution
number = int(input("Enter your number : "))
for i in range(1, 11):
    print(number,'x', i, '=', i*number)
# or
num = 1
while num <=10:
    print(num,'x', num, '=', num*num)
    num = num + 1
16. Python Program to Print the Fibonacci sequence
Solution num = 7 factorial = 1 if num < 0: print("Sorry, factorial does not exist for negative numbers") elif num == 0: print("The factorial of 0 is 1") else: for i in range(1,num + 1): factorial = factorial*i print("The factorial of",num,"is",factorial)

⬆ Back to Top