30 C# Basic Exercises for Beginners with Solutions

Master beginner C# skills with our comprehensive list of top 30 exercises. Dive into coding challenges that improve your understanding and proficiency in C#, setting a solid foundation for intermediate challenges. Start your journey to C# mastery today!

Learning Objectives:

These beginner C# exercises will help you grasp fundamental programming concepts such as variables, data types, loops, conditional statements, and functions. By practicing these challenges, you'll build a strong foundation in C# and develop problem-solving skills essential for more advanced topics.

Exercise Instructions:

  • Start with the first exercise and attempt to solve it before checking the hint or solution.
  • Ensure you understand the logic behind each solution, as this will help you in more complex problems.
  • Use these exercises to reinforce your learning and identify areas that may require further study.

1. Write a program to print "Hello, World!" to the console.

Required Input:

None.

Expected Output:

Hello, World!

Code In C#

using System; class HelloWorld { static void Main() { // Your code here to print "Hello, World!" } }

Run Code?

Click Run Button to view compiled output

2. Create a program that declares an integer variable, assigns it a value, and prints it.

Required Input:

Integer: 10

Expected Output:

The value is: 10

Code In C#

using System; class PrintInteger { static void Main() { // Declare and initialize an integer // Print the integer value } }

Run Code?

Click Run Button to view compiled output

3. Write a program to find the sum of two hardcoded numbers.

Required Input:

Numbers: 5 and 7

Expected Output:

Sum: 12

Code In C#

using System; class SumTwoNumbers { static void Main() { // Declare two numbers // Find their sum and print it } }

Run Code?

Click Run Button to view compiled output

4. Create a program to swap two variables without using a third variable.

Required Input:

Numbers: 4 and 7

Expected Output:

After Swap: a = 7, b = 4

Code In C#

using System; class SwapVariables { static void Main() { // Declare two variables and swap them } }

Run Code?

Click Run Button to view compiled output

5. Write a program to check if a hardcoded number is even or odd.

Required Input:

Number: 8

Expected Output:

8 is Even

Code In C#

using System; class EvenOrOdd { static void Main() { // Check if the number is even or odd } }

Run Code?

Click Run Button to view compiled output

6. Create a program to print numbers from 1 to 10 using a for loop.

Required Input:

None.

Expected Output:

1 2 3 4 5 6 7 8 9 10 

Code In C#

using System; class PrintNumbers { static void Main() { // Use a for loop to print numbers from 1 to 10 } }

Run Code?

Click Run Button to view compiled output

7. Write a program that takes a hardcoded year and checks if it is a leap year.

Required Input:

Year: 2024

Expected Output:

2024 is a Leap Year

Code In C#

using System; class LeapYearCheck { static void Main() { // Check if the hardcoded year is a leap year } }

Run Code?

Click Run Button to view compiled output

8. Create a program to calculate the factorial of a hardcoded number.

Required Input:

Number: 5

Expected Output:

Factorial: 120

Code In C#

using System; class Factorial { static void Main() { // Calculate the factorial of a number } }

Run Code?

Click Run Button to view compiled output

9. Write a program to find the largest of three hardcoded numbers.

Required Input:

Numbers: 10, 25, 15

Expected Output:

Largest Number: 25

Code In C#

using System; class LargestNumber { static void Main() { // Find the largest of three numbers } }

Run Code?

Click Run Button to view compiled output

10. Create a program to print the multiplication table of a hardcoded number.

Required Input:

Number: 5

Expected Output:

5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Code In C#

using System; class MultiplicationTable { static void Main() { // Print the multiplication table of a number } }

Run Code?

Click Run Button to view compiled output

ad vertical

1 of 3