DS010923 - Practice Problems Based on Functions(03-10-2023)

1.Python program to Check weather a Given Number is Strong or not.
2.Python Problem to Check weather a Given Number is Armstrong or not.
3.Python Program to check weather a Given Number is Palindrome or not.
Python Program to Check weather a Given Number is Prime or not
5.Python Program to check weather a Given Number is Fibonacci Number.

Note → All These Problems must be solved with the help Fucntions and All the inputs **
** must be taken from the end User.

2 Likes

#Python program to Check weather a Given Number is Strong or not.
from math import factorial as f

def strong(num):
sum=0
for i in str(num):
sum =sum + f(int(i))
return sum
x= 145
t= strong(x)
if x == t :
print(‘yes it is a strong number’)
else:
print(‘not a strong number’)

secomnd way

def fact(n):
t=1
for i in range(1, n+1):
t = t*i
return t
num=int(input(‘enter the number:’))
sum=0
for i in str(num):
sum = sum+fact(int(i))
if num == sum:
print(‘strong number’)
else:
print(‘not a strong number’)

Python Problem to Check weather a Given Number is Armstrong or not.

def arm(num):
sum =0
l = len(str(num))
for i in str(num):
sum = sum + (int(i))**l
return sum
n= int(input(‘enter number:’))
x=arm(n)
if n==x:
print(’ yes it is a armstrong number’)
else:
print(‘not an armstrong number’)

Python Program to check weather a Given Number is Palindrome or not.

def poli(num):
r=int(str(num)[::-1])
return r
n =int(input(‘enter the number:’))
x = poli(n)
if x == n:
print(‘yes it is polindrome’)
else:
print(‘not a polindrome’)

Python Program to Check weather a Given Number is Prime or not

def prime(n):
if n==1:
return False
for i in range(2,n):
if n%i == 0:
return False
return True
num =int(input(‘enter the number:’))
x = prime(num)
if x:
print(‘prime’)
else:
print(‘not a prime’)

second way

def prime(n):
t=0
if n==1:
t=1
for i in range(2,n):
if n%i ==0:
t=1
break
return t
num =int(input(‘enter the number:’))
x = prime(num)
if x==0:
print(‘prime number’)
else:
print(‘not a prime’)

Python Program to check weather a Given Number is Fibonacci Number.

def fib(n):
if n < 0:
return False
elif n == 0:
return True
else:
a, b = 0, 1
while b < n:
a, b = b, a + b
return b == n
num = int(input("Enter a number: "))
if fib(num):
print(‘it is a Fibonacci number.’)
else:
print(‘it is not a Fibonacci number.’)

#1 strong number

import math

def strong_number(num):
sum = 0
original_num = num

while num > 0:
    digit = num % 10
    sum += math.factorial(digit)
    num = num//10

return sum == original_num

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

if strong_number(num):
print(“given number is strng number”)
else:
print(“given number is not strong number”)

#2.armstrong number.

def armstrong_number(num):

sum = 0
original = num
count = len(str(num))

while num != 0:
    rem = num % 10
    sum = sum + (rem ** count)
    num = num // 10
return sum == original

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

if armstrong_number(num):
print(num ,“is an Armstrong number.”)
else:
print(num,“is not an Armstrong number.”)

#3.palindrome number

def palindrome(num):
string1 = str(num)
return string1 == string1[::-1]

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

if palindrome(num):
print(num,“is a palindrome.”)
else:
print(num,“is not a palindrome.”)

#4prime numbers

def prime(num):
if num > 1:
for i in range(2, num):
if num % i == 0:
print(“Composite number”)
break
else:
print(“Prime number”)
else:
print(“Number is not a prime number”)

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

Ans:

import math
num =int(input("Enter a Number: "))
temp = num
sum = 0
while num!=0:
rem = num%10
print(“reminder is:”,rem)
sum = sum+(math.factorial(rem))
print(“sum is”,sum)
num = num//10
print(“Number value is”,num)

if temp==sum:
print(“Number is a Stong Number”)
else:
print(“Number is Not a Stong number”)

2.Python Problem to Check weather a Given Number is Armstrong or not.

Ans:
num = int(input("Enter a Number: "))
order = len(str(num))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10
if num == sum:
print(num,“is an Armstrong number”)
else:
print(num,“is not an Armstrong number”)

3Python Program to check weather a Given Number is Palindrome or not.

Ans:
num = int(input("Enter a Number: "))
temp = num
reverse = 0
while temp > 0:
remainder = temp % 10
reverse = (reverse * 10) + remainder
temp = temp // 10
if num == reverse:
print(“The Number is a Palindrome”)
else:
print(“The Number is Not Palindrome”)

4.Python Program to check weather a Given Number is Fibonacci Number.

Ans:
import math

def isPerfectSquare(number):
s=int (math.sqrt(number))
return s*s == number

def isFibonacci(number):
if isPerfectSquare(5*(number2)+4) or isPerfectSquare(5*(number2)-4):
return True
else:
return False

num = int(input(“Enter a Number: “))
if isFibonacci(num):
print(f”{num} is a Fibonacci Number”)
else:
print(f"{num} is Not a Fibonacci Number")

#1)Python program to Check weather a Given Number is Strong or not.
def factorial(d):
if(d==1 or d==0):
return 1
return d*factorial(d-1)
def is_strong(n):
num = n
sum=0
while(n>0):
digit=n%10
sum=sum+factorial(digit)
n=n//10
if (sum==num):
return True
else:
return False
print(“Enter a number:”)
a=int(input())
print(is_strong(a))

#2) Python Problem to Check weather a Given Number is Armstrong or not.
def isArmstrong(num):
sum=0
temp=num
l=len(str(num))
while(temp!=0):
r=temp%10
sum=sum+pow(r,num)
temp=temp//10
return(sum==num)
num=int(input(“Enter a number:”))
print(isArmstrong(num))

#3)Python Program to check weather a Given Number is Palindrome or not.

def palindrome(num):
str1 = str(num)
return str1 == str[::-1]
num = int(input("Enter a number: "))
if palindrome(num):
print(“Given Number is a Palindrome”)
else:
print(“Given Number is not a Palindrome”)

1.Python program to Check weather a Given Number is Strong or not.

def fact (number):
Sum=1
for i in range(1,number+1):
Sum=Sum*i
return(Sum)

def strong (num):
temp = num
sum = 0
while num!=0:
rem = num%10
sum = sum+(fact(rem))
num = num//10
if temp==sum:
print("Your Number : ",temp,“is a Strong Number”)
else:
print(“Your Number : “,temp,” is not a Strong Number”)

a=int(input("Enter a Number : ",))
print(strong(a))

2.Python Problem to Check weather a Given Number is Armstrong or not.

image

3.Python Program to check weather a Given Number is Palindrome or not.

image

1.Python program to Check weather a Given Number is Strong or not.

n=int(input("Enter the number: "))
def factorial(n):
fact=1
for i in range(1,n+1):
fact=fact*i
return fact

def strong(n):
temp=n
sum=0
while n!=0:
rem=n%10
sum=sum+factorial(rem)
n=n//10
if sum==temp:
return True
else:
return False
n= strong(n)
if n:
print(" It is a strong number")
else:
print(“It is not a strong number”)

2.Python Problem to Check weather a Given Number is Armstrong or not.

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

def armstrong(n):
count=(len(str(n)))
temp=n
sum=0
while n!=0:
rem=n%10
sum=sum+rem**count
n=n//10
if sum==temp:
return True
else:
return False
n= armstrong(n)
if n:
print(" It is a Armstrong number")
else:
print(“It is not a Armstrong number”)

3.Python Program to check weather a Given Number is Palindrome or not.

n=int(input("Enter the number: "))
def palindrome(n):
reverse=str(n)
return reverse==reverse[::-1]
if n:
print(“It is a panlindrome number”)
else:
print(“It is not a palindrome number”)

4.Python Program to Check weather a Given Number is Prime or not

n = int(input("Enter a number: "))
def is_prime():
if n > 1:
for i in range(2, n):
if n % i == 0:
return False
return True
else:
return False
if is_prime():
print(“It is a prime number.”)
else:
print(“It is not a prime number.”)

Q-1.Python program to Check weather a Given Number is Strong or not.

=====================================================================

import math
def strong_number(num):
sum = 0
while num!=0:
rem = num%10
sum = sum + math.factorial(rem)
num = num//10
return sum

num = int(input(“Enter a Number :”))
temp = num
num2 = strong_number(num)

if temp==num2:
print("Number is a Strong Number ")
else:
print(“Number is not a Strong Number”)

Q-2.Python Problem to Check weather a Given Number is Armstrong or not.

=======================================================================

num = int(input(“Enter a Number :”))
count = len(str(num))
temp = num
def Armstrong_number(num):
sum = 0
while num!=0:
rem = num%10
sum = sum+(rem**count)
num = num//10
return sum

num2=Armstrong_number(num)

if temp==num2:
print("Number is a Armstrong Number ")
else:
print(“Number is not a Armstrong Number”)

Q-3.Python Program to check weather a Given Number is Palindrome or not.

========================================================================

def Palindrom_number(num):
string = ‘’
while num!=0:
rem = num%10
string = string + str(rem)
num = num//10
return string

num = int(input(“Enter your number :”))
temp = num

num2 = Palindrom_number(num)

if temp == int(num2):
print(“Number is palindrome”)
else:
print(“Number is not palindrome”)

Q-4.Python Program to Check weather a Given Number is Prime or not

==================================================================

number = int(input(“Enter a Number :”))

def Prime_number(number):
if number>1:
for i in range(2,number):
if number%i==0:
print(“Number is not a Prime”)
break
else:
print(“Number is Prime”)

Prime_number(number)

Q-5.Python Program to check weather a Given Number is Fibonacci Number.

=======================================================================

import math

def is_perfect_square(number):
s= int(math.sqrt(number))
return s*s == number

def is_fibonacci_number(number):
if is_perfect_square(5*(number2)+4) or is_perfect_square(5*(number2)-4):
return True
else:
return False

num = int(input(“Enter your fibonacci number :”))
if is_fibonacci_number(num):
print(“Given number is fibonacci number”)
else:
print(“Given number is not fibonacci number”)