0% found this document useful (0 votes)
2 views

Python_Cycle_Test_Full_Answers

The document contains Python code solutions for various programming problems, including checking for palindromes, summing series, finding prime numbers, and calculating GCD using the Euclidean algorithm. It also includes functions for generating Fibonacci sequences and checking Armstrong numbers. Each section provides a specific task along with the corresponding code implementation.

Uploaded by

ragulganesh2007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python_Cycle_Test_Full_Answers

The document contains Python code solutions for various programming problems, including checking for palindromes, summing series, finding prime numbers, and calculating GCD using the Euclidean algorithm. It also includes functions for generating Fibonacci sequences and checking Armstrong numbers. Each section provides a specific task along with the corresponding code implementation.

Uploaded by

ragulganesh2007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Python for Data Science - Cycle Test 1 Answers

1a. Check if a number is a palindrome without converting to a string

def is_palindrome(n):
original = n
reverse = 0
while n > 0:
reverse = reverse * 10 + n % 10
n //= 10
return original == reverse

num = int(input("Enter a number: "))


print("Palindrome:", is_palindrome(num))

1b. Sum of series: 1 + 2 + 4 + ... + N

def sum_series(n):
total, term = 0, 1
while term <= n:
total += term
term *= 2
return total

N = int(input("Enter N: "))
print("Sum of series:", sum_series(N))

2a. Prime numbers between two given numbers

def is_prime(n):
if n < 2:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
start = int(input("Enter start: "))
end = int(input("Enter end: "))
print([n for n in range(start, end + 1) if is_prime(n)])

2b. Sum of numbers divisible by 5 but skipping multiples of 10

total = sum(n for n in range(1, 101) if n % 5 == 0 and n % 10 != 0)


print("Sum:", total)

3. Sum of digits in range and count of even sum numbers

def sum_of_digits(n):
return sum(int(d) for d in str(n))

start, end = map(int, input("Enter range: ").split())


even_count = sum(1 for n in range(start, end + 1) if sum_of_digits(n) % 2 == 0)
print("Even sum count:", even_count)

4. Print pattern

n = 5
for i in range(1, n+1):
print(" " * (n-i), str(i) + " " * (2*i-2) + (str(i) if i > 1 else ""))

5a. Factorial using while loop

num = int(input("Enter number: "))


fact, i = 1, 1
while i <= num:
fact *= i
i += 1
print("Factorial:", fact)

5b. Check if a number is an Armstrong number

num = int(input("Enter number: "))


digits = [int(d) for d in str(num)]
print("Armstrong:", sum(d ** len(digits) for d in digits) == num)
6. GCD using Euclidean algorithm

def gcd(a, b):


while b:
a, b = b, a % b
return a

a, b = map(int, input("Enter two numbers: ").split())


print("GCD:", gcd(a, b))

7. Fibonacci sequence and sum of even numbers

def fibonacci(n):
a, b = 0, 1
fib_seq = []
while a <= n:
fib_seq.append(a)
a, b = b, a + b
return fib_seq

N = int(input("Enter N: "))
fib_seq = fibonacci(N)
print("Fibonacci:", fib_seq)
print("Sum of even numbers:", sum(x for x in fib_seq if x % 2 == 0))

You might also like