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
7 ответов
+ 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));
+ 1
In
printf("%d", ceil (c));
%d is not for print a float variable.
+ 1
Emerson Prado done
+ 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?
+ 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 💫
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.
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.