Providion extra 10% discount for the sub total 1000 or more and calculate the total

I cant get the discount and total right, I need the output to be like

Example 1
Enter quantity: 160
Sub Total = 544.00
Discount = 0
Total = 544.00


Example 2
Enter quantity: 380
Sub Total = 1292.00
Discount = 129.00
Total = 1162

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
void main()
{
	// Veriable declaration
	int qty, discount;
	float price, pricelow, total;

	// Input
	printf("Enter Quantity: ");
	scanf("%d", &qty);

	// Process
	pricelow = qty * 3.40;
	price = qty * 4.00;
	discount = qty % 10;

	if (qty > 100)
	{
		printf("Sub Total = %.2lf\n", pricelow);
		printf("Discount = %f\n", discount);
		printf("Total = %.2lf", total);
	}
	else
	{
		printf("Sub Total = %.2lf\n", price);
		printf("Discount = %f\n", discount);
		printf("Total = %.2lf", total);
	}

	// Output
	printf("\n");
	system("pause");
}
Last edited on
discount = qty % 10;
Well that looks wrong. Surely it should be
discount = price - pricelow;
Last edited on
% is NOT percentage. It is remainder from integer division. % 10 can only be 0-9 in value for any given number....

you want
discount = .1 * subtotal; //10% of the subtotal. Did you want this as an integer truncate as in your example or the actual value?

you can also say
discount = subtotal / 10; //integer division ?? did you confuse / and % ?

Last edited on
from what I can see, float total is not given any value.
lol sorry I did confuse / and %
I want an actual value.

Example 1
Enter quantity: 160
Sub Total = 544.00
Discount = 0
Total = 544.00


Example 2
Enter quantity: 380
Sub Total = 1292.00
Discount = 129.00
Total = 1162
I believe the term for price before taxes, etc. is "net". You want a discount if the net price is >= 1000, so your conditional at line 19 is also wrong.

C++ example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <iomanip>

using namespace std;

int main() 
{
    float val = 3.40f;
    float discount_threshold = 1000.0f;    
    int qty = 380;
    float tax = 1.05f;

    float net = qty * val;
    float discount = net>=discount_threshold ? net*0.10 : 0.0f;

    cout << fixed << setprecision(2);
    cout << "Quantity:  " << qty << " @ $" << val << endl << 
            "Net:      $" << net << endl << 
            "Discount: $" << discount << endl << 
            "Pre-tax:  $" << (net-discount) << endl <<
            "Final:    $" << (net-discount)*tax << endl << endl;

    return 0;
}


Quantity:  380 @ $3.40
Net:      $1292.00
Discount: $129.20
Pre-tax:  $1162.80
Final:    $1220.94


Edit: fixed qty/net typo in the discount
Last edited on
I am trying to understand your logic, but why in example 1 is discount = 0?

In example 2 discount is equal to 10%.

In your code you have, if qty>100 but in your examples both are greater than 100.
One way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int qty;
    float discount, price, total;

    printf("Enter Quantity: ");
    scanf("%d", &qty);

    price = qty * 3.40;
    if (price > 1000)
        discount = (int)price / 10;
    else
        discount = 0;

    total = (int)price - discount;

    printf("Sub Total = %.2lf\n", price);
    printf("Discount = %.2f\n", discount);
    printf("Total = %.2f", total);
    system("pause");
}


BTW In example 2 the supposed total is wrong.
1292 - 129 = 1163 not 1162
Topic archived. No new replies allowed.