-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
  • 1,030
  • 11
  • 26
Doniyor Niyozov
  • 1,151
  • 1
  • 9
  • 16

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ɴᴀᴍ
  • 5,850
  • 5
  • 29
  • 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
  • 6,829
  • 9
  • 41
  • 69