1. Engineering
  2. Computer Science
  3. programming language c need help to convert the code inside...

Question: programming language c need help to convert the code inside...

Question details

(Programming language: C) Need help to convert the code inside if, else if, and else statement into a method

#include

int main()
{
int quantity;
double discount;
double discountAmount;
double price;
double cost;
double total;
  
puts("Welcome to \"Temple\" store \n");
  
printf( "%s", "\tEnter item price: ");
scanf("%lf", &price);
  
if(price>0){
printf( "%s", "\tEnter quantity: ");
scanf("%d", &quantity);
if(quantity>0){
if(quantity>=1 && quantity<=49){
cost = price * quantity;
discount = 0;
discountAmount = cost * (discount/100);
total = cost - discountAmount;
printf("\n\tThe item price is: $%.1f \n", price);
printf("\tThe order is: %d item(s) \n", quantity);
printf("\tThe cost is: $%.1f \n", cost);
printf("\tThe discount is: %.1f% \n", discount);
printf("\tThe discount amount is: $%.1f \n", discountAmount);
printf("\tThe total is: $%.1f \n", total);

}
else if(quantity>=50 && quantity<=99){
cost = price * quantity;
discount = 10;
discountAmount = cost * (discount/100);
total = cost - discountAmount;
printf("\n\tThe item price is: $%.1f \n", price);
printf("\tThe order is: %d item(s) \n", quantity);
printf("\tThe cost is: $%.1f \n", cost);
printf("\tThe discount is: %.1f% \n", discount);
printf("\tThe discount amount is: $%.1f \n", discountAmount);
printf("\tThe total is: $%.1f \n", total);
}
else if(quantity>=100 && quantity<=149){
cost = price * quantity;
discount = 15;
discountAmount = cost * (discount/100);
total = cost - discountAmount;
printf("\n\tThe item price is: $%.1f \n", price);
printf("\tThe order is: %d item(s) \n", quantity);
printf("\tThe cost is: $%.1f \n", cost);
printf("\tThe discount is: %.1f% \n", discount);
printf("\tThe discount amount is: $%.1f \n", discountAmount);
printf("\tThe total is: $%.1f \n", total);
}
else{
cost = price * quantity;
discount = 25;
discountAmount = cost * (discount/100);
total = cost - discountAmount;
printf("\n\tThe item price is: $%.1f \n", price);
printf("\tThe order is: %d item(s) \n", quantity);
printf("\tThe cost is: $%.1f \n", cost);
printf("\tThe discount is: %.1f% \n", discount);
printf("\tThe discount amount is: $%.1f \n", discountAmount);
printf("\tThe total is: $%.1f \n", total);
}
}else{
puts("\n\tThis is not a valid quantity order.");
puts("\tPlease run the program again");
}
}else{
puts("\n\tThis is not a valid item price.");
puts("\tPlease run the program again");
}
puts("\nThank You for using \"Temple\" store");
exit(0);

}

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution