[go: up one dir, main page]

0% found this document useful (0 votes)
7 views6 pages

Basic Questions

The document contains several Python functions for common mathematical operations, including reversing a number, checking for prime numbers, calculating factorials, identifying palindromes, generating Fibonacci sequences, summing digits, checking Armstrong numbers, and finding the greatest common divisor (GCD). Each function is accompanied by example test cases demonstrating its usage. The functions are designed to handle various numerical inputs and provide specific outputs based on the operation performed.

Uploaded by

sujatabadiger232
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

Basic Questions

The document contains several Python functions for common mathematical operations, including reversing a number, checking for prime numbers, calculating factorials, identifying palindromes, generating Fibonacci sequences, summing digits, checking Armstrong numbers, and finding the greatest common divisor (GCD). Each function is accompanied by example test cases demonstrating its usage. The functions are designed to handle various numerical inputs and provide specific outputs based on the operation performed.

Uploaded by

sujatabadiger232
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Reverse

def reverse_number_math(n):
reversed_num = 0
n = abs(n)
while n > 0:
digit = n % 10 # Get the last digit
reversed_num = reversed_num * 10 + digit
n = n // 10 # Remove the last digit
return reversed_num
# Example
num = 12345
print(reverse_number_math(num)) # Output: 54321

Prime
def is_prime(n):
if n <= 1:
return False
for i in range(2, n // 2 + 1):
if n % i == 0:
return False
return True
# Test
print(is_prime(29)) # Output: True
Factorial
def factorial(n):
fact = 1
for i in range(2, n + 1):
fact *= i
return fact
# Test
print(factorial(5)) # Output: 120

Palindrome
def is_palindrome(n):
original = n
reversed_num = 0
while n > 0:
reversed_num = reversed_num * 10 + n % 10
n = n // 10
return original == reversed_num
# Test
print(is_palindrome(121)) # Output: True

Fionacci
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
print(a, end=' ')
a, b = b, a + b
# Test
fibonacci(10) # Output: 0 1 1 2 3 5 8 13 21 34

Prime Numbers in a Given Range (C-style Python)


def is_prime(n):
if n <= 1:
return False
for i in range(2, n // 2 + 1):
if n % i == 0:
return False
return True

def primes_in_range(start, end):


for num in range(start, end + 1):
if is_prime(num):
print(num, end=' ')
# Test
start = 10
end = 50
primes_in_range(start, end) # Output: 11 13 17 19 23 29 31 37 41 43 47

Palindrome Numbers in a Given Range


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

def palindromes_in_range(a, b):


for i in range(a, b + 1):
if is_palindrome(i):
print(i, end=' ')
# Test
a = 100
b = 200
palindromes_in_range(a, b)
# Output: 101 111 121 131 141 151 161 171 181 191

Sum of Digits
def sum_of_digits(n):
n = abs(n) # Handle negative numbers
total = 0
while n > 0:
total += n % 10
n = n // 10
return total
# Test
num = 12345
print(sum_of_digits(num)) # Output: 15

Armstrong
def is_armstrong(n):
original = n
num_digits = 0
temp = n

# Count number of digits


while temp > 0:
num_digits += 1
temp = temp // 10

# Calculate the sum of digits raised to the power of num_digits


temp = n
result = 0
while temp > 0:
digit = temp % 10
result += digit ** num_digits
temp = temp // 10
return result == original
# Test
num = 153
print(is_armstrong(num)) # Output: True

GCD
def gcd_euclidean(a, b):
while b != 0:
a, b = b, a % b
return a

# Test
print(gcd_euclidean(36, 60)) # Output: 12

You might also like