Join GyanXp WhatsApp Channel! - Join Now
Practice exam questions, execute code live, and track your overall progress!
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
num = 29
if is_prime(num):
print(f"{num} is a Prime Number!")
else:
print(f"{num} is Not a Prime Number.")
โn ensures optimization. If no factor is found, the number is prime.n_terms = 10
n1, n2 = 0, 1
count = 0
if n_terms <= 0:
print("Please enter a positive integer")
elif n_terms == 1:
print("Fibonacci sequence:", n1)
else:
print("Fibonacci sequence:")
while count < n_terms:
print(n1, end=" ")
nth = n1 + n2
n1 = n2
n2 = nth
count += 1
print()
n1 and n2 to track and compute subsequent sequence terms.def is_palindrome(s):
rev_s = s[::-1]
return s.lower() == rev_s.lower()
my_str = "Madam"
if is_palindrome(my_str):
print(f"'{my_str}' is a Palindrome!")
else:
print(f"'{my_str}' is Not a Palindrome.")
[::-1] reverses string order efficiently.def factorial(n):
if n == 1 or n == 0:
return 1
else:
return n * factorial(n - 1)
num = 5
print(f"The factorial of {num} is {factorial(num)}")
n == 0 or 1.Not registered yet? Sign up
By login, you agree to our Privacy & Policy & Disclaimer