-2

I do not know why am I getting output as 0. Please help me to figure out my mistake in correcting the calculation for the long data type in C#. I have almost all variables in long to make it easier, but even then, the code does not work as expected.

        long data_user;
        long GB;
        long MB;
        long kB;
        long bits;
        long bits_rem;
        double giga_C;
        double mega_c;
        double kilo_C;
        double byte_C;
        double sub_tot;

        Console.Write("\nEnter the number of bytes used: ");
        data_used = Console.Read();

        //Divide data into each units.
        GB = data_user / 1073741824; 
        giga_C = Convert.ToDouble(15.00 * GB);

        MB = bits_rem / 1045214;
        mega_C = (6.05 * MB);                  

        kB = bits_rem / 10002;
        kilo_C = (3.50 * kB);  

        bits = bits_rem / 0.1;
        bits_C = (0.01 * bits);                     

        sub_tot = (giga_C + mega_C + kilo_C + byte_C);
Kal
  • 9
  • 3
  • 4
    Duplicate of [Why division is always ZERO?](https://stackoverflow.com/questions/41278585/why-division-is-always-zero), [Division returns zero](https://stackoverflow.com/q/9288904/8967612), and [Wrong integer output on Console.Read()](https://stackoverflow.com/q/32050026/8967612). – 41686d6564 stands w. Palestine Oct 17 '20 at 21:01
  • 1
    See also https://stackoverflow.com/q/10851273/1070452 and https://stackoverflow.com/q/661028/1070452 – Ňɏssa Pøngjǣrdenlarp Oct 17 '20 at 21:02

1 Answers1

1

Console.Read reads the next character from the Console, so that you always get smaller number divided by big one (which is zero in integer arithmetic). For example, you write 1234, but Console.Read returns 49 because Unicode point for '1' is 49. Then you try to divide 49 by 1073741824 and obviously get 0.

You need to read your value as following Convert.ToInt64(Console.ReadLine())

E. Shcherbo
  • 1,128
  • 8
  • 15