Advertisement
Niloy007

Problem 2

Apr 11th, 2021
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main() {
  4.     int amount, price, totalAmount = 0, totalPrice = 0;
  5.     while (true) {
  6.         printf("Enter amount (in kg): ");
  7.         scanf("%d", &amount);
  8.         if (amount == 0) {
  9.             break;
  10.         } else if (amount < 0) {
  11.             printf("Invalid input, enter a positive number\n");
  12.             continue;
  13.         } else {
  14.             totalAmount += amount;
  15.         }
  16.         printf("Enter Price: ");
  17.         scanf("%d", &price);
  18.         if (price < 0) {
  19.             printf("Invalid input, enter a positive number\n");
  20.             totalAmount -= amount;
  21.             continue;
  22.         } else {
  23.             totalPrice += price;
  24.         }
  25.     }
  26.     double avgPrice = ((totalPrice * 1.0) / totalAmount);
  27.     printf("Total amount (in kg): %d, Total Price: %d, Average price per kg: %.2lf\n", totalAmount, totalPrice, avgPrice);
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement