0% found this document useful (0 votes)
10 views7 pages

Number System Test Solutions

The document provides a series of Python programs that implement various number-related functions, including checking for Emirp numbers, validating dates, identifying Armstrong numbers, counting even and odd digits, converting decimal to hexadecimal, determining happy numbers, generating Fibonacci series, and classifying numbers as evil or odious. Each section includes function definitions and input prompts for user interaction. The document serves as a comprehensive guide for basic number manipulation in Python.

Uploaded by

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

Number System Test Solutions

The document provides a series of Python programs that implement various number-related functions, including checking for Emirp numbers, validating dates, identifying Armstrong numbers, counting even and odd digits, converting decimal to hexadecimal, determining happy numbers, generating Fibonacci series, and classifying numbers as evil or odious. Each section includes function definitions and input prompts for user interaction. The document serves as a comprehensive guide for basic number manipulation in Python.

Uploaded by

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

Number Test Solutions

1. Write a Function to return “True” if the given number is EMIRP NUMBER


otherwise return “False”.

def isPrime(n):
if n == 0 or n == 1:
return False

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

def getReverse(n):
rev = 0
while n != 0:
digit = n % 10
rev = rev * 10 + digit
n = n//10
return rev

def isEmirp(n):
return isPrime(n) and isPrime(getReverse(n))

n = int(input("Enter the number:"))

if isEmirp(n):
print("{} is an Emirp Number".format(n))
else:
print("{} is not an Emirp Number".format(n))

Number Test Solutions 1


2. Write a Python program to check the validity of the date.

def isValid(d,m,y):
daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31]

if m < 1 or m > 12 or y < 1:


return False

if (y % 4 == 0 and y % 100 != 0) or y % 400 == 0:


daysInMonth[1] = 29

if d < 1 or d > daysInMonth[m-1]:


return False

return True

d = int(input("Enter the day:"))


m= int(input("Enter the month:"))
y = int(input("Enter the year:"))

print("{}/{}/{}".format(d,m,y))
if isValid(d,m,y):
print("It is a Valid Date")
else:
print("It is an invalid Date")

3. Write a Python program to print all 3-digit ARMSTRONG numbers.

def getCount(num):
num = str(num)
count = len(num)
return count

Number Test Solutions 2


def isArmStrong(num):
sum = 0
temp = num
count = getCount(num)
while temp != 0 :
digit = temp % 10
sum = sum + digit ** count
temp = temp // 10
return sum == num

num = int(input("Enter the number:\n"))

for i in range(100,1000):
if isArmStrong(i):
print(i)

4. Write a function to return number of EVEN digits and number of ODD digits
in the number.

def getEvenOddCount(n):

evenCount = 0
oddCount = 0

while n != 0:
digit = n%10

if digit % 2 == 0:
evenCount = evenCount+1
else:
oddCount = oddCount+1

n = n//10

Number Test Solutions 3


return [evenCount,oddCount]

n = int(input("Enter the number:"))

a = getEvenOddCount(n)
print("Number of even digits in the number:",a[0])
print("Number of odd digits in the number:",a[1])

5. Write a python program to convert DECIMAL to HEXADECIMAL number.

def decimalToHexaDec(num):
hexaDec = ""

if num == 0:
return 0

while num != 0:

rem = num % 16
if rem<10:
hexaDec = str(rem) + hexaDec
elif rem == 10:
hexaDec = "A" + hexaDec
elif rem == 11:
hexaDec = "B" + hexaDec
elif rem == 12:
hexaDec = "C" + hexaDec
elif rem == 13:
hexaDec = "D" + hexaDec
elif rem == 14:
hexaDec = "E" + hexaDec
elif rem == 15:
hexaDec = "F" + hexaDec

Number Test Solutions 4


num = num //16

return hexaDec

dec = int(input("Enter decimal number:"))


print(f'The binary format of {dec} is {decimalToHexaDec(dec)}

Another Logic:

def decimalToHexadecimal(num):
hexChars = "0123456789ABCDEF"
hexaDecimal = ""

while num != 0:
rem = num % 16
hexaDecimal = hexChars[rem] + hexaDecimal
num = num //16
return hexaDecimal

6. Write a function to return “True” if the number is HAPPY NUMBER


otherwise return “False”.

def isHappy(num):
while num > 9:
sum = 0
while num != 0:
digit = num % 10
sum = sum + digit * digit
num = num // 10
num = sum
return num == 1 or num ==7

num = int(input("Enter the number:\n"))

Number Test Solutions 5


if isHappy(num):
print("Given number is Happy number")
else :
print("Given number is not a Happy number")

7. Write a python program to print FIBBONACCI SERIES within ‘n’

num = int(input("Enter the last number:\n"))

f1 = 0
f2 = 1
while f1 <= num:
print(f1,end=" ")
f3 = f1 + f2
f1 = f2
f2 = f3

8. Write a Python program to check whether the number is EVIL or ODIOUS


15 -> Convert to Binary -> 1111 -> if even number of 1’s -> Evil Number
16 -> Convert to Binary -> 10000 -> if odd number of 1’s -> Odious Number

def decimalToBinary(num):
binary = ""

if num == 0:
return 0

while num != 0:
rem = num % 2
binary = str(rem) + binary
num = num //2

Number Test Solutions 6


return binary

n = int(input("Enter decimal number:"))

a = decimalToBinary(n)
count1 = 0

for i in a:
if i == "1":
count1 = count1+1

if count1 % 2 == 0:
print("{} is an Evil Number".format(n))
else:
print("{} is an Odious Number".format(n))

Number Test Solutions 7

You might also like