Question

In: Computer Science

Using loop statements, write a C++ program which takes the number of items that a shopper...

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.

Solutions

Expert Solution

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


Related Solutions

Write a program using c++. Write a program that uses a loop to keep asking the...
Write a program using c++. Write a program that uses a loop to keep asking the user for a sentence, and for each sentence tells the user if it is a palindrome or not. The program should keep looping until the user types in END. After that, the program should display a count of how many sentences were typed in and how many palindromes were found. It should then quit. Your program must have (and use) at least four VALUE...
2.c++ if and loop statement Write a program that will count the number of even number...
2.c++ if and loop statement Write a program that will count the number of even number and odd numbers between two inputted numbers. Display the numbers and compute the sum and average of all the even numbers and the sum and average all the odd numbers. Sample outputs: Enter starting number:3 Enter starting number:4 Enter ending number:10 Enter ending number:10 odd numbers Even number 3 4 5 6 7 8 9 10 number of even numbers=4 number of even numbers=4...
Write a program to reverse each integer number on array (size 3) using while loop. C++...
Write a program to reverse each integer number on array (size 3) using while loop. C++ Input: 3678 2390 1783 Output: 8763 0932 3871
Write a program in C or C++ that takes a number series of size n (n...
Write a program in C or C++ that takes a number series of size n (n integers) as input from the user, push all the numbers to the stack, and reverse the stack using recursion. Please note that this is not simply popping and printing the numbers, but the program should manipulate the stack to have the numbers stored in reverse order. In addition to the provided header file, the students can use the following function to print the content...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
Loop Introduction Assignment Please write a program in c# Using the conditions below, write one program...
Loop Introduction Assignment Please write a program in c# Using the conditions below, write one program that calculates a person’s BMI. Your main() function will call functions 1, 2, and 3. Your program will contain three functions: Function #1: Will ask the user for their weight in pounds and their height in inches.   Your function will convert the weight and height into Body Mass Index (BMI). The formula for converting weight into BMI is as follows: BMI = Weight *...
Create a c program that takes 1 parameter, a number using that number, dynamically allocate a...
Create a c program that takes 1 parameter, a number using that number, dynamically allocate a memory so you store that number of integers write integers in order starting from 1 until you fill all that memory print the address and values of the first and the last integer stored in the memory
2. Write a c++ program that takes from the user the ​number of courses​ and constructs...
2. Write a c++ program that takes from the user the ​number of courses​ and constructs 3 ​dynamic 1D arrays​ with size courses+1. Each array represents a student. Each cell in the array represents a student’s mark in a course. In the last cell of each 1D array you should calculate the average mark of that student. Then output the average mark of all students in each course. Delete any allocated memory. Example Number of courses : 4 50 60...
Write a program in C that takes as input an 8-bit binary number and prints the...
Write a program in C that takes as input an 8-bit binary number and prints the next 10 binary numbers. Define a binary number as int binNum[8]; Use binNum[0] to store the most significant (i.e., leftmost) bit and binNum[7] to store the least significant bit. Ask the user to input the first binary number with each bit separated by at least one space.
Write a program in C that takes as input a four-digit hexadecimal number and prints the...
Write a program in C that takes as input a four-digit hexadecimal number and prints the next 10 hexadecimal numbers. Define a hexadecimal number as int hexNum[4] Allow upper- or lowercase letters for input and use uppercase letters for the hexadecimal output. For example, 3C6f should be valid input and should produce output 3C6F, 3C70, 3C71, . . . .
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT