Total expenses

I am getting wrong answer with this code:

int n;
scanf("%d", &n);
while(n–)
{
int q,p;
float price;
scanf("%d %d", &q, &p);
price = q*p;
if(q > 1000)
price = 0.9 * q * p;
else
price = q * p;
printf("%f\n", price);
}

but right answer with this code:

int n;
scanf("%d", &n);
while(n–)
{
int q,p;
float price;
scanf("%d %d", &q, &p);
price = q*p;
if(q > 1000)
printf("%f\n", price * 0.9);
else
printf("%f\n", price);
}

Can someone please explain what’s going on inside?

Post the question link and please format your code :slight_smile:

1 Like

Thank you for responding.
Here’s the question:

While purchasing certain items, a discount of 10% is offered if the quantity purchased is more than 1000.
If the quantity and price per item are input, write a program to calculate the total expenses.

Input

The first line contains an integer T, total number of test cases. Then follow T lines, each line contains integers quantity and price.

Output

For each test case, output the total expenses while purchasing items, in a new line.

How do I format my code?

The only difference I can find in both the codes you pasted is that you are multiplying q * p twice in code1- before the if else conditional and then after it with 0.9.

Fine, I just got confused by that. Maybe you want to add curly braces after that else part in code1.

Codechef Markdown This should help in Markdown.
And you can insert code like
```

[Code here]

```