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
Question 1:
Write a program to print all even numbers between 1 and 20.
Hint:
Use a for
loop starting from 2, increase by 2 each time. Here’s your starting point:
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
Question 2:
Write a program that prints the first 10 multiples of 3.
Hint:
Use a for
loop that runs 10 times and multiplies the loop index by 3.
Expected Output:
for (/* your logic here */)
{
//print numbers
}
3
6
9
12
15
18
21
24
27
30
🔄 2. Foreach Loop
Question 1:
Print each fruit from the array: {"Apple", "Banana", "Cherry", "Date"}
using a foreach
loop.
Hint:
Loop through the array and print each item:
string[] fruits = { "Apple", "Banana", "Cherry", "Date" };
foreach (/* your loop here */)
{
// Prints fruits
}
Expected Output:
Apple
Banana
Cherry
Date
Question 2:
You have a list of cities: {"New York", "London", "Tokyo", "Sydney"}
. Print each city using a foreach
loop.
Hint:
Store the cities in a string array and loop through them.
string[] cities = { "New York", "London", "Tokyo", "Sydney" };
foreach (/* your loop here */)
{
// Print city name
}
Expected Output:
New York
London
Tokyo
Sydney
🔁 3. While Loop
Question 1:
Print numbers from 5 down to 1 using a while
loop.
Hint:
Start at 5 and decrease until you reach 1:
int number = 5;
while (/* condition */)
{
Console.WriteLine(number);
number--;
}
Expected Output:
5
4
3
2
1
Question 2:
Print numbers from 1 to 5 using a while
loop.
Hint:
Initialize a counter variable before the loop and increase it inside.
int num = 1;
while (num <= 5)
{
// Print num and increase it
}
Expected Output:
1
2
3
4
5
🔃 4. Do While Loop
Question 1:
Print “Coding is fun!” exactly 3 times.
Hint:
Use a do-while
that ensures it prints at least once:
int count = 0;
do
{
Console.WriteLine("Coding is fun!");
count++;
} while (/* condition */);
Expected Output:
Coding is fun!
Coding is fun!
Coding is fun!
Question 2:
Keep asking the user to enter “yes” until they actually type “yes”.
Hint:
Use do-while
to ensure the question is asked at least once.
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
Question 1:
Print numbers from 1 to 5 using Parallel.For
. Notice how the order might change!
Hint:
Here’s the structure—fill in the range:
Parallel.For(/* start */, /* end */, i =>
{
Console.WriteLine(i);
});
Expected Output:
1
3
2
5
4
Question 2:
Write a program using Parallel.For
to print a welcome message for 5 users with user numbers (e.g., “Welcome User 1”, “Welcome User 2”, etc.).
Hint:
- Use
Parallel.For(start, end, action)
wherestart
is 1 andend
is 6. - Inside the loop, use string interpolation to print the welcome message.
- Remember, the order might vary due to parallel execution!
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
Question 1:
Print a list of programming languages ("C#"
, "Java"
, "Python"
) using Parallel.ForEach
.
Hint:
Use Parallel.ForEach
to process each item in the list.
List<string> languages = new List<string> { "C#", "Java", "Python" };
Parallel.ForEach(languages, lang =>
{
Console.WriteLine(lang);
});
Expected Output:
Java
C#
Python
Question 2:
Write a program using Parallel.For
to display the names of five fruits.
Hint:
- Create a
List<string>
or an array containing 5 fruit names. - Use
Parallel.For(0, list.Count, action)
to iterate through the list using indices. - Inside the loop, print the fruit name using the current index.
- Don’t forget that the output order might differ because of parallel execution!
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
Question 1:
Use goto
to print numbers from 1 to 3.
Hint:
Use a label and goto
to loop until the count reaches 3.
int count = 1;
start:
Console.WriteLine(count);
count++;
if (count <= 3) goto start;
Expected Output:
1
2
3
Question 2:
Write a program using the goto
statement to repeatedly ask the user to enter a number until they enter a positive number.
Hint:
- Use a label (e.g.,
start:
) before the input section. - Use
goto start;
to jump back if the number is not positive. - Use
int.Parse()
orConvert.ToInt32()
to convert user input to an integer. - Add a message to guide the user when they enter a non-positive number.
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
Question 1:
Write a recursive function to print numbers from 1 to 4.
Hint:
Call the function inside itself with an incremented value.
void PrintNumbers(int n)
{
if (n > 4) return;
Console.WriteLine(n);
PrintNumbers(n + 1);
}
PrintNumbers(1);
Expected Output:
1
2
3
4
Question 2:
Write a program using recursion to calculate the sum of all natural numbers from 1 to a given number n
.
Hint:
- Create a recursive method (e.g.,
SumNumbers
) that takes an integer parametern
. - Base case: If
n
is 1, return 1. - Recursive case: Return
n + SumNumbers(n - 1)
. - In the
Main
method, ask the user to enter a positive number and call the recursive method.
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! 🚀