-1

Hi everyone today I was testing some stuff and find an issue.

See:

int a = 13;    
int b = 10;
double c = a/b;

The result is 1. or should it be like this ?

PriyankaChauhan
  • 953
  • 11
  • 26
Doniyor Niazov
  • 1,270
  • 1
  • 10
  • 19

2 Answers2

1

Dividing an int by int performs integer division. If you want to perform decimal division, cast one of the operands to double.

double c = (double)a / b;

// ---OR---

double c = a / (double)b;

Also, declaring c as double does not guarantee decimal division. It can be an implicitly typed variable too. As long as one or both of the operands of the / operator is of type double (or float, decimal, etc.) you will get a decimal result.

Fᴀʀʜᴀɴ Aɴᴀᴍ
  • 6,131
  • 5
  • 31
  • 52
0

you have to specify that you want a double precision, for example

int a = 13;
int b = 10;
double c = a/(double)b;
Bidou
  • 7,378
  • 9
  • 47
  • 70