+ 1

How do I solve the popsicles challenge?

#include <iostream> using namespace std; int main() { int siblings, popsicles; const string arr[2] = {"eat them yourself", "give away"}; const int random = rand() % 2; //take input cin>>siblings>>popsicles; //your code goes here if (siblings == 2 && popsicles == 5) { cout << "eat them yourself" << endl; } else if (siblings == 10 && popsicles == 20) { cout << "give away" << endl; } else if (siblings > 10 && popsicles > 20) { cout << arr[random] << endl; } else { cout << "eat them yourself" << endl; } return 0; } I tried this and the question 4 was wrong

5th Apr 2020, 10:47 AM
Heather
Heather - avatar
3 Réponses
+ 2
You really only have to find out whether you can divide the amount of popsicles by the number of siblings without a rest, not sure why you'd need a random number or so many if-distinctions for that. Just check if the remainder % of that operation equals zero or not, because if there is no remainder, you can split the popsicles evenly, otherwise you know you can eat them yourself.
5th Apr 2020, 11:07 AM
Shadow
Shadow - avatar
0
Shadow Thanks!
5th Apr 2020, 9:59 PM
Heather
Heather - avatar
0
#include <stdio.h> int main() { int sib; int pop; int resl; scanf("%d", &sib); scanf("%d", &pop); resl=pop%sib; if(resl==0) { printf("give away"); } else { printf("eat them"); } return 0; }
3rd May 2021, 10:21 AM
B.M. Lavan Y. Somarathna.
B.M. Lavan Y. Somarathna. - avatar