Problem Set2
Digits in a factor
Q.
Given a number ‘n’, design an algorithm and write the Python program to
print the digits of ‘n’ that divides ‘n’. Print the digits in reverse
order of their appearance in the number ‘n’. For example, if n is 122
then print 2, 2, 1. Use only conditional and iterative statements to write the
code. If none of the digits divide the number, then print ‘No factors’
Input
Format
A
number, n
Output
Format
Digits
in the number ‘n’ that divides ‘n’ in reverse order
Input:
Get
the input number and store it in num1
Processing:
let
temp = num1
let
temp1 = 0
let
count = 0
while
temp!=0
temp1 =
temp%10
if
num1%temp1 == 0
print(temp1)
count+=1
Output:
Print
the factors of the number in the reverse order if any else display no factors.
Algorithm:
Step1.
Get the number and store it in variable n.
Step2. Assign count as 0 and let copy
as n.
Step3.
Repeat till copy is not equal to zero
Step3.1
Let temp as copy%10
Step3.2
if the remainder obtained when n is
divided by temp is zero
Step3.2.1
if true display temp and increment count by 1
Step4. if count is equal to zero then display no factor
Step5. End
Flowchart:
To edit this click on this
Program:
n=int(input())
temp = n
count = 0
length = len(str(n))
for i in range(length):
factor=temp%10
if(n%factor==0):
print(factor)
count+=1
count+=1
temp=temp//10
if(count==0):
print('No factors')
No comments:
Post a Comment