diff --git a/problems/easy/easy_q1.py b/problems/easy/easy_q1.py index adf9762..ba6d728 100644 --- a/problems/easy/easy_q1.py +++ b/problems/easy/easy_q1.py @@ -1,8 +1,14 @@ # Check Even or Odd: Write a program to check if a given number is even or odd. 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)) diff --git a/problems/easy/easy_q10.py b/problems/easy/easy_q10.py index 7dc1db5..b052934 100644 --- a/problems/easy/easy_q10.py +++ b/problems/easy/easy_q10.py @@ -4,6 +4,15 @@ def is_palindrome(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 @@ -28,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 cee7645..86d704f 100644 --- a/problems/easy/easy_q11.py +++ b/problems/easy/easy_q11.py @@ -10,6 +10,16 @@ def day_of_week(day): 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] @@ -35,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_q16.py b/problems/easy/easy_q16.py index 279747f..16b00c5 100644 --- a/problems/easy/easy_q16.py +++ b/problems/easy/easy_q16.py @@ -3,5 +3,13 @@ def sum_first_last(arr): return arr[0] + arr[-1] if __name__ == "__main__": # Handle the input by Yourself + + 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 2f0840f..ac3dffa 100644 --- a/problems/easy/easy_q17.py +++ b/problems/easy/easy_q17.py @@ -1,9 +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 + + 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 a14e71b..ba8b693 100644 --- a/problems/easy/easy_q18.py +++ b/problems/easy/easy_q18.py @@ -1,7 +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 + + s=input("Enter the string:") + print(last_char_of_string(s)) + s=[7,8,9,6] - print( last_char_of_string(s)) \ No newline at end of file + print( last_char_of_string(s)) + diff --git a/problems/easy/easy_q2.py b/problems/easy/easy_q2.py index ac0a9c6..e77c070 100644 --- a/problems/easy/easy_q2.py +++ b/problems/easy/easy_q2.py @@ -22,6 +22,10 @@ def largest_of_two(a, 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) @@ -47,4 +51,5 @@ def largest_of_two(a, b): - \ No newline at end of file + + diff --git a/problems/easy/easy_q3.py b/problems/easy/easy_q3.py index e874a42..5210fe7 100644 --- a/problems/easy/easy_q3.py +++ b/problems/easy/easy_q3.py @@ -1,6 +1,12 @@ # 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: @@ -42,6 +48,7 @@ def is_leap_year(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 644a47f..6fb7c41 100644 --- a/problems/easy/easy_q4.py +++ b/problems/easy/easy_q4.py @@ -1,6 +1,12 @@ # 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: @@ -32,4 +38,4 @@ def check_number(num): print(res) - + diff --git a/problems/easy/easy_q5.py b/problems/easy/easy_q5.py index 794055c..96fa00c 100644 --- a/problems/easy/easy_q5.py +++ b/problems/easy/easy_q5.py @@ -1,5 +1,5 @@ def grade_system(marks): -<<<<<<< HEAD + if marks >=90 and marks <=100: return "A" elif marks >= 80 and marks <=90: @@ -13,7 +13,7 @@ def grade_system(marks): num = int(input("Enter the Mark : ")) res = grade_system(num) print(res) -======= + if marks >= 90: return "A" elif marks >= 80: @@ -22,13 +22,17 @@ def grade_system(marks): return "C" else: + return "F" + + return "D" return "F" + if __name__ == "__main__": num = int(input("Enter the Mark : ")) res = grade_system(num) print(res) ->>>>>>> c39895b92dbcd0dd59250c4268a660b41d857e3d + diff --git a/problems/easy/easy_q6.py b/problems/easy/easy_q6.py index a7b1fef..f8d83d0 100644 --- a/problems/easy/easy_q6.py +++ b/problems/easy/easy_q6.py @@ -4,6 +4,12 @@ def print_numbers(n): while i <= n: print(i) + i += 1 + +if __name__ == "__main__": + num = int(input("Enter the Number ")) + print_numbers(num) + i+= 1 if __name__ == "__main__": @@ -30,3 +36,4 @@ def print_numbers(n): + diff --git a/problems/easy/easy_q7.py b/problems/easy/easy_q7.py index aaaf4b5..7f13401 100644 --- a/problems/easy/easy_q7.py +++ b/problems/easy/easy_q7.py @@ -1,7 +1,19 @@ -# 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 @@ -21,4 +33,4 @@ def sum_of_digits(num): - + \ No newline at end of file diff --git a/problems/easy/easy_q8.py b/problems/easy/easy_q8.py index 861ba7f..9f8aa51 100644 --- a/problems/easy/easy_q8.py +++ b/problems/easy/easy_q8.py @@ -1,5 +1,13 @@ # 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 @@ -12,8 +20,9 @@ def reverse_number(num): 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 f2dcf9f..2c82f2e 100644 --- a/problems/easy/easy_q9.py +++ b/problems/easy/easy_q9.py @@ -1,9 +1,16 @@ # 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__": @@ -16,7 +23,10 @@ def factorial(n): print(factorial(num)) - + + print(factorial(num)) + + res=factorial(num) print(res) diff --git a/problems/medium/m1.py b/problems/medium/m1.py index 4e4a4a3..a4685d2 100644 --- a/problems/medium/m1.py +++ b/problems/medium/m1.py @@ -34,6 +34,12 @@ def math_operations_menu(choice): 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}") @@ -57,6 +63,7 @@ def math_operations_menu(choice): print("Subtraction:", a - b) elif choice == 1: print("Addition:", a + b) + elif choice == 4: print("Division:", a / b) elif choice == 3: @@ -82,6 +89,10 @@ def math_operations_menu(choice): print(f"{a} to the power of {b} : {a**b}") else: print("Invalid option") + + +math_operations_menu() + math_operations_menu() print("Invalid option!!!") @@ -120,3 +131,4 @@ def math_operations_menu(choice): + diff --git a/problems/medium/m17.py b/problems/medium/m17.py index b0dc40b..c554451 100644 --- a/problems/medium/m17.py +++ b/problems/medium/m17.py @@ -1,5 +1,10 @@ '''Write a program to reverse a given list without using built-in functions''' def reverse_list(lst): + + return lst[::-1] +lst=[1,2,3,4,5] +print(reverse_list(lst)) + start = 0 end = len(lst)-1 while start < end: @@ -13,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/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() +