+ 3

What's wrong with this code??

You have a box of popsicles and you want to give them all away to a group of brothers and sisters. If you have enough left in the box to give them each an even amount you should go for it! If not, they will fight over them, and you should eat them yourself later. Task Given the number of siblings that you are giving popsicles to, determine if you can give them each an even amount or if you shouldn't mention the popsicles at all. Input Format Two integer values, the first one represents the number of siblings, and the second one represents the number of popsicles that you have left in the box. Output Format A string that says 'give away' if you are giving them away, or 'eat them yourself' if you will be eating them yourself. Sample Input 3 9 Sample Output give away It is showing wrong output as I input 10 20 It shows eat them yourself instead of give away! https://code.sololearn.com/c4UGYaBXhsfW/?ref=app

20th Dec 2019, 1:23 PM
Bhushan
Bhushan - avatar
6 ответов
+ 3
Well, if you handtrace it, you can see that q = p = 20 / 10 = 2 r = p / q = 1 ( p - q ) = 0 0 < 1 Therefore the if-statement is executed. Your solution seems rather complex for the problem at hand to me. For example, what is 'r' for? Essentially, you are being asked to test if there is a rest when dividing the amount of popsicles by the amount of siblings, which can be done in a single operation.
20th Dec 2019, 1:37 PM
Shadow
Shadow - avatar
+ 2
#include <iostream> using namespace std; int main() { int siblings, popsicles; //take input cin>>siblings>>popsicles; //your code goes here if (popsicles%siblings==0 ) { cout <<"give away"; } else{ cout <<"eat them yourself"; } return 0; } https://www.sololearn.com/coach/3?ref=app
20th Feb 2020, 3:05 PM
IZAHAR ANSARI
IZAHAR ANSARI - avatar
+ 2
https://www.sololearn.com/coach/3?ref=app Go for easyway..
18th Apr 2020, 5:48 PM
Ramu Kaka
0
This might help you to do it in a shorter way
23rd Dec 2019, 7:19 AM
Ritwik
0
You just need equally shared all the popsicles among all siblings. So: popsicles % siblings == 0 will do the trick.
12th Jun 2020, 3:50 PM
Shimax
Shimax - avatar