New course! Every coder should learn Generative AI!
Try a free lesson+ 1
What is the problem here in this code? Why is it not printing the expected output
Question is in the code https://code.sololearn.com/cy00KgupK83u/?ref=app
5 Answers
+ 6
Your code has two problems:
First the range must start at one and not zero. Remember that division by zero is undefined.
Second you have written in the conditional structure r% num == 0 that is incorrect because the values of r will always be less than or equal to num, therefore it will not be divisors. The correct answer would be num% r == 0, try it and tell me.
My apologies for my english
+ 5
Suppose I enter 12 as input. According to your code, it will first check if
0 % 12 == 0
then
1 % 12 == 0
2 % 12 == 0
and so on
See the problem? Try thinking about what is the value of 2 % 12 and 3 % 12.
+ 4
It is because, "r" is the variable of iteration, therefore it should be your divisor, and not the "num" .
Another thing is that your starting point of iteration should be 1 and not 0, to avoid ZeroDivisionError.
+ 3
You all were saying it right ♨️♨️ 《 Nicko12 》 XXX Luis Andrés Valido Fajardo it was showing zero modulo error. This mistake will not be repeated. Next time i will do the mathematical part 1st and then code it. Thank you all.
https://code.sololearn.com/cLd7lYZOAC99/?ref=app
+ 2
num = int(input()) # enter the number you want to check the divisor of
for r in range (0,num+1):
if r%num== 0:
print(r)
Your code will print only 2 values
If u will enter 26 and r will be start for 0 to num+ 1
In if condition 0%num==0 this will be true so r will print then r will be 1 . If1%26==0 False this will be true that time when r will be reach at 26
26%26==0 then will print again so total 2 values will be print in your case
change your logic.