Question

Convert from Python to C++ how would this code be written in C++? def calculate(price,quantity): total=price*quantity...

Convert from Python to C++

how would this code be written in C++?

def calculate(price,quantity):
total=price*quantity
if(quantity<10):
discount=0
elif(quantity>=10 and quantity<=20):
discount=total*10/100
elif(quantity>20):
discount=total*20/100
#returning price after discount
return total-discount
  
quantity=float(input("Enter quantity: "))
price=float(input("Enter price: "))

print("Price after discount is ${0:.2f}".format(calculate(price,quantity)))

Homework Answers

Answer #1
#include <iostream>

using namespace std;

double calculate(double price, int quantity){
    double total=price*quantity, discount;
    if(quantity<10)
        discount=0;
    else if(quantity>=10 and quantity<=20)
        discount=total*10/100;
    else if(quantity>20)
        discount=total*20/100;
    //returning price after discount
    return total-discount;
}

int main()
{
    int quantity;
    double price;
    
    cout<<"Enter quantity: ";
    cin>>quantity;
    
    cout<<"Enter price: ";
    cin>>price;
    
    cout<<"Price after discount is "<<calculate(price, quantity)<<endl;
    return 0;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
convert this code to accept int value instead of float values using python. Make sure to...
convert this code to accept int value instead of float values using python. Make sure to follow the same code. do not change the steps and make sure to point to what code you replaced. make sure to have 2 files Method:----------------------- #define a python user difined method def get_float_val (prompt): is_num = False str_val = input (prompt) #prming read for our while #while is_num == False: (ignore this but it works) old school while not is_num: try: value =...
Python Blackjack Game Here are some special cases to consider. If the Dealer goes over 21,...
Python Blackjack Game Here are some special cases to consider. If the Dealer goes over 21, all players who are still standing win. But the players that are not standing have already lost. If the Dealer does not go over 21 but stands on say 19 points then all players having points greater than 19 win. All players having points less than 19 lose. All players having points equal to 19 tie. The program should stop asking to hit if...
Please answer this using def (functions) in python 3 with a single print statement under #main....
Please answer this using def (functions) in python 3 with a single print statement under #main. Is this possible to do in one print as output? User specifies the number of books ordered, and if the order is online, or not. You can collect the second input by asking the user to enter 1 for online, and 0 for offline. You can assume that the user will enter 1 or 0 as directed. Unit price for a book is 15$....
So, i have this code in python that i'm running. The input file is named input2.txt...
So, i have this code in python that i'm running. The input file is named input2.txt and looks like 1.8 4.5 1.1 2.1 9.8 7.6 11.32 3.2 0.5 6.5 The output2.txt is what i'm trying to achieve but when the code runs is comes up blank The output doc is created and the code doesn't error out. it should look like this Sample Program Output 70 - 510, [semester] [year] NAME: [put your name here] PROGRAMMING ASSIGN MENT #2 Enter...
Program will allow anywhere between 1 and 6 players (inclusive). Here is what your output will...
Program will allow anywhere between 1 and 6 players (inclusive). Here is what your output will look like: Enter number of players: 2 Player 1: 7S 5D - 12 points Player 2: 4H JC - 14 points Dealer: 10D Player 1, do you want to hit? [y / n]: y Player 1: 7S 5D 8H - 20 points Player 1, do you want to hit? [y / n]: n Player 2, do you want to hit? [y / n]: y...
write a code in python Write the following functions below based on their comments. Note Pass...
write a code in python Write the following functions below based on their comments. Note Pass is a key word you can use to have a function the does not do anything. You are only allowed to use what was discussed in the lectures, labs and assignments, and there is no need to import any libraries. #!/usr/bin/python3 #(1 Mark) This function will take in a string of digits and check to see if all the digits in the string are...
Implement the following functions in the given code: def random_suit_number(): def get_card_suit_string_from_number(n): def random_value_number(): def get_card_value_string_from_number(n):...
Implement the following functions in the given code: def random_suit_number(): def get_card_suit_string_from_number(n): def random_value_number(): def get_card_value_string_from_number(n): def is_leap_year(year): def get_letter_grade_version_1(x): def get_letter_grade_version_2(x): def get_letter_grade_version_3(x): Pay careful attention to the three different versions of the number-grade-to-letter-grade functions. Their behaviors are subtly different. Use the function descriptions provided in the code to replace the pass keyword with the necessary code. Remember: Parameters contain values that are passed in by the caller. You will need to make use of the parameters that each...
NOTE:- PLEASE USE THE INSTRUCTION ACCORDING TO "CHAPTER 3" OF "STARTING OUT WITH PYTHON 4TH EDITION"...
NOTE:- PLEASE USE THE INSTRUCTION ACCORDING TO "CHAPTER 3" OF "STARTING OUT WITH PYTHON 4TH EDITION" PROGRAM 3_1:- Write a program that determines the cost of online order of T-shirts. T-shirts are priced at $12.99 each, but discounts are applied for quantities as follows: - 12 or more shirts earn a 30% discount and free shipping - 6-11 shirts earn a 20% discount - and 3-5 shirts earn a 10% discount - 1 or 2 shirts, no discounts Prompt the...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will implement Queue Abstract Data Type with the following functions/methods.  Any build-in/pre-defined Queue function/library (e.g., java.util.Queue in Java) is NOT allowed to use in your code. push(Element):  insert the input Element (e.g., String or Integer in Java) to the end of the queue. pop(): remove the head element of the queue and print the head element on screen. count():  return the total number of elements in the queue...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT