diff --git a/problems/easy/easy_q1.py b/problems/easy/easy_q1.py index 759376b..ba6d728 100644 --- a/problems/easy/easy_q1.py +++ b/problems/easy/easy_q1.py @@ -2,15 +2,19 @@ num = int(input("Enter a number: ")) +if (num % 2) != 0: + print("{0} is Odd".format(num)) +else: + if (num % 2) == 0: + + print("{0} is Even".format(num)) else: print("{0} is Odd".format(num)) -if (num % 2) != 0: - print("{0} is Odd".format(num)) -else: - print("{0} is Even".format(num)) + + diff --git a/problems/easy/easy_q10.py b/problems/easy/easy_q10.py index 3ad440b..b052934 100644 --- a/problems/easy/easy_q10.py +++ b/problems/easy/easy_q10.py @@ -3,10 +3,29 @@ def is_palindrome(num): original = num reverse = 0 + + while True: + reverse = reverse * 10 + num % 10 + num //= 10 + if num == 0: + break + return reverse == original + + + + while True: + reverse = reverse * 10 + num % 10 + num //= 10 + if num != 0: + break + while num != 0: reverse = reverse * 10 + num % 10 num //= 10 + + + return reverse == original reverse = '' @@ -18,9 +37,13 @@ def is_palindrome(num): else: return "It is not a Palindrome" + if __name__ == "__main__": nums = int(input("Enter the Number: ")) res = is_palindrome(nums) print(res) - - + + + + + diff --git a/problems/easy/easy_q11.py b/problems/easy/easy_q11.py index 182540b..86d704f 100644 --- a/problems/easy/easy_q11.py +++ b/problems/easy/easy_q11.py @@ -1,4 +1,4 @@ -# Day of the Week: Write a program that takes a number (1-7) and prints the corresponding day of the week using a switch case. +# def day_of_week(day): switch = { 1: "Monday", @@ -9,6 +9,26 @@ def day_of_week(day): 6: "Saturday", 7: "Sunday" } + + if day <=7 : + return switch[8] + else: + print("Error occured") +if __name__ == "__main__": + day = int(input("Enter a number (1-7) to get the day of the week: ")) + result = day_of_week(day) + print(result) + + + if day < 1 or day > 7: + return "Invalid day" + return switch[day] +if __name__ == "__main__": + xcd = day_of_week(32) + print(xcd) + + + return switch[day] if __name__ == "__main__": @@ -25,5 +45,4 @@ def day_of_week(day): print(xcd) else: print("Enter Valid Input") - - + \ No newline at end of file diff --git a/problems/easy/easy_q12.py b/problems/easy/easy_q12.py index 1a02b99..c762cae 100644 --- a/problems/easy/easy_q12.py +++ b/problems/easy/easy_q12.py @@ -13,7 +13,11 @@ def calculator(a, b, operator): n2 = int(input("Enter the Number 2 :")) opr = input("Enter the Operator :") result = calculator(n1,n2,opr) + + print(result) + print(result) + print(result) \ No newline at end of file diff --git a/problems/easy/easy_q13.py b/problems/easy/easy_q13.py index 24e98a6..90d0a0c 100644 --- a/problems/easy/easy_q13.py +++ b/problems/easy/easy_q13.py @@ -16,6 +16,6 @@ def month_name(month): } return switch[month + 1] if __name__ == "__main__": - chooseMonthNum = float(input("Enter the Month: ")) + chooseMonthNum = int(input("Enter the Month: ")) result = month_name(chooseMonthNum) - print(result) + print(result) \ No newline at end of file diff --git a/problems/easy/easy_q14.py b/problems/easy/easy_q14.py index eae63b0..10153a9 100644 --- a/problems/easy/easy_q14.py +++ b/problems/easy/easy_q14.py @@ -7,8 +7,11 @@ def vowel_or_consonant(char): 'o': "Vowel", 'u': "Vowel", } - return switch.get(char, "Vowel") + return switch.get(char.lower(),"Constant") if __name__ == "__main__": - characterInput = int(input("Enter the charactrer : ")) - res = vowel_or_consonant(characterInput) - print(res) + characterInput = input("Enter the charactrer : ") + if len(characterInput)==1: + res = vowel_or_consonant(characterInput) + print(res) + else: + print("Please enter exactly one character.") \ No newline at end of file diff --git a/problems/easy/easy_q15.py b/problems/easy/easy_q15.py index 2868314..c13c8bf 100644 --- a/problems/easy/easy_q15.py +++ b/problems/easy/easy_q15.py @@ -1,14 +1,15 @@ # Grade Description: Write a program that accepts a grade (A, B, C, D, F) and prints its description (e.g., A = Excellent, B = Good, etc.) using a switch case. def grade_description(grade): switch = { - 'A': "Good", - 'B': "Average", - 'C': "Poor", - 'D': "Excellent", + 'A': "Excellent", + 'B': "Good", + 'C': "Average", + 'D': "Poor", 'F': "Fail" } - return switch.get(grade, "Not a valid grade") + return switch.get(grade.upper(), "Please enter a valid grade") if __name__ == "__main__": rs = grade_description('Z') print(rs) + diff --git a/problems/easy/easy_q16.py b/problems/easy/easy_q16.py index b810a85..16b00c5 100644 --- a/problems/easy/easy_q16.py +++ b/problems/easy/easy_q16.py @@ -1,6 +1,15 @@ # Print the Sum of First and Last Array Element def sum_first_last(arr): - return arr[1] + arr[-1] + return arr[0] + arr[-1] if __name__ == "__main__": # Handle the input by Yourself - sum_first_last() \ No newline at end of file + + arr=[] + n=int(input("Enter the Total number of Elements:")) + for i in range(n): + x=int(input("Enter the element :")) + arr.append(x) + + arr=[2,5,8,7,6] + + print(sum_first_last(arr)) \ No newline at end of file diff --git a/problems/easy/easy_q17.py b/problems/easy/easy_q17.py index 7a5356c..ac3dffa 100644 --- a/problems/easy/easy_q17.py +++ b/problems/easy/easy_q17.py @@ -1,7 +1,13 @@ # Print X N Times -def print_x_n_times(x, n): - for i in range(1, n): # Bug: Loop runs one less time than expected - print(x) +def print_x_n_times(x, n): # Bug: Loop runs one less time than expected + print(x*n) if __name__ == "__main__": # Handle the input by Yourself - print_x_n_times() \ No newline at end of file + + n=int(input("Enter the number of times:")) + x="X" + + x=input("Enter a character:") + n=int(input("enter the number of times to loop:")) + + print_x_n_times(x,n) \ No newline at end of file diff --git a/problems/easy/easy_q18.py b/problems/easy/easy_q18.py index abfc1ea..ba8b693 100644 --- a/problems/easy/easy_q18.py +++ b/problems/easy/easy_q18.py @@ -1,6 +1,12 @@ # Print Last Character of String def last_char_of_string(s): - return s[-2] # Bug: Fetches second-to-last character instead of last + return s[-1] # Bug: Fetches second-to-last character instead of last if __name__ == "__main__": # Handle the input by Yourself - last_char_of_string() \ No newline at end of file + + s=input("Enter the string:") + print(last_char_of_string(s)) + + s=[7,8,9,6] + print( last_char_of_string(s)) + diff --git a/problems/easy/easy_q2.py b/problems/easy/easy_q2.py index 110f086..e77c070 100644 --- a/problems/easy/easy_q2.py +++ b/problems/easy/easy_q2.py @@ -2,42 +2,54 @@ def largest_of_two(a, b): if a > b: - return a + return a else: return b +if __name__=="__main__": + a = int(input("Enter the First Number :")) + b = int(input("Enter the Second Number :")) + res = largest_of_two(a,b) + print(res) + + + return a + + return b + + return a + else: + return b if __name__ == "__main__": num1 = int(input("Enter the First Number :")) num2 = int(input("Enter the Second Number :")) + + res = largest_of_two(num1,num1) + print(res,"is the largest number") + res = largest_of_two(num1,num2) + + print(res) + + + print(res) + print(res) + print(num1,"larger") else: + return b print(num2,"larger") + + if __name__ == "__main__": num1 = int(input("Enter the First Number :")) num2 = int(input("Enter the Second :")) - res = largest_of_two(num1,num1) + res = largest_of_two(num1,num2) print(res) - return a - - return a - - else: - return b -if __name__ == "__main__": - num1 = int(input("Enter the First Number :")) - num2 = int(input("Enter the Second Number :")) - res = largest_of_two(num1,num2) - keerthi - print("The Largest Number is",res) - print(res) - - - main diff --git a/problems/easy/easy_q3.py b/problems/easy/easy_q3.py index 2a6b7cc..5210fe7 100644 --- a/problems/easy/easy_q3.py +++ b/problems/easy/easy_q3.py @@ -1,6 +1,18 @@ # Leap Year or Not: Write a program to determine whether a given year is a leap year. def is_leap_year(year): + if year % 4 != 0 and year % 100 == 0 or year % 400 != 0: + return "Not a Leap Year" + else: + return "Leap Year" + + + if year % 4 == 0 or year % 100 == 0 or year % 400 == 0: + return "Leap Year" + else: + return "Not a Leap Year" + + if year % 4 == 0: if year % 100 == 0: return "Not a Leap Year" @@ -10,6 +22,16 @@ def is_leap_year(year): if year % 4 == 0 and year % 100 != 0 or year % 400 == 0: + return "Leap Year" + else: + return "not a Leap Year" + + + return "Leap Year" + else: + return "Not Leap Year" + + return "Leap Year" return "Not a Leap Year" @@ -23,6 +45,10 @@ def is_leap_year(year): return "Not a Leap Year" + + + + if __name__ == "__main__": num = int(input("Enter the number :")) diff --git a/problems/easy/easy_q4.py b/problems/easy/easy_q4.py index ec24801..6fb7c41 100644 --- a/problems/easy/easy_q4.py +++ b/problems/easy/easy_q4.py @@ -1,5 +1,19 @@ # Positive, Negative, or Zero: Accept a number and check if it is positive, negative, or zero. def check_number(num): + + print("The given number is Negative" if (num < 0 ) else "The given number is Positive" if (num > 0) else "The given number is Zero") +if __name__ == "__main__": + num = int(input("Enter the Number : ")) + check_number(num) + + + if num > 0: + print("positive") + elif num < 0: + print("negative") + else: + print("Number is zero") + if num < 0: print("Negative") elif num > 0: @@ -7,6 +21,7 @@ def check_number(num): else: keerthi print("Number is Zero") + if __name__ == "__main__": num = int(input("Enter the Number : ")) @@ -17,8 +32,10 @@ def check_number(num): if __name__ == "__main__": num = int(input("Enter the Number : ")) res = check_number(num) - print(res) - main - + + + print(res) + + diff --git a/problems/easy/easy_q5.py b/problems/easy/easy_q5.py index 01db7d7..96fa00c 100644 --- a/problems/easy/easy_q5.py +++ b/problems/easy/easy_q5.py @@ -1,6 +1,19 @@ -# Grading System: Write a program that takes a student’s marks as input and prints the grade (A, B, C, or F) based on given thresholds. - def grade_system(marks): + + if marks >=90 and marks <=100: + return "A" + elif marks >= 80 and marks <=90: + return "B" + elif marks >= 70 and marks <=80: + return "C" + else: + return "D" + +if __name__ == "__main__": + num = int(input("Enter the Mark : ")) + res = grade_system(num) + print(res) + if marks >= 90: return "A" elif marks >= 80: @@ -8,9 +21,18 @@ def grade_system(marks): elif marks >= 70: return "C" else: + + return "F" + + + return "D" + return "F" + + if __name__ == "__main__": num = int(input("Enter the Mark : ")) res = grade_system(num) print(res) + diff --git a/problems/easy/easy_q6.py b/problems/easy/easy_q6.py index 2a3d8c5..f8d83d0 100644 --- a/problems/easy/easy_q6.py +++ b/problems/easy/easy_q6.py @@ -3,6 +3,27 @@ def print_numbers(n): i = 1 while i <= n: print(i) + + i += 1 + +if __name__ == "__main__": + num = int(input("Enter the Number ")) + print_numbers(num) + + i+= 1 + +if __name__ == "__main__": + n= int(input("Enter the Number ")) + res = print_numbers(n) + + + + i+= 1 +if __name__ == "__main__": + num = int(input("Enter the Number: ")) + res = print_numbers(num) + print(res) + i += 1 if __name__ == "__main__": @@ -13,3 +34,6 @@ def print_numbers(n): num = int(input("Enter the Number :")) print_numbers(num) + + + diff --git a/problems/easy/easy_q7.py b/problems/easy/easy_q7.py index 0b3053c..7f13401 100644 --- a/problems/easy/easy_q7.py +++ b/problems/easy/easy_q7.py @@ -1,20 +1,36 @@ -# Sum of Digits: Write a program to calculate the sum of digits of a number using a while loop. def sum_of_digits(num): total = 0 while num > 0: + + total += num % 10 + num //= 10 + return total + +if __name__ == "__main__": + num = int(input("Enter the Number: ")) + if num < 0: + num = -num + result = sum_of_digits(num) + print(f"Sum of digits: {result}") + + total += num % 10 + num = num//10 + num = num // 10 num //= 10 - + return total if __name__ == "__main__": num = int(input("Enter the Number : ")) + print(sum_of_digits(num)) + res=sum_of_digits(num) print(res) - + \ No newline at end of file diff --git a/problems/easy/easy_q8.py b/problems/easy/easy_q8.py index 526f5ab..9f8aa51 100644 --- a/problems/easy/easy_q8.py +++ b/problems/easy/easy_q8.py @@ -1,13 +1,28 @@ # Reverse a Number: Accept a number and print its reverse using a while loop. def reverse_number(num): + + rev = "" + while num != 0: + digit = num % 10 #digit = 123%10 = 3 12%10 + rev = rev + str(digit) #rev = 3 + num //= 10 # num//= 10 = 12 + return rev + rev = '' while num != 0: digit = num % 10 + + rev = rev*10+ digit + num //= 10 + return rev + rev = rev + str(digit) num //= 10 return int(rev) + + if __name__ == "__main__": - num = int(input("Enter num : ")) + num = int(input("Enter num : ")) #123 res = reverse_number(num) print(res) diff --git a/problems/easy/easy_q9.py b/problems/easy/easy_q9.py index df9a503..2c82f2e 100644 --- a/problems/easy/easy_q9.py +++ b/problems/easy/easy_q9.py @@ -1,16 +1,32 @@ # Factorial of a Number: Write a program to calculate the factorial of a given number using a while loop. def factorial(n): result = 1 + + i=1 + while i <= n: + result *= i + i += 1 + while n > 0: result *= n n -= 1 + return result if __name__ == "__main__": + + n= int(input("Enter the Number :")) + print(factorial(n)) + + num = int(input("Enter the Number :")) print(factorial(num)) - + + + print(factorial(num)) + + res=factorial(num) print(res) diff --git a/problems/medium/m1.py b/problems/medium/m1.py index e5d9805..a4685d2 100644 --- a/problems/medium/m1.py +++ b/problems/medium/m1.py @@ -1,15 +1,69 @@ -''' -Create a menu to perform basic mathematical operations (addition, subtraction, multiplication, division, modulo) on two numbers. - -''' def math_operations_menu(choice): - a, b = map(int, input("Enter two numbers(separated by commas): ").split(sep=",")) + try: + a, b = map(int, input("Enter two numbers (separated by a comma): ").split(sep=",")) + + # Perform the operation based on user's choice + if choice == 1: + print(f"Addition of {a} and {b}: {a + b}") + elif choice == 2: + print(f"Subtraction of {a} and {b}: {a - b}") + elif choice == 3: + print(f"Multiplication of {a} and {b}: {a * b}") + elif choice == 4: + if b == 0: + print("Error! Division by zero is not allowed.") + else: + print(f"Division of {a} and {b}: {a / b}") + elif choice == 5: + if b == 0: + print("Error! Modulo by zero is not allowed.") + else: + print(f"Modulus of {a} and {b}: {a % b}") + elif choice == 6: + print(f"{a} to the power of {b}: {a ** b}") + else: + print("Invalid option!!!") + except ValueError: + print("Invalid input! Please enter two valid numbers separated by a comma.") + + +# Menu for mathematical operations +print("-------------Mathematical Operation Menu---------------") + + + a=int(input("Enter first numbers: ")) + b=int(input("Enter second numbers: ")) + + + if choice == 2: + print("Subtraction:", a - b) + elif choice == 1: + print("Addition:", a + b) + + if choice == 1: + + print(f"Addition of {a} and {b} : {a + b}") + + print("Addition:", a + b) + elif choice == 2: + print("Subtration:", a - b) + elif choice == 3: + print("Multiplication:", a * b) + elif choice == 4: + print("Division:", a / b) + elif choice == 5: + print("Modulo:", a // b) + else: + print("Invalid option") +math_operations_menu() + if choice == 2: print("Subtraction:", a - b) elif choice == 1: print("Addition:", a + b) + elif choice == 4: print("Division:", a / b) elif choice == 3: @@ -17,29 +71,64 @@ def math_operations_menu(choice): if choice == 1: print(f"Subtraction of {a} and {b}:{a - b}") + elif choice == 2: - print(f"Addition of {a} and {b}:{a + b}") + print(f"Subtraction of {a} and {b} : {a - b}") elif choice == 3: - print(f"Division of {a} and {b}:{a / b}") + print(f"Division of {a} and {b} : {a / b}") elif choice == 4: + + print(f"Multiplication of {a} and {b} : {a * b}") + elif choice == 5: + print(f"Modulus of {a} and {b} : {a % b}") + print(f"Multiplication of {a} and {b}:{a * b}") elif choice == 5: print(f"Modulus of {a} and {b}:{a // b}") - elif choice == 6: - print(f"{a} to the power of {b}:{a**b}") + elif choice == 6: + print(f"{a} to the power of {b} : {a**b}") else: print("Invalid option") + + +math_operations_menu() + math_operations_menu() print("Invalid option!!!") print("-------------Mathematical operation menu---------------") + +print("1. Subtract") +print("2. Add") +print("3. Multiply") +print("4. Divide") +print("5. Modulus") +print("6. Exponential") +======= + print("1. Add") print("2. Subtract") print("3. Multiply") print("4. Divide") print("5. Modulus") + +print("6. Exponential") + +# Get user's choice for operation +try: + choice = int(input("Enter your choice: ")) + math_operations_menu(choice) +except ValueError: + print("Invalid choice! Please enter a number between 1 and 6.") + +print("-------------------------------------------------------") + print("6. Expontential") + choice = int(input("Enter your choice: ")) math_operations_menu(choice) print("-------------------------------------------------------") - \ No newline at end of file + + + + diff --git a/problems/medium/m10.py b/problems/medium/m10.py index 89f6b41..a8046f3 100644 --- a/problems/medium/m10.py +++ b/problems/medium/m10.py @@ -2,7 +2,7 @@ def game_menu(): while True: - print("\n1. Number Guessing Game") + print("1. Number Guessing Game") print("2. Rock-Paper-Scissors") print("3. Dice Roll Simulation") print("4. Exit") @@ -31,5 +31,16 @@ def game_menu(): break else: print("Invalid Choice") + +game_menu() + + +x=input("Can we play some games?? (type yes and no)") +if x=="yes": + game_menu() +else: + print("Okey , let's play later!") + -game_menu() \ No newline at end of file +game_menu() + \ No newline at end of file diff --git a/problems/medium/m11.py b/problems/medium/m11.py index a6422c7..e09e520 100644 --- a/problems/medium/m11.py +++ b/problems/medium/m11.py @@ -1,36 +1,36 @@ # Menu driven program to calculate perimeter and area of different shapes def per_circle(radius): - perimeter = 3.14 * radius - print("Perimeter of Circle: ", perimeters) + perimeter =2* 3.14 * radius + print("Perimeter of Circle: ", perimeter) def per_triangle(side1, side2, side3): - perimeter = side1 + side3 + perimeter = side1 + side2+ side3 print("Perimeter of Triangle: ", perimeter) def per_rectangle(height, width): - perimeter = (height + width) - print("Perimeter of Rectangle: ", perimeters) + perimeter = 2*(height + width) + print("Perimeter of Rectangle: ", perimeter) def per_square(side): - perimeter = side * 3 + perimeter = side * 4 print("Perimeter of Square: ", perimeter) def a_circle(radius): - area = 3.14 * radius ** 2 - print("Area of Circle: ", areas) + area = 3.14 * (radius ** 2) + print("Area of Circle: ", area) def a_triangle(base, height): area = base * height print("Area of Triangle: ", area) def a_rectangle(height, width): - area = height + width + area = height * width print("Area of Rectangle: ", area) def a_square(side): - area = side * 3 + area = side *2 print("Area of Square: ", area) print("\nWELCOME TO MENSURATION PROGRAM! TRY CALCULATING PERIMETER AND AREA OF DIFFERENT GEOMETRIC SHAPES.") @@ -53,10 +53,10 @@ def a_square(side): choice1 = int(input("\nEnter choice for calculations: ")) if choice1 == 1: radius = float(input("Enter Radius of Circle: ")) - per_circles(radius) + per_circle(radius) elif choice1 == 2: radius = float(input("Enter Radius of Circle: ")) - a_circle() + a_circle(radius) elif choice1 == 3: break else: @@ -69,13 +69,13 @@ def a_square(side): print("3. Exit") choice1 = int(input("\nEnter choice for calculations: ")) if choice1 == 1: - side1 = int(input("Enter length of side1: ")) - side2 = int(input("Enter length of side2: ")) - side3 = int(input("Enter length of side3: ")) - per_triangle(side1, side2) + side1 = float(input("Enter length of side1: ")) + side2 = float(input("Enter length of side2: ")) + side3 = float(input("Enter length of side3: ")) + per_triangle(side1,side2,side3) elif choice1 == 2: base = float(input("Enter base of triangle: ")) - height = input("Enter height of triangle: ") + height = float(input("Enter height of triangle: ")) a_triangle(base, height) elif choice1 == 3: break @@ -89,12 +89,13 @@ def a_square(side): print("3. Exit") choice1 = int(input("\nEnter choice for calculations: ")) if choice1 == 1: - height = int(input("Enter height of rectangle: ")) - per_rectangle(height) + height = float(input("Enter height of rectangle: ")) + width = float(input("Enter width of rectangle: ")) + per_rectangle(height,width) elif choice1 == 2: height = float(input("Enter height of rectangle: ")) width = int(input("Enter width of rectangle: ")) - a_rectangle(height) + a_rectangle(height,width) elif choice1 == 3: break else: @@ -107,10 +108,10 @@ def a_square(side): print("3. Exit") choice1 = int(input("\nEnter choice for calculations: ")) if choice1 == 1: - side = int(input("Enter side of square: ")) + side = float(input("Enter side of square: ")) per_square(side) elif choice1 == 2: - side = input("Enter side of square: ") + side = int(input("Enter side of square: ")) a_square(side) elif choice1 == 3: break diff --git a/problems/medium/m12.py b/problems/medium/m12.py index 7692543..48b725c 100644 --- a/problems/medium/m12.py +++ b/problems/medium/m12.py @@ -9,8 +9,10 @@ def multiply(x, y): return x * y def divide(x, y): - return x / y - + try: + return x / y + except Exception as e : + print(f"Error occured due to {e}") while True: print("\nMENU") print("1. Add") @@ -23,7 +25,9 @@ def divide(x, y): if choice == '5': break - + elif choice not in "1234" : + print("Invalid Choice") + continue num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) @@ -35,8 +39,5 @@ def divide(x, y): result = multiply(num1, num2) elif choice == '4': result = divide(num1, num2) - else: - print("Invalid choice") - continue - + print("Result: ", result) diff --git a/problems/medium/m13.py b/problems/medium/m13.py index 33dfa3f..ffad2e1 100644 --- a/problems/medium/m13.py +++ b/problems/medium/m13.py @@ -4,12 +4,15 @@ def is_prime(num): if num < 2: return False - for i in range(2, num): - if num % i == 0: + n=num // 2 + 1 + for i in range(2, n): + if n % i == 0: return False return True n = int(input("Enter a number: ")) -for i in range(2, n + 1): - if is_prime(i): - print(i) + +if is_prime(n): + print(f"The given number {n} is a not prime number ") +else: + print(f"The given number {n} is a prime number ") \ No newline at end of file diff --git a/problems/medium/m15.py b/problems/medium/m15.py index ab58cd0..afcb2e3 100644 --- a/problems/medium/m15.py +++ b/problems/medium/m15.py @@ -1,30 +1,16 @@ '''Write a program to check if a number is an Armstrong number (e.g., 153 = 1^3 + 5^3 + 3^3 ) .''' -def count_digits(n): - i = 0 - while n > 0: - n //= 10 - i += 1 - return i +a=input("Enter Number:") +def Arms(a): + e=0 + for i in a: + c=int(i) + d=c*c*c + e+=d + return e -def sum(n): - i = count_digits(n) - s = 0 - temp=n - while temp > 0: - digit = temp%10 - temp//= 10 - s += pow(digit,i) - return s - - -num = 1634 - - -s = sum(num) - - -if s == num: - print('Given number is an Armstrong Number') +b=Arms(a) +f=int(a) +if b==f: + print(b,"is an Armstrong Number") else: - print('Given number is not an Armstrong Number') - + print(b,"is not An Armstrong Number") \ No newline at end of file diff --git a/problems/medium/m17.py b/problems/medium/m17.py index e857a27..c554451 100644 --- a/problems/medium/m17.py +++ b/problems/medium/m17.py @@ -1,16 +1,16 @@ '''Write a program to reverse a given list without using built-in functions''' def reverse_list(lst): - start = -1 - end = -len(lst) - - - while start > end: - + + return lst[::-1] +lst=[1,2,3,4,5] +print(reverse_list(lst)) + + start = 0 + end = len(lst)-1 + while start < end: lst[start], lst[end] = lst[end], lst[start] - - - start -= 1 - end += 1 + start += 1 + end -= 1 return lst @@ -18,4 +18,5 @@ def reverse_list(lst): print("Original list:", input_list) reversed_list = reverse_list(input_list) -print("Reversed list:", reversed_list) \ No newline at end of file +print("Reversed list:", reversed_list) + \ No newline at end of file diff --git a/problems/medium/m2.py b/problems/medium/m2.py index 2b42991..e0f8021 100644 --- a/problems/medium/m2.py +++ b/problems/medium/m2.py @@ -3,8 +3,10 @@ def array_operations_menu(): print("2. Largest Element") print("3. Smallest Element") print("4. Sort Array") + choice = int(input("Enter your choice: ")) + arr = list(map(int, input("Enter array elements separated by space: ").split())) if choice == 1: @@ -14,6 +16,13 @@ def array_operations_menu(): elif choice == 3: print("Smallest Element:", min(arr)) elif choice == 4: + + arr.sort() # sorts the array in ascending order by default + print("Sorted Array:", arr) + else: + print("Invalid option") +array_operations_menu() + arr.sort() print("Sorted Array:", arr) @@ -25,5 +34,4 @@ def array_operations_menu(): else: print("Invalid option") array_operations_menu() - - + \ No newline at end of file diff --git a/problems/medium/m3.py b/problems/medium/m3.py index 5d5d7b8..045664c 100644 --- a/problems/medium/m3.py +++ b/problems/medium/m3.py @@ -24,7 +24,12 @@ def string_manipulation_menu(): elif choice == 4: old = input("Substring to replace: ") new = input("Replacement substring: ") + + s = s.replace(old, new) + print("Updated String:", s) + print("Updated String:", s.replace(old,new)) + else: print("Invalid option") string_manipulation_menu() \ No newline at end of file diff --git a/problems/medium/m8.py b/problems/medium/m8.py index 89c7cb1..689a6c6 100644 --- a/problems/medium/m8.py +++ b/problems/medium/m8.py @@ -8,23 +8,41 @@ def conversion_menu(): choice = int(input("Enter your choice: ")) if choice == 1: + + celsius = float(input("Enter temperature in Celsius: ")) + fahrenheit = (celsius * 9 / 5) + 32 + celsius = int(input("Enter temperature in Celsius: ")) fahrenheit = (celsius * 9 / 5) + 32 + print("Temperature in Fahrenheit:", fahrenheit) elif choice == 2: - fahrenheit = int(input("Enter temperature in Fahrenheit: ")) - celsius = (fahrenheit - 32) * 5 / 9 + fahrenheit = float(input("Enter temperature in Fahrenheit: ")) + celsius = (fahrenheit - 32) * 5 / 9 print("Temperature in Celsius:", celsius) elif choice == 3: decimal = int(input("Enter a decimal number: ")) + + print(f"Binary: {bin(decimal)[2:]}, Octal: {oct(decimal)[2:]}, Hexadecimal: {hex(decimal)[2:].upper()}") # Correct formatting + print(f"Binary: {bin(decimal)}, Octal: {oct(decimal)}, Hexadecimal: {hex(decimal)}") + elif choice == 4: km = float(input("Enter distance in kilometers: ")) miles = km * 0.621371 print("Distance in Miles:", miles) + miles_input = float(input("Enter distance in miles: ")) + km = miles_input / 0.621371 + print("Distance in Kilometers:", km) elif choice == 5: print("Exiting...") break else: print("Invalid Choice") -conversion_menu() \ No newline at end of file + + +if __name__ == "__main__": + conversion_menu() + +conversion_menu() +