+ 1
Is this wrong?
I'm attempting to get a percentage value. I know we can divide a variable with a constant, but can we divide a constant by a variable? I've run this program many times, and every time the answer is zero. Help. https://code.sololearn.com/cY72ko6Q6Ucp/?ref=app
9 Antworten
+ 1
Mayur Gowda cast 2 with double to get value in decimal.
double y;
y=((double) 2/x)*100;
+ 1
Mayur Gowda ,
int/int is always int in Java.
Answered a similar question yesterday which was for C but still valid for Java. Read :
https://www.sololearn.com/discuss/2623725/?ref=app
+ 1
🇮🇳Omkar🕉, Thanks. Much appreciated for the help.
+ 1
Mayur Gowda
Yes you declared variable with float but when devide int value with int result will be always integer.
+ 1
Mayur Gowda
First use Math.ceil then Math.round
Math.round(Math.ceil(y))
+ 1
I Am Groot ! Ok, Thanks a lot. It's been really helpfull.
0
I Am Groot ! Thanks a lot, but can you tell me why we cast 2 with double? We're already specifying that the y value should be a float or a double which means y value is in decimal after calculating.
0
I Am Groot ! Thanks again, much appreciated. If you don't mind I have another question. I've been trying the community Challenge called the Halloween bowl. I've written the code and the first two cases were correct but the test cases that are locked are showing its wrong.Can you tell me if something's wrong?
https://code.sololearn.com/c2q5fVYc1jyI/?ref=app
- 1
السلام عليكم