+ 4

What's going on....

How the below code yields 2011 as it's output since I got 1011 only.... #include <stdio.h> int recur(int num) { if((num/2) !=0) return recur(num/2)*10+num%2; } int main() { int i=11; printf("%d",recur(i)); return 0; } https://code.sololearn.com/ciOTa10WPp2g/?ref=app

12th Jun 2022, 5:50 AM
Ur-Wellwisher
Ur-Wellwisher - avatar
7 Réponses
+ 3
The recursive function is missing a base case. There's nothing returned when the `if` onditional evaluates to false. Add this under the `if` block return num;
12th Jun 2022, 7:35 AM
Ipang
+ 2
Ipang Thank you for your concern
12th Jun 2022, 7:52 AM
Ur-Wellwisher
Ur-Wellwisher - avatar
+ 1
Recursion is quite a complex subject. I still find it difficult to explain. Someone will eventually.
12th Jun 2022, 7:49 AM
Ipang
0
Ipang then how it will be 2011
12th Jun 2022, 7:39 AM
Ur-Wellwisher
Ur-Wellwisher - avatar
0
Why do you expect 2011? The question is not “how to change the code to produce specific output”. The question is “what I’m trying to achieve and how to change the code to cover it”.
13th Jun 2022, 6:36 AM
JayV
0
JayV okay then can you explain how the output will be 2011
13th Jun 2022, 6:39 AM
Ur-Wellwisher
Ur-Wellwisher - avatar
- 1
Write the Python code of a program that reads an integer, and prints the integer it is a multiple of either 2 or 5 but not both. If the number is a multiple of 2 and 5 both, then print "Multiple of 2 and 5 both". For all other numbers, the program prints "Not a multiple we want". For example, 2, 4, 5, 6, 8, 12, 14, 15, 16, 18, 22... ie. this includes multiples of 2 only and multiples of 5 only, NOT multiples of 2 and 5 both or other numbers hint: we may use the modulus (6) operator for checking the divisibility hin(2): we can consider the number to be an integer Sample Input 1: 6 Sample Output 1: 6 Sample Input 2: 15 Sample Output 2: 15 Sample Input 3: 10 Sample Output 3: Multiple of 2 and 5 both Sample Input 4: 17 Sample Output 4: Not a multiple we want Can any one hlp me with this???
13th Jun 2022, 2:36 PM
Tarif Tanweer