//when I am trying to make a division in a float variable for some reason it
//prints 000
#include <stdio.h>
#include <math.h>
int main(){
float price , discount , total ;
scanf ("%f",&price);
if (price > 99 && price < 201)
discount = 10/100;
// i can also set <<discount>> variable 0.10
else if (price < 301)
// but i can t understand why this way prints 00
discount = 10/100;
else if (price <401)
discount = 20/100;
else
discount = 30/100;
total = price - discount;
printf ("tha exete ekptwsei %.2f",discount);
}
Asked
Active
Viewed 40 times
0

Jonathan Leffler
- 730,956
- 141
- 904
- 1,278

vaggelis
- 1
- 3
-
1Add a ".0" or an "f" after every number. Else they will be truncated and then converted to floating point. – Micrified Feb 10 '19 at 16:05
-
ohh thank you very much, i am new in c language i prefer c++ and python – vaggelis Feb 10 '19 at 16:07
-
my code had the same view but stackoverflow for some reason wanted to have 4 /t space to let me post my code, tvm btw – vaggelis Feb 10 '19 at 16:10
-
1With respect to integer division, C behaves like C++ and unlike Python. – Jonathan Leffler Feb 10 '19 at 16:56
-
When formatting code for presentation on SO, make sure you use blanks, not tabs, to indent (or detab the code before copy'n'pasting). Copy the code into the question (or answer) box (ignore the preview at this stage), making sure there are no tabs in the code. Select the code, use the **`{}`** button above the edit box to indent it as code. Now check the preview. – Jonathan Leffler Feb 10 '19 at 17:00
-
have you ever divided a number in C++ or python 2? The behavior in those languages are exactly the same as this case, which is similar to python 3 `//` – phuclv Feb 11 '19 at 01:49
-
yes, i had forget the fact when i have to deivide 2 numbers those numbers must be writen for example 2.0 in c++ this process was automated – vaggelis Feb 11 '19 at 15:22
1 Answers
4
if (price > 99 && price < 301)
discount = 10.0/100.0;
else if (price <401)
discount = 20.0/100.0;
else
discount = 30.0/100.0;
Update Only .0 in divider is suffice like 10/100.0

Vhndaree
- 594
- 1
- 6
- 20