Home > Software design >  Why am I not getting desired output in this code
Why am I not getting desired output in this code

Time:10-28

I want to write a code for this question: write a code which creates a new number 'n2' which consists reverse order of digits of a number 'n' which divides it without any remainder for example if input is 122 it will print 221 because 1,2,2 can divide 122 without any remainder another example is 172336 here 1,2,3,3,6 can divide it without any remainder so the output should be 63321(reverse order). my code is:

n = str(input())
x = ""
z = 0
for z in range(len(n)):
    if int(n)%int(n[z])==0:
        x = n[z]   ""
    else:
        n.replace(n[z],"")
    z = z 1
print(x[::-2])

if I input the number 122 here i get the output 2 but i should be getiing output of 221 why.

CodePudding user response:

This should do the Trick

num = input()
new_num = ""

for i in num:

    if int(num)%int(i) == 0:
        new_num  = i

print(new_num[::-1])

CodePudding user response:

The reason why you are not getting the desired output in this code is because of several logical and syntactical errors in your code. Here are some of them:

  • You are using x = n[z] "" to append the digits that divide n without any remainder, but this will overwrite the previous value of x every time. You should use x = x n[z] instead.
  • You are using n.replace(n[z],"") to remove the digits that do not divide n without any remainder, but this will not modify the original value of n, since strings are immutable in Python. You should assign the result of n.replace(n[z],"") to a new variable, or use a different approach to filter out the unwanted digits.
  • You are using z = z 1 to increment the loop variable, but this is unnecessary and redundant, since the for loop already does that for you. You can remove this line.
  • You are using print(x[::-2]) to print the reversed value of x, but this will skip every other digit, since the negative step of -2 means you are slicing the string from the end to the beginning with a step of 2. You should use print(x[::-1]) instead, which will reverse the whole string. A possible corrected version of your code is:
python
n = str(input())
x = ""
for z in range(len(n)):
   if int(n)%int(n[z])==0:
      x = x   n[z]
print(x[::-1])

This code will print the reversed order of the digits of n that divide it without any remainder, as expected. For example, if the input is 122, the output will be 221.

The code works by iterating over each digit of the input number n, which is converted to a string for convenience. For each digit, it checks if it divides n without any remainder, using the modulo operator %. If it does, it appends the digit to the end of the string x, using the operator. If it does not, it ignores the digit. After the loop, it prints the reversed value of x, using the slicing notation [::-1], which means start from the end and go backwards until the beginning.

CodePudding user response:

You could use a comprehension to filter digits based on whether the number is divisible by them or not, and then join all the digits to make the output. Process the input in reverse order and then you don't need to reverse the output:

# num = input() - no need to use `str` as that's what input returns anyway
num = '172336'
x = ''.join(digit for digit in num[::-1] if int(num) % int(digit) == 0)
# '21' - 172336 is not divisible by 7, 3 or 6
num = '122'
x = ''.join(digit for digit in num[::-1] if int(num) % int(digit) == 0)
# '221'
  • Related