Is this wrong? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

13th Dec 2020, 9:57 AM
Mayur Gowda
Mayur Gowda - avatar
9 Answers
+ 1
Mayur Gowda cast 2 with double to get value in decimal. double y; y=((double) 2/x)*100;
13th Dec 2020, 10:00 AM
A͢J
A͢J - avatar
+ 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
13th Dec 2020, 11:07 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 1
🇮🇳Omkar🕉, Thanks. Much appreciated for the help.
13th Dec 2020, 11:09 AM
Mayur Gowda
Mayur Gowda - avatar
+ 1
Mayur Gowda Yes you declared variable with float but when devide int value with int result will be always integer.
13th Dec 2020, 12:26 PM
A͢J
A͢J - avatar
+ 1
Mayur Gowda First use Math.ceil then Math.round Math.round(Math.ceil(y))
13th Dec 2020, 12:42 PM
A͢J
A͢J - avatar
+ 1
I Am Groot ! Ok, Thanks a lot. It's been really helpfull.
13th Dec 2020, 12:44 PM
Mayur Gowda
Mayur Gowda - avatar
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.
13th Dec 2020, 10:17 AM
Mayur Gowda
Mayur Gowda - avatar
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
13th Dec 2020, 12:30 PM
Mayur Gowda
Mayur Gowda - avatar
- 1
السلام عليكم
13th Dec 2020, 10:06 PM
عمر محمود حامد
عمر محمود حامد - avatar