Write a program to calculate the total cost from shopping for n commodities. The input consists of an integer n, followed by n pairs or doubles. In each pair, the first number is the quantity bought of the commodity, and the second the rate per unit for the commodity. The amount to be paid for each commodity is the product of the quantity and the rate; the program must print the total over all commodities. For example if the input is 3 2 30 3 10 5 60, the expected answer is 2*30 + 3*10 + 5*60 = 390.usingc++
Answers
#include<iostream>
using namespace std;
int main()
{
int a,i,c,d,s=0;
cin>>a;
i=1;
while(i<=a)
{
cin>>c>>d;
s=s+(c*d);
i++;
}
cout<<s;
}
Language using : C++ Programming
Program :
#include <iostream>
using namespace std;
int main()
{
int noofitems, quantity;
float rate, total=0;
cout<<"Enter no.of items : ";
cin>>noofitems;
for(int i=0; i<noofitems; i++)
{
cout<<"Enter quantity of item : ";
cin>>quantity;
cout<<"Enter the rate per piece : ";
cin>>rate;
total = total + quantity*rate;
}
cout<<"Total amount for the items in the cart is : "<<total;
return 0;
}
Input :
Enter no.of items : 3
Enter quantity of item : 2
Enter the rate per piece : 30
Enter quantity of item : 3
Enter the rate per piece : 10
Enter quantity of item : 5
Enter the rate per piece : 60
Output :
Total amount for the items in the cart is : 390
Explanation :
- Initialize all the necessary integer and float variables.
- Take an input from the user on how many items he is going to buy.
- Based on the items number, run a loop and ask the quantity of each item and its price per unit.
- On every iteration, add the sum up of each item total quantity's cost to the total and store it in the total variable.
total = total + quantity*cost_per_unit
- Once all the items corresponding quantities and price per units computation is done and stored in total, the loop terminates.
- Then, print the total.
Learn more :
1) Explain C language and C++language ?
https://brainly.in/question/4602037
2) Tell About C++ in Programming Language . What is C++ in programming language?
https://brainly.in/question/640127