While loops runs through the if statement once and stops.
I am new to c#, so excuse me if I am overlooking something seemingly obvious. I am currently writing a program that visualizes the Collatz conjecture through console entries. The program begins by prompting the user to enter a natural number. The program is supposed to run that number through the formulae of the conjecture until it eventually reaches a value of 1. However, when I type in the number in console, the program runs it through one formula and crashes. It seems that is has a problem with the Double.Parse line. I already tried using the convert method and tried defining "num" as a decimal instead of a double.
{
Console.WriteLine("Enter a natural number:");
Double num = Convert.ToDouble(Console.ReadLine());
while (num != 1)
{
{
if (num % 2 == 0)
{
Console.WriteLine(num / 2);
num = Double.Parse(Console.ReadLine());
}
else
{
Console.WriteLine(num * 3 + 1);
num = Double.Parse(Console.ReadLine());
}
}
Console.ReadLine();
}
}
}
}
num = Double.Parse(Console.ReadLine());while loop?Console.ReadLine()gets input from the user. Your program isn't crashing, it's waiting for you to type something and then pressEnter. Most likely, you want to perform an operation onnumrather than get more input from the user, likenum = num / 2;ornum = num * 3 + 1;Console.ReadLine()was recording a blank entry after the first run through the while loop. I removed theConsole.ReadLine()and replaced it with manic_coder's suggestion.