How to use Console.ReadLine() inside a while loop in C#

1 Answer

0 votes
using System;
 
public class Program
{
	public static void Main()
	{
		int n;
		
		Console.WriteLine("Enter numbers, 42 to exit:");
		
		while ((n = int.Parse(Console.ReadLine())) != 42) {
			Console.WriteLine(n);
		}
	}
}



/*
run:

Enter numbers, 42 to exit:
23
23
7
7
9
9
0
0
3290
3290
42

*/

 



answered Apr 25, 2025 by avibootz

Related questions

1 answer 126 views
3 answers 103 views
103 views asked Apr 28, 2025 by avibootz
2 answers 88 views
88 views asked Apr 24, 2025 by avibootz
1 answer 108 views
1 answer 166 views
1 answer 163 views
...