C# Intermediate Tutorial: Functions, Conditionals, Loops & Strings
Part 1: Functions
Functions in C# help organize code into reusable blocks.
Use return for output and parameters to pass values.
Example:
int Add(int a, int b)
return a + b;
Practice Questions:
1. Write a function to calculate the factorial of a given number.
2. Create a function that checks if a number is a prime.
3. Write a function that returns the maximum of three numbers.
4. Create a function that reverses a given integer.
5. Write a function that takes a string and returns true if it's a palindrome.
Part 2: If-Else Statements
Use if, else if, and else for conditional decision making.
Example:
if (number > 0)
[Link]("Positive");
}
C# Intermediate Tutorial: Functions, Conditionals, Loops & Strings
else
[Link]("Not Positive");
Practice Questions:
6. Write a program that checks whether a number is even or odd using if-else.
7. Check whether a character is a vowel or consonant.
8. Write a program to determine if a person is eligible to vote (age >= 18).
9. Create a program that finds the grade based on the percentage (>=90 A, >=80 B, etc.).
10. Compare two strings and check if they are equal, case-insensitively.
Part 3: Switch Case
Use switch to simplify multiple if-else conditions.
Example:
switch (day)
case 1:
[Link]("Monday");
break;
default:
[Link]("Invalid Day");
break;
}
C# Intermediate Tutorial: Functions, Conditionals, Loops & Strings
Practice Questions:
11. Create a calculator using switch that performs +, -, *, /.
12. Write a program that prints the day of the week based on a number (1-7).
13. Use a switch statement to assign grades to students based on marks.
14. Write a program that takes a character and determines if it's an operator (+, -, *, /).
15. Create a switch that maps month numbers to month names.
Part 4: Loops (for, while, do-while)
Use loops to iterate over code blocks.
Example:
for (int i = 0; i < 5; i++)
[Link](i);
Practice Questions:
16. Print the multiplication table of a number using a for loop.
17. Write a program that prints the Fibonacci series up to N terms.
18. Count the number of digits in an integer using a while loop.
19. Reverse a string using a loop (without built-in functions).
20. Print all even numbers between 1 and 100 using a do-while loop.
Part 5: Break and Continue
C# Intermediate Tutorial: Functions, Conditionals, Loops & Strings
break is used to exit loops or switch cases early.
continue skips the current iteration and moves to the next one.
Examples:
for (int i = 0; i < 10; i++)
if (i == 5)
break;
[Link](i);
for (int i = 0; i < 10; i++)
if (i % 2 == 0)
continue;
[Link](i);
Practice Questions:
21. Write a program that prints numbers 1 to 10 but skips 5 using continue.
22. Create a loop that takes input until the user types "exit" using break.
23. Find the first number divisible by 7 and 11 in a range and stop using break.
24. Print all numbers from 1 to 50, but skip multiples of 3 using continue.
25. In a list of numbers, break the loop if a negative number is found.