Complete C# Tutorial

Loop Practice Questions C#: Fun & Easy Loop Exercises

Hey there, coder! 👋 You’ve learned about loops, but can you actually use them? It’s time to put your skills to the test with these fun and simple Loop practice questions C#! 🚀

Each problem has a hints, so you only need to focus on writing the loop. Plus, I’ve included the expected output so you can check if you got it right. No solutions here—because I believe in you! 💪

So, grab your keyboard, take a deep breath, and let’s dive into some awesome C# loop practice questions! 💡

🔢 1. For Loop

Expected Output:

				
					for (/* your logic here */)
{
    if (i % 2 == 0) // Check if the number is even
    {
        // Print even number
    }
}
				
			
				
					2  
4  
6  
8  
10  
12  
14  
16  
18  
20  
				
			

Expected Output:

				
					for (/* your logic here */)
{
    //print numbers
}
				
			
				
					3  
6  
9  
12  
15  
18  
21  
24  
27  
30  
				
			

🔄 2. Foreach Loop

				
					string[] fruits = { "Apple", "Banana", "Cherry", "Date" };  
foreach (/* your loop here */)  
{  
    // Prints fruits
}  
				
			

Expected Output:

				
					Apple  
Banana  
Cherry  
Date  
				
			
				
					string[] cities = { "New York", "London", "Tokyo", "Sydney" };
foreach (/* your loop here */)
{
    // Print city name
}
				
			

Expected Output:

				
					New York  
London  
Tokyo  
Sydney  
				
			

🔁 3. While Loop

				
					int number = 5;  
while (/* condition */)  
{  
    Console.WriteLine(number);  
    number--;  
}  
				
			

Expected Output:

				
					5  
4  
3  
2  
1  
				
			
				
					int num = 1;
while (num <= 5)
{
    // Print num and increase it
}
				
			

Expected Output:

				
					1  
2  
3  
4  
5  
				
			

🔃 4. Do While Loop

				
					int count = 0;  
do  
{  
    Console.WriteLine("Coding is fun!");  
    count++;  
} while (/* condition */);  
				
			

Expected Output:

				
					Coding is fun!  
Coding is fun!  
Coding is fun!  
				
			
				
					string input;
do
{
    Console.WriteLine("Type 'yes' to continue:");
    input = Console.ReadLine();
} while (input != "yes");
				
			

Expected Output:

				
					Type 'yes' to continue: no  
Type 'yes' to continue: maybe  
Type 'yes' to continue: yes  
				
			

⚡ 5. Parallel.For

				
					Parallel.For(/* start */, /* end */, i =>  
{  
    Console.WriteLine(i);  
});  
				
			

Expected Output:

				
					1  
3  
2  
5  
4  
				
			
				
					Parallel.For(1, 6, i =>
{
    Console.WriteLine($"Welcome User {i}"); // Print welcome message
});
				
			

Expected Output:

				
					Welcome User 1  
Welcome User 3  
Welcome User 2  
Welcome User 5  
Welcome User 4  
				
			

🚀 6. Parallel.ForEach

				
					List<string> languages = new List<string> { "C#", "Java", "Python" };
Parallel.ForEach(languages, lang =>
{
    Console.WriteLine(lang);
});
				
			

Expected Output:

				
					Java  
C#  
Python  
				
			
				
					string[] fruits = { "Apple", "Banana", "Orange", "Grapes", "Mango" };

Parallel.For(0, fruits.Length, i =>
{
    Console.WriteLine($"Fruit: {fruits[i]}"); // Print fruit name
});
				
			

Expected Output:

				
					Fruit: Apple  
Fruit: Banana  
Fruit: Orange  
Fruit: Grapes  
Fruit: Mango  
				
			

🌀 7. Goto Loop

				
					int count = 1;
start:
Console.WriteLine(count);
count++;
if (count <= 3) goto start;
				
			

Expected Output:

				
					1  
2  
3  
				
			
				
					start:
Console.Write("Enter a positive number: ");
int number = Convert.ToInt32(Console.ReadLine());

if (number <= 0)
{
    Console.WriteLine("Oops! That's not a positive number. Try again.");
    goto start; // Go back to the input step
}

Console.WriteLine($"Great! You entered: {number}");
				
			

Expected Output:

				
					Enter a positive number: -2  
Oops! That's not a positive number. Try again.  
Enter a positive number: 0  
Oops! That's not a positive number. Try again.  
Enter a positive number: 5  
Great! You entered: 5  
				
			

🔄 8. Recursion

				
					void PrintNumbers(int n)
{
    if (n > 4) return;
    Console.WriteLine(n);
    PrintNumbers(n + 1);
}
PrintNumbers(1);
				
			

Expected Output:

				
					1  
2  
3  
4  
				
			
				
					int SumNumbers(int n)
{
    if (n == 1)
        return 1; // Base case

    return n + SumNumbers(n - 1); // Recursive call
}
				
			

Expected Output:

				
					Enter a positive number: 5  
Sum of numbers from 1 to 5 is: 15  
				
			

🏆 Conclusion

Boom! 💥 You just nailed some loop practice questions C#! How do you feel? Hopefully, a bit more confident with loops now! 😊 Loops are super useful, and the more you practice, the better you’ll get.

Did you get all the outputs right? If not, don’t worry—learning to debug is part of the journey! 🚀 Keep practicing, stay curious, and most importantly, have fun with coding! 💪

🚀 Next What?

Alright, champ! 🏆 You’ve conquered loop practice questions C#, but what’s next? It’s time to level up with Classes and Methods in C#! 🎯

In the next chapter, you’ll learn how to organize code better, create powerful objects, and make your programs super efficient. It’s going to be awesome! 😎

So, ready for the next challenge? Let’s keep coding and dive into Classes and Methods! 🚀

Leave a Comment

Share this Doc

Programming exercises

Or copy link