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

four − three =

Share this Doc

Programming exercises

Or copy link