Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: (untagged)
x = 32154
if (X % 2)

Error Cannot implicitly convert type 'int' to 'bool'
Posted
Comments
ridoy 5-Apr-13 13:09pm    
It will be if ((x % 2) == 0)
Amir Mahfoozi 6-Apr-13 7:49am    
No it should be "if((X % 2) != 0)"

Yes, in c#, an integer valued expression will not be implicitly converted to bool with 0 == false, 0 != true (like c/c++ does).
Typically, you perform some comparison on the value:
C#
int x = 32154;
if ((x % 2) == 0)

If you are just checking for even vs. odd, just check the low order bit (assuming you're on a twos-compliment architecture):
C#
if ((x & 1) == 0)    // x is even
if ((x & 1) == 1)    // x is odd
 
Share this answer
 
Comments
ridoy 5-Apr-13 13:08pm    
correct..+5
Thomas Daniels 6-Apr-13 6:25am    
+5!
i have been experamenting with

if ((x % 2) == 0)

and the ting i wanted is

if ((x % 2) == 1)

but tnx anyway i appreciate it
 
Share this answer
 
hmm... yes but it is buging like crazy look at this !i am using C#!

C#
Console.WriteLine("This is a program that calculates the gcd of some typed number");
            int StartingNum = Convert.ToInt16(Console.ReadLine());
            int NumForCalculate = StartingNum;
            string gcd = ("");
            int gathering = 1;
            operation:
            int x = 2;
            operation2:
            if ((NumForCalculate / x) == :see below:)
            {
                if (gcd == "")
                    gcd = ("" + x);
                else
                    gcd = (gcd + "," + x);
                NumForCalculate = (NumForCalculate / x);
                gathering = gathering * x;
                if (NumForCalculate <= 1)
                    goto end;
                else
                    goto operation;
            }
            else
            {
                x++;
                goto operation2;
            }
            end:
            Console.WriteLine("gcd of integers {0} is {1}, and gathering this set of numbers is {2}", StartingNum, gcd, gathering);
            Console.ReadKey();


if i set 20

if ((NumForCalculate / x) == 1)

then gcd of integers 20 is 11, and gathering this set of numbers is 11

if ((NumForCalculate / x) == 0)

then gcd of integers 20 is 21, and gathering this set of numbers is 21
 
Share this answer
 
Comments
SoMad 6-Apr-13 6:33am    
I am not sure if you are asking a question or posting an answer here. In any case, there are way too many goto's in that code for my liking.

Soren Madsen
Matt T Heffron 8-Apr-13 13:23pm    
I'm not sure what you are doing. GCD is not defined for a single integer. It requires two or more integers.
Since this doesn't seem directly related to the original question, I recommend posting this as a new question with its own descriptive subject.
And, like Soren said, this can be done without any goto.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900