0

This program not working what is the problem

https://code.sololearn.com/ctlyIG3nKbGF/?ref=app

13th Aug 2021, 5:27 AM
Jafar
Jafar - avatar
11 Answers
+ 2
Problem A: Let me explain why your code doesn't work. You are taking input like that: scanf("%d%d",&j,&k); scanf ("%c",&sig); here you have written scanf("%d%d",&j,&k); here it works fine because most scanf %-conversions skip leading whitespace (all except for %c, %[ and %n).So spaces before %-conversions are irrelevant. But there are exceptions as I already mentioned. When you use scanf("%c",&sig); it takes white space as the first character. So when you take input like this in your code: 4 5 a or in a separate line instead of the gap, it takes 4 and 5 as the value of j and k and the "gap" between 5 and 'a' as the value of sig instead of 'a' which makes your code not working. So what's the solution? Well, there are some possible ways of doing this. 1.Either use gap at the last of the int input or give a gap before taking the char input. Example1: scanf("%d%d ",&j,&k); scanf ("%c",&sig); Example2: scanf("%d%d",&j,&k); scanf (" %c",&sig); 2. The way mentioned by @AJ sir .You can take all the input in one line .The gap between two %d is not necessary but you must give gap before %c. Like that: scanf("%d%d %c",&j,&k, &sig); here's a nice topic about this: https://www.codesdope.com/discussion/why-are-you-using-a-space-before-c-in-scanf-c-ch/ Problem B: In your code when you use division, your code doesn't work properly. For example, if the sample output is: 5 6 d the output should be 0.8333.... but your output is 0.000 .So your code doesn't work because dividing two integers doesn't get a float value. When the '/' operator sees two integers, it has to divide and hence returns an integer in the result that gets implicitly converted to a float by adding a decimal point. In short, you are converting an int to float which only adds some extra decimal points and nothing else. You can use the float keyword before the s inside div function, and it will work fine. I think your code should be like this: https://code.sololearn.com/cILXJrmT2TwO
13th Aug 2021, 6:57 AM
The future is now thanks to science
The future is now thanks to science - avatar
+ 2
Jafar Working. Just take input like : 4 5 d One more thing no need to assign result in a variable then return that variable. You can directly return result. https://code.sololearn.com/cGbHz297rsYT/?ref=app
13th Aug 2021, 11:10 AM
A͢J
A͢J - avatar
+ 2
AJ 😍 You solve so many problems that you created codes for them 😅. Oppa
13th Aug 2021, 12:12 PM
mycrochip
mycrochip - avatar
+ 2
I didn't understand anything when AJ Sir first said it, now I understand everything correctly
13th Aug 2021, 5:46 PM
Jafar
Jafar - avatar
+ 1
In sololearn you have to give all the input at once.Like that: sample input: 4 5 a sample output: 9 or sample input: 4 5 a sample output: 9
13th Aug 2021, 6:24 AM
The future is now thanks to science
The future is now thanks to science - avatar
+ 1
All Thanks
13th Aug 2021, 5:24 PM
Jafar
Jafar - avatar
+ 1
Jafar One more thing when you divide lower integer value with higher integer value then result always will be 0 so to get in decimal do typecast of lower integer value with float like given in corrected code.
13th Aug 2021, 8:01 PM
A͢J
A͢J - avatar
+ 1
Well you missed many blocks. First in if and else you didnt close any blocks! And in one of your methods you made this mistake again.
14th Aug 2021, 1:03 PM
Code Land
Code Land - avatar
0
Jafar take input in a single scanf like this: scanf("%d %d %c",&j,&k, &sig); remember there should be space if you take in single scanf
13th Aug 2021, 5:37 AM
A͢J
A͢J - avatar
0
No out put
13th Aug 2021, 6:13 AM
Jafar
Jafar - avatar
13th Aug 2021, 8:03 PM
Jafar
Jafar - avatar