Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
947 views

Python Coding Interview Questions - 3

This document contains Python coding interview questions and solutions related to finding the sum of digits of a number, reversing a number, checking if a number is strong or perfect, finding factors of a number, and adding two fractions. It includes code examples and test cases for each problem.

Uploaded by

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

Python Coding Interview Questions - 3

This document contains Python coding interview questions and solutions related to finding the sum of digits of a number, reversing a number, checking if a number is strong or perfect, finding factors of a number, and adding two fractions. It includes code examples and test cases for each problem.

Uploaded by

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

1/31/23, 2:05 PM Python Coding interview Questions - 3 - Jupyter Notebook

Dharavath Ramdas

Python Coding Interview Questions and Answers link


github link : https://lnkd.in/gNZs5b9N (https://lnkd.in/gNZs5b9N)

Python Coding Interview Questions - 3

Q.14 Program to find the sum of digits of a given number in Python

In [2]:

def sum_digits(num):
sum = 0
for digit in str(num):
sum = sum + int(digit)
return "Sum of digits :",sum
sum_digits(int(input("Enter Number :")))

Enter Number :123

Out[2]:

('Sum of digits :', 6)

In [4]:

sum_digits(int(input("Enter Number :")))

Enter Number :9182

Out[4]:

('Sum of digits :', 20)

In [5]:

sum_digits(int(input("Enter Number :")))

Enter Number :5537

Out[5]:

('Sum of digits :', 20)

In [6]:

sum_digits(int(input("Enter Number :")))

Enter Number :5050

Out[6]:

('Sum of digits :', 10)

Q.15 Program to Reverse a given number in Python ?

In [9]:

def reverse_number(num):
return "Reversed Number :",(str(num)[::-1])
reverse_number(int(input("Enter number :")))

Enter number :123

Out[9]:

('Reversed Number :', '321')

In [10]:

reverse_number(int(input("Enter number :")))

Enter number :567

Out[10]:

('Reversed Number :', '765')

localhost:8888/notebooks/Python Coding interview Questions - 3.ipynb 1/3


1/31/23, 2:05 PM Python Coding interview Questions - 3 - Jupyter Notebook

In [11]:

reverse_number(int(input("Enter number :")))

Enter number :199

Out[11]:

('Reversed Number :', '991')

In [13]:

reverse_number(int(input("Enter number :")))

Enter number :389

Out[13]:

('Reversed Number :', '983')

Q.16 Program to check whether a given number is Strong or Not in Python ?

In [18]:

# identify individual digits


# find factorial for digits
# sum all the factorial of individual digits

In [2]:

n = int(input())
temp = n
sum = 0
while n>0:
rem = n%10
fact = 1

print("digits",rem)
for i in range(1,rem+1):
fact = fact*i
print("factorial",fact)
sum = sum + fact
print("sum :",sum)
n = n//10
if sum == temp:
print(temp,"is strong number ")
else:
print(temp,"not strong number ")

145
digits 5
factorial 120
sum : 120
digits 4
factorial 24
sum : 144
digits 1
factorial 1
sum : 145
145 is strong number

Q.17 Program to check given numberis perfect number or not in python ?

In [3]:

# A number which is equal to sum of all its factors except given number is known as perfect number
# n = 6
# fact of 6 1,2,3
# sum 1+2+3 = 6 so perfect number
# 6%1=0,652=0,6%3=0,6%4=2,6%5=1

localhost:8888/notebooks/Python Coding interview Questions - 3.ipynb 2/3


1/31/23, 2:05 PM Python Coding interview Questions - 3 - Jupyter Notebook

In [8]:

n=int(input("Enter Number :"))


sum = 0
for i in range(1,n):
if n%i == 0:
print(i,end=" ")
sum = sum + i
if sum == n:
print(n,"is a perfect number")
else:
print(n,"is not a perfect number ")

Enter Number :6
1 2 3 6 is a perfect number

Q.18 Factors of agiven number ?

In [15]:

n = int(input("Enter Number :"))


for i in range(1,n+1):
if (n%i == 0):
print("Factors of a number {}:".format(n),i)

Enter Number :8
Factors of a number 8: 1
Factors of a number 8: 2
Factors of a number 8: 4
Factors of a number 8: 8

Q.19 Addition of two Fraction in python ?

In [18]:

a = int(input("Enter Number :"))


b = int(input("Enter Number :"))
c = int(input("Enter Number :"))
d = int(input("Enter Number :"))
result1 = (a*d)+(c*b)
result2 = (b*d)
print(result1/result2)
print("the result {}/{} result is :".format(result1,result2),result1/result2)

Enter Number :2
Enter Number :3
Enter Number :2
Enter Number :3
1.3333333333333333
the result 12/9 result is : 1.3333333333333333

In [ ]:

localhost:8888/notebooks/Python Coding interview Questions - 3.ipynb 3/3

You might also like