In: Computer Science
Using loop statements, write a C++ program which takes the number of items that a shopper wants to buy, and then takes the price of each item, and at the end tells the shopper how much she must pay.
This is a sample of the output:
How many items do you have in your basket? 3
Enter the price in dollar? 10.25
Enter the price in dollar? 20.75
Enter the price in dollar? 67.5
You must pay 98.5 $ today.
The C++ program using while loop for the following problem is given below.with full code and output.
CODE
#include <iostream>
using namespace std;
//driver code
int main() {
int n; //initialize variable n to take no. of input
cout << "How many items do you have in your basket? ";
cin>>n;
//delcare array of size n and initialize amount with zero
//In loop we directly take input and add it to the amount
double amount=0;
double arr[n];
for(int i=0;i<n;i++){
cout<<"Enter the price in dollar?";
cin>>arr[i];
amount+=arr[i]; //adding item price to final amount
}
//here we display the resultant amount value
cout<<"You must pay "<<amount<<" $ today.";
}
SNAPS
FOR ANY QUERY MESSAGE ME IN COMMENT.
int n; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <iostream> using namespace std; //driver code int main() { 1/initialize variable n to take no. of input cout << "How many items do you have in your basket? "; cin>>n; //delcare array of size n and initialize amount with zero //In loop we directly take input and add it to the amount double amount=0; double arr[n]; for(int i=0;i<n;i++){ cout<<"Enter the price in dollar?"; cin>>arr[i]; amount+=arr[i]; //adding item price to final amount } //here we display the resultant amount value cout<<"You must pay "<<amount<<" $ today."; }
} ./main How many items do you have in your 3 Enter the price in dollar?10.25 Enter the price in dollar?20.75 Enter the price in dollar?67.5