C# >> Something with the numbers get scrambled up











up vote
-1
down vote

favorite












So this is what happens: I enter a number and what happens is that it writes out only 1 number: 324, like the variable c is getting this value for no reason..



class Program
{
static void Main(string args)
{
Console.WriteLine("enter a number with change");
double num = double.Parse(Console.ReadLine());
num = (int)num;
int c = 0;
Console.WriteLine(num);
while (num != 0)
{
num /= 10;
c++;
}
Console.WriteLine(c);
}
}









share|improve this question









New contributor




Lidor Cohen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • What number are you entering? What output are you expecting?
    – Carcigenicate
    Nov 10 at 16:04












  • You mean, as input you enter 324 and c become 324?
    – Mehdi Dehghani
    Nov 10 at 16:05










  • Try and print the values of num within your loop. I think the calculation doesn't end when you think it should
    – Hans Kesting
    Nov 10 at 16:07






  • 1




    num is a variable of type double (a floating point number). How many times do you think the while loop has to divide num by 10 to become zero?
    – elgonzo
    Nov 10 at 16:08












  • What is this program supposed to do?
    – Mureinik
    Nov 10 at 16:08















up vote
-1
down vote

favorite












So this is what happens: I enter a number and what happens is that it writes out only 1 number: 324, like the variable c is getting this value for no reason..



class Program
{
static void Main(string args)
{
Console.WriteLine("enter a number with change");
double num = double.Parse(Console.ReadLine());
num = (int)num;
int c = 0;
Console.WriteLine(num);
while (num != 0)
{
num /= 10;
c++;
}
Console.WriteLine(c);
}
}









share|improve this question









New contributor




Lidor Cohen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • What number are you entering? What output are you expecting?
    – Carcigenicate
    Nov 10 at 16:04












  • You mean, as input you enter 324 and c become 324?
    – Mehdi Dehghani
    Nov 10 at 16:05










  • Try and print the values of num within your loop. I think the calculation doesn't end when you think it should
    – Hans Kesting
    Nov 10 at 16:07






  • 1




    num is a variable of type double (a floating point number). How many times do you think the while loop has to divide num by 10 to become zero?
    – elgonzo
    Nov 10 at 16:08












  • What is this program supposed to do?
    – Mureinik
    Nov 10 at 16:08













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











So this is what happens: I enter a number and what happens is that it writes out only 1 number: 324, like the variable c is getting this value for no reason..



class Program
{
static void Main(string args)
{
Console.WriteLine("enter a number with change");
double num = double.Parse(Console.ReadLine());
num = (int)num;
int c = 0;
Console.WriteLine(num);
while (num != 0)
{
num /= 10;
c++;
}
Console.WriteLine(c);
}
}









share|improve this question









New contributor




Lidor Cohen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











So this is what happens: I enter a number and what happens is that it writes out only 1 number: 324, like the variable c is getting this value for no reason..



class Program
{
static void Main(string args)
{
Console.WriteLine("enter a number with change");
double num = double.Parse(Console.ReadLine());
num = (int)num;
int c = 0;
Console.WriteLine(num);
while (num != 0)
{
num /= 10;
c++;
}
Console.WriteLine(c);
}
}






c#






share|improve this question









New contributor




Lidor Cohen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Lidor Cohen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 10 at 16:09









D Stanley

122k9110168




122k9110168






New contributor




Lidor Cohen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 10 at 16:02









Lidor Cohen

11




11




New contributor




Lidor Cohen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Lidor Cohen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Lidor Cohen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • What number are you entering? What output are you expecting?
    – Carcigenicate
    Nov 10 at 16:04












  • You mean, as input you enter 324 and c become 324?
    – Mehdi Dehghani
    Nov 10 at 16:05










  • Try and print the values of num within your loop. I think the calculation doesn't end when you think it should
    – Hans Kesting
    Nov 10 at 16:07






  • 1




    num is a variable of type double (a floating point number). How many times do you think the while loop has to divide num by 10 to become zero?
    – elgonzo
    Nov 10 at 16:08












  • What is this program supposed to do?
    – Mureinik
    Nov 10 at 16:08


















  • What number are you entering? What output are you expecting?
    – Carcigenicate
    Nov 10 at 16:04












  • You mean, as input you enter 324 and c become 324?
    – Mehdi Dehghani
    Nov 10 at 16:05










  • Try and print the values of num within your loop. I think the calculation doesn't end when you think it should
    – Hans Kesting
    Nov 10 at 16:07






  • 1




    num is a variable of type double (a floating point number). How many times do you think the while loop has to divide num by 10 to become zero?
    – elgonzo
    Nov 10 at 16:08












  • What is this program supposed to do?
    – Mureinik
    Nov 10 at 16:08
















What number are you entering? What output are you expecting?
– Carcigenicate
Nov 10 at 16:04






What number are you entering? What output are you expecting?
– Carcigenicate
Nov 10 at 16:04














You mean, as input you enter 324 and c become 324?
– Mehdi Dehghani
Nov 10 at 16:05




You mean, as input you enter 324 and c become 324?
– Mehdi Dehghani
Nov 10 at 16:05












Try and print the values of num within your loop. I think the calculation doesn't end when you think it should
– Hans Kesting
Nov 10 at 16:07




Try and print the values of num within your loop. I think the calculation doesn't end when you think it should
– Hans Kesting
Nov 10 at 16:07




1




1




num is a variable of type double (a floating point number). How many times do you think the while loop has to divide num by 10 to become zero?
– elgonzo
Nov 10 at 16:08






num is a variable of type double (a floating point number). How many times do you think the while loop has to divide num by 10 to become zero?
– elgonzo
Nov 10 at 16:08














What is this program supposed to do?
– Mureinik
Nov 10 at 16:08




What is this program supposed to do?
– Mureinik
Nov 10 at 16:08












2 Answers
2






active

oldest

votes

















up vote
9
down vote













This program should loop infinitely if double could represent every real number. Say you enter in 1. Then the loop will divide it by 10, leaving 0.1. Since 0.1 is not equal to 0, the loop will continue, resulting in 0.01, etc.



double, however, only can support a minimum value of about 5E-324, so if you get a number that small and try to divide it by 10, you'll get zero.



So your program loops about 324 times, and quits after it gets a small enough number.






share|improve this answer





















  • Nice analysis and well explained.
    – johey
    Nov 10 at 16:10


















up vote
0
down vote













Try printing out num inside the loop. You'll see it tries to divide by 10 (division like this can never end in 0) infinitely until it can no longer hold that small of a value and == 0 will return true.



Just because you set num to it's rounded value by using (int)num doesn't mean it'll behave like an int it'll still end up as a fractional when dividing, use an int if you want it to be:



Console.WriteLine("enter a number with change");
double num = double.Parse(Console.ReadLine());
int num2 = (int)num;
int c = 0;
Console.WriteLine(num2);
while (num2 != 0)
{
num2 /= 10;
c++;
}
Console.WriteLine(c);





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    Lidor Cohen is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240757%2fc-sharp-something-with-the-numbers-get-scrambled-up%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    9
    down vote













    This program should loop infinitely if double could represent every real number. Say you enter in 1. Then the loop will divide it by 10, leaving 0.1. Since 0.1 is not equal to 0, the loop will continue, resulting in 0.01, etc.



    double, however, only can support a minimum value of about 5E-324, so if you get a number that small and try to divide it by 10, you'll get zero.



    So your program loops about 324 times, and quits after it gets a small enough number.






    share|improve this answer





















    • Nice analysis and well explained.
      – johey
      Nov 10 at 16:10















    up vote
    9
    down vote













    This program should loop infinitely if double could represent every real number. Say you enter in 1. Then the loop will divide it by 10, leaving 0.1. Since 0.1 is not equal to 0, the loop will continue, resulting in 0.01, etc.



    double, however, only can support a minimum value of about 5E-324, so if you get a number that small and try to divide it by 10, you'll get zero.



    So your program loops about 324 times, and quits after it gets a small enough number.






    share|improve this answer





















    • Nice analysis and well explained.
      – johey
      Nov 10 at 16:10













    up vote
    9
    down vote










    up vote
    9
    down vote









    This program should loop infinitely if double could represent every real number. Say you enter in 1. Then the loop will divide it by 10, leaving 0.1. Since 0.1 is not equal to 0, the loop will continue, resulting in 0.01, etc.



    double, however, only can support a minimum value of about 5E-324, so if you get a number that small and try to divide it by 10, you'll get zero.



    So your program loops about 324 times, and quits after it gets a small enough number.






    share|improve this answer












    This program should loop infinitely if double could represent every real number. Say you enter in 1. Then the loop will divide it by 10, leaving 0.1. Since 0.1 is not equal to 0, the loop will continue, resulting in 0.01, etc.



    double, however, only can support a minimum value of about 5E-324, so if you get a number that small and try to divide it by 10, you'll get zero.



    So your program loops about 324 times, and quits after it gets a small enough number.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 10 at 16:08









    D Stanley

    122k9110168




    122k9110168












    • Nice analysis and well explained.
      – johey
      Nov 10 at 16:10


















    • Nice analysis and well explained.
      – johey
      Nov 10 at 16:10
















    Nice analysis and well explained.
    – johey
    Nov 10 at 16:10




    Nice analysis and well explained.
    – johey
    Nov 10 at 16:10












    up vote
    0
    down vote













    Try printing out num inside the loop. You'll see it tries to divide by 10 (division like this can never end in 0) infinitely until it can no longer hold that small of a value and == 0 will return true.



    Just because you set num to it's rounded value by using (int)num doesn't mean it'll behave like an int it'll still end up as a fractional when dividing, use an int if you want it to be:



    Console.WriteLine("enter a number with change");
    double num = double.Parse(Console.ReadLine());
    int num2 = (int)num;
    int c = 0;
    Console.WriteLine(num2);
    while (num2 != 0)
    {
    num2 /= 10;
    c++;
    }
    Console.WriteLine(c);





    share|improve this answer

























      up vote
      0
      down vote













      Try printing out num inside the loop. You'll see it tries to divide by 10 (division like this can never end in 0) infinitely until it can no longer hold that small of a value and == 0 will return true.



      Just because you set num to it's rounded value by using (int)num doesn't mean it'll behave like an int it'll still end up as a fractional when dividing, use an int if you want it to be:



      Console.WriteLine("enter a number with change");
      double num = double.Parse(Console.ReadLine());
      int num2 = (int)num;
      int c = 0;
      Console.WriteLine(num2);
      while (num2 != 0)
      {
      num2 /= 10;
      c++;
      }
      Console.WriteLine(c);





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Try printing out num inside the loop. You'll see it tries to divide by 10 (division like this can never end in 0) infinitely until it can no longer hold that small of a value and == 0 will return true.



        Just because you set num to it's rounded value by using (int)num doesn't mean it'll behave like an int it'll still end up as a fractional when dividing, use an int if you want it to be:



        Console.WriteLine("enter a number with change");
        double num = double.Parse(Console.ReadLine());
        int num2 = (int)num;
        int c = 0;
        Console.WriteLine(num2);
        while (num2 != 0)
        {
        num2 /= 10;
        c++;
        }
        Console.WriteLine(c);





        share|improve this answer












        Try printing out num inside the loop. You'll see it tries to divide by 10 (division like this can never end in 0) infinitely until it can no longer hold that small of a value and == 0 will return true.



        Just because you set num to it's rounded value by using (int)num doesn't mean it'll behave like an int it'll still end up as a fractional when dividing, use an int if you want it to be:



        Console.WriteLine("enter a number with change");
        double num = double.Parse(Console.ReadLine());
        int num2 = (int)num;
        int c = 0;
        Console.WriteLine(num2);
        while (num2 != 0)
        {
        num2 /= 10;
        c++;
        }
        Console.WriteLine(c);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 16:10









        Sombrero Chicken

        22.3k32772




        22.3k32772






















            Lidor Cohen is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            Lidor Cohen is a new contributor. Be nice, and check out our Code of Conduct.













            Lidor Cohen is a new contributor. Be nice, and check out our Code of Conduct.












            Lidor Cohen is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240757%2fc-sharp-something-with-the-numbers-get-scrambled-up%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python