tags:

views:

257

answers:

3

in my program, i think my count variable is not holding the value.. what do i do so that it can hold? here`s my code.

static void Main(string[] args)
    {
        double a;
        double count = 0;
        Console.WriteLine("Enter the Numbers : ");
        for (double i = 1; i <= 10; i++)
        {
            a = Convert.ToDouble(Console.ReadLine());
            if (a % 2 != 0 || a % 3 != 0 || a % 5 != 0)
            {
                count = count++;
            }
            //else
            //{

           // }
            Console.ReadLine();
        }
        Console.WriteLine("The Numbers That Are divisible by 2,3,5 are : " + count);
        Console.ReadLine();


    }
+8  A: 

Your mistake is the line count = count++;. This does not increase count by one. You need to use just count++;.

The expression count++ will increment the value of count by one and then return as the expression's value the original value of count. In this case the increment of count++ happens before the assignment, so count is first incremented by one, but then you assign the value of count++, that is, the original value, to count again, so it does not increase at all.

jk
i also tried count++ .. but same response here with count = count++
Abid
The behavior is actually undefined (there is no sequence point in that line), but yes, that is the issue. Either use `count++;` or `count = count + 1;`. The former is generally preferred.
David Kanarek
this program also stops onto a = Convert.ToInt32(Console.ReadLine()); saying an exception that an input string is not in a correct format..
Abid
@David: count=count++ i; invokes Undefined Behavior in C and in C++ but in C# the behavior is well defined. Correct me if I am wrong.
Prasoon Saurav
Prasoon is right; this is well defined in C#. If this subject interests you, here's an article I wrote about it: http://blogs.msdn.com/ericlippert/archive/2009/08/10/precedence-vs-order-redux.aspx
Eric Lippert
Serves me right for not knowing C# well enough. Thanks for the info.
David Kanarek
+6  A: 

Your program lists numbers that are not divisible by any of those numbers. If you want to count numbers which aren't divisible by all of them then you need to use if (a % 2 != 0 && a % 3 != 0 && a % 5 != 0) instead. I would also suggest using integers instead of doubles if possible.

Finally, your print statement says numbers that are divisible by 2,3,5, but count is the number of numbers which are not divisible by those numbers.

Edit: Are you entering 10 numbers each time you test? I'm not sure what kind of result you will get if you give a blank input.

David Kanarek
Abid
Anton
+2  A: 

Not to add to what jk and David Kanarek said in their answers or what the others the comments on those answers, As pointed out by jk use count+1 instead of count ++ , also a few notes:

1) Your using console.Readline() twice in the loop, so the user will enter 20 inputs but only 10 will be read..

2) Just alittle extra thought on Anton's comment, in your if clause , if you use || your trying to catch any of the conditions being true, in other words:

 // a=2

  if (a % 2 != 0 || a % 3 != 0 || a % 5 != 0) // False || True || True = True 
        {
            count = count + 1 ;// count will increase
        }

on the other hand, using && :

 // a=2

  if (a % 2 != 0 &&  a % 3 != 0 &&  a % 5 != 0) // False && True && True = false
        {
            count = count + 1 ; //count will not increase
        }

A useful Link explaining operators

Madi D.