I'm taking my first class about C language, so I'm a noob and I need some help trying to solve a simple task. The main idea for this program is to ask the user to enter the price of one carton of juice as well as the number of cartons being purchased to determine the final cost of buying orange juice under the "Buy One Get One" (BOGO) offer.
The requirement for this programs is that needs to use if statement to be solved, and that's what I'm most confused with.
Here is the program setup that is given to me:
Program Setup
Your program should ask the user to enter the price of one carton of juice as well as the number of cartons being purchased. Note that since juice is an ordinary grocery item, no sales tax is charged on it.Then, determine the final cost of buying orange juice under the BOGO offer. While it is possible to make a solution that does not use If Statements, the purpose of the assignment is to practice with conditions. Make sure you take this opportunity to get the practice you need to master the programming concepts.
You'll need to account for two different cases: buying an even number of OJ cartons and buying an odd number of OJ cartons. How can you determine if a number is even or odd? A number that is evenly divisible by 2 is even. An odd number has a remainder of 1 when divided by 2. Which arithmetic operator gives you the remainder?
This is what I have written so far and I would like some guidance please. I hope I'm making sense.
#include <stdio.h>
#include <stdlib.h>
int main() {
double OJ_price;
int OJ_containers;
printf("What is the cost of one container of OJ in dollars?\n");
scanf("%lf", &OJ_price);
printf("How many containers are you buying?\n");
scanf("%d", &OJ_containers);
if(OJ_containers % 2 == 0)
printf("%d ", OJ_containers);
else
printf("The total cost is $%1f ", (OJ_containers*OJ_price)/2);
return 0;
Aucun commentaire:
Enregistrer un commentaire