Unexpected output | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Unexpected output

Here is my code ---> #include <stdio.h> #include<math.h> int main() { int a , b; float c; scanf("%d %d ", &a, &b); c= (a/b); printf("%f", ceil (c)); return 0; } I'm not able to understand why my code taking i/p 3 times and giving unexpected output Code link --> https://code.sololearn.com/cx8544m7IVSK/?ref=app

9th Oct 2022, 2:33 PM
Laukesh singh
Laukesh singh - avatar
7 Answers
+ 3
Laukesh singh The C library function double ceil(double x) returns the smallest integer value greater than or equal to x. Declaration: double ceil(double x) If you want an integer so explicit conversion will be needed as follows: printf("%d", (int)ceil(c));
9th Oct 2022, 7:36 PM
JaScript
JaScript - avatar
+ 1
In printf("%d", ceil (c)); %d is not for print a float variable.
9th Oct 2022, 3:00 PM
JaScript
JaScript - avatar
10th Oct 2022, 4:38 AM
Laukesh singh
Laukesh singh - avatar
+ 1
Laukesh singh I ran the code, but the problems didn't occur. I gave as input: 25 4 And the output was: 6.0000... Exactly like it should. Can you give some examples of input/output pairs which show the problem?
10th Oct 2022, 9:17 AM
Emerson Prado
Emerson Prado - avatar
+ 1
Emerson Prado I've found my mistake, see 25/ 4 is 6.25 and it's ceil value is 7 so I'm expecting output as 7 but getting 6 . What I did wrong is that I was dividing int to int and expecting float ( ofcourse it'll give int only) I've made some changes in code and now it's working fine. Thank you for your help 💫
10th Oct 2022, 9:33 AM
Laukesh singh
Laukesh singh - avatar
0
JaScript I used integer format specifier (%d) because I'm expecting integer after ceil (c). And other things is that I'm scanning for two values a and b but code scanning 3 values.
9th Oct 2022, 3:09 PM
Laukesh singh
Laukesh singh - avatar
0
Laukesh singh Put your code in Code Playground, save as public and add a link to it (with "+" button) in the question description. This way, we can test it.
10th Oct 2022, 12:46 AM
Emerson Prado
Emerson Prado - avatar