== Code founded online in Chegg, however the output is not the one that was told. It supposed to give a ounce_left of 4, but is giving an 8.
Someone knows where is the bug?
==
In this problem, our task is to find the no_of _cans needed and the no_of_ounce left over.
The formula to calculate no_of_cans needed is: ceil(amount/12).
Formula to calculate no_of_ounce needed is: 12*ceil(amount/12)-amount.
Algorithm
- Take input amount from user.
- Divide amount/12 and store ceil of quotient in variable cans_needed.
- Ounce_left will be calculated as 12*cans_needed-amount.
- Print cans_needed.
- Print ounce_left.
- Stop.
Now let us try to implement the above algorithm using C++.
//header files used
#include<bits/stdc++.h>
using namespace std;
int main()
{
int amount;
//taking input from user
cout<<"Enter Amount in ounces:";
cin>>amount;
//finding no of cans needed
//if it is evenly divided by 12 then ceil(quotient)=quotient
//else ceil(quotient)=quotient+1
int cans_needed=ceil((double)amount/12);
//finding ounce_left
int ounce_left=amount%12;
cout<<"Cans Needed :"<<cans_needed<<endl; //printing cans_needed
cout<<"Ounce left: "<<ounce_left<<endl; //printing ounce_left
}