-1

What values integer gets? When i wrote this code the output was 0 and not 0.333333

int a=18;
Int b=6;
int c=b/a; 
Console.WriteLine(c);

thank you (:

Flydog57
  • 6,851
  • 2
  • 17
  • 18
  • You have to use double and not int – Stefino76 Apr 30 '21 at 21:35
  • In your example, `a`, `b` and `c` are integers. If divide two integers, you get an integer, so `int c=b/a` makes sense. But, there are rules for dividing integers - rules needed to make the result an integer. If you _integer divide_ 6 by 18, the result must be an integer (and one third isn't). The rule is (roughly) do the division and then truncate the result towards zero. So `6/18` is zero, and `24/18` is one. You might want to look up the _modulus operator_ (`%`) and see how it gives you the _Remainder_ from integer division – Flydog57 Apr 30 '21 at 21:40

3 Answers3

0

int can not show Decimal. you must use float or double

alirezasejdei
  • 180
  • 2
  • 14
0

The answer is in your question, integer can only hold integer value (...,-2,-1,0,1,2,...) so 6/18 would be 0. You could expect otherwise if 2 of these conditions are met:

1.c (the variable result is assigned) can hold floating type (float, double, decimal)

  1. either an implicit or an explicit cast is done.

To describe (2) a bit more:

if either of a or b is of floating point type, you implicitly casting the result to a floating point value.

double a = 6;
int b = 18;
double c =a / b;

or you explicitly say that the int values (at leat one of them) should be considered as floating point:

int a = 6;
int b= 18;
double c = (double)a/(double)b; 
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
0
double c = (double) a/b;

All you need to know about types

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/types

AlexM
  • 1
  • 1