Have the user input three values: Number of large (5lb) bags of candy available
ID: 3789767 • Letter: H
Question
Have the user input three values: Number of large (5lb) bags of candy available Number of small (1lb) bags of candy available Size of the order (number of pounds) Determine if the order can be fulfilled. If it is possible to fill the order, print the number of large bags and small bags required to fill the order. If it is not possible, print a message indicating that the order cannot be fulfilled. 1 large bag, 4 small bags, order size 9 lbs - can fulfill the order using all of the available bags. 2 large bags, 1 small bag, order size 9lbs - cannot fulfill the order 2 large bags, 5 small bags, order size 7lbs - can fulfill the order with 1 large bag and 2 small ones.Explanation / Answer
THERE COULD BE MULTIPLE SOLUTION TO THIS PROBLEM BUT THIS CODE RETURNS ONE WITH HIGHEST NUMBER OF LARGE BAGS
CODE (C language)
#include<stdio.h>
int main()
{
printf("Enter total number of large bags : ");
int largeBags;
scanf("%d",&largeBags);
printf("Enter total number of small bags : ");
int smallBags;
scanf("%d",&smallBags);
printf("Enter order size: ");
int orderSize;
scanf("%d",&orderSize);
int noOfLargeBags=orderSize/5;
int noOfSmallBags=orderSize-(5*noOfLargeBags);
if(noOfLargeBags>largeBags)
{
noOfSmallBags=noOfSmallBags+((noOfLargeBags-largeBags)*5);
if(noOfSmallBags>smallBags){printf(" Order cannot be placed");return 0;}
}
printf("Order can be made by using %d large bags and %d small bags",noOfLargeBags,noOfSmallBags);
return 0;
}
SAMPLE OUTPUT
Enter total number of large bags : 2
Enter total number of small bags : 5
Enter order size: 7
Order can be made by using 1 large bags and 2 small bags
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.