Advertisement
brandblox

python lab (06/02/2024)

Feb 6th, 2024
852
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.68 KB | None | 0 0
  1. #Reverse
  2. num = int(input("Enter a number: "))
  3. reversed_num = 0
  4. while num > 0:
  5.     digit = num % 10
  6.     reversed_num = (reversed_num * 10) + digit
  7.     num = num // 10
  8. print(f"The reversed number: {reversed_num}")
  9.  
  10. #fibonacchi
  11. num_terms = int(input("Enter the number of terms for the Fibonacci series: "))
  12. if num_terms <= 0:
  13.     print("Invalid input. Please enter a positive integer.")
  14. else:
  15.     fib_series = [0, 1]
  16.     while len(fib_series) < num_terms:
  17.         fib_series.append(fib_series[-1] + fib_series[-2])
  18.     sum_fib_series = sum(fib_series)
  19.     print(f"The sum of the first {num_terms} terms in the Fibonacci series is: {sum_fib_series}")
  20.  
  21. #armstrong
  22. num = int(input("Enter a number: "))
  23. original_num = num
  24. num_digits = len(str(num))
  25. sum_of_digits = 0
  26. while num > 0:
  27.     digit = num % 10
  28.     sum_of_digits += digit ** num_digits
  29.     num = num // 10
  30. if original_num == sum_of_digits:
  31.     print(f"{original_num} is an Armstrong number.")
  32. else:
  33.     print(f"{original_num} is not an Armstrong number.")
  34.  
  35.  
  36. #positive negative sum
  37. sum_positive = 0
  38. sum_negative = 0
  39. for i in range(10):
  40.     num = float(input(f"Enter number {i + 1}: "))
  41.  
  42.     if num > 0:
  43.         sum_positive += num
  44.     elif num < 0:
  45.         sum_negative += num
  46.  
  47. print(f"Sum of positive numbers: {sum_positive}")
  48. print(f"Sum of negative numbers: {sum_negative}")
  49.  
  50.  
  51. #palindrome
  52.  
  53. num = int(input("Enter a number: "))
  54. original_num = num
  55. reversed_num = 0
  56. while num > 0:
  57.     digit = num % 10
  58.     reversed_num = (reversed_num * 10) + digit
  59.     num = num // 10
  60. if original_num == reversed_num:
  61.     print(f"{original_num} is a palindrome.")
  62. else:
  63.     print(f"{original_num} is not a palindrome.")
  64.  
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement