For my day job, I do embedded C programming for PIC24 compilers and some Windows C programming in something called LabWindows. Lately, I’ve been touching some C# stuff, so I decided to revisit last night’s 3X+1 program by converting it to C#.
You can compile and run it online here: https://www.onlinegdb.com/online_csharp_compiler
// 3X+1
using System;
public class Program
{
public static void Main()
{
while (true)
{
Int32 x = 0;
Console.WriteLine();
Console.Write("STARTING NUMBER? ");
x = Int32.Parse(Console.ReadLine());
while (true)
{
Console.Write(x);
Console.Write(" ");
if (x == 1) break;
if ((x & 1) == 1) // Odd
{
x = x * 3 + 1;
}
else // Even
{
x = x / 2;
}
}
}
}
}
In c exist isOdd or isEven?
I am not sure. There are “isnum()” for strings. I think a macro:
#define isOdd(n) (x & 1)
unsigned int x;
X = 42;
If isOdd(x)
{
/* odd /
}
else
{
/ even */
}