+ 2

# Can someone tell me that my logic is correct or not

#include <iostream> using namespace std; int main() { int fruit; //take input cin>>fruit; //your code goes here int apples=fruit/2; int pie=(apples-1)/3; cout<<pie; return 0; } You have a bowl on your counter with an even number of pieces of fruit in it. Half of them are bananas, and the other half are apples. You need 3 apples to make a pie. Task Your task is to evaluate the total number of pies that you can make with the apples that are in your bowl given to total amount of fruit in the bowl. Input Format An integer that represents the total amount of fruit in the bowl. Output Format An integer representing the total number of whole apple pies that you can make. Sample Input 26 Sample Output 4

15th Jun 2024, 10:38 AM
Haram Abbas Lar
4 Antworten
+ 6
What if the input is 18? Then apples=9 and pie should be 3, but that calculation would output 2. It is unnecessary to change the number of apples. Doing so makes a wrong mathematical model. Even though apples might not be evenly divisible by 3 it works anyway. Integer division simply discards the fractional result. So in integer math, 13/3 = 4, and 14/3 = 4 as well.
15th Jun 2024, 3:34 PM
Brian
+ 4
Haram Abbas Lar the code subtracts 1 from apples before dividing. What is the reason for that?
15th Jun 2024, 1:18 PM
Brian
0
We have to make 1 pie from 3 apples so 13 is an odd number how can we divide 13 by 3 It is given in task that output should be 4
15th Jun 2024, 2:16 PM
Haram Abbas Lar
0
Haram Abbas Lar , Brian already provided detailed explanation with examples to answer this question. No need to subtract apples by 1 just to be divisible by 3.
17th Jun 2024, 6:09 PM
Pat