C# Examples

Welcome to The Coding College! In this post, we’ll cover various examples of C# programs that showcase different features of the language. These examples are perfect for beginners and intermediate learners looking to understand core C# concepts.

1. Hello World Program

Code:

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Hello, World!");
    }
}

Output:

Hello, World!

Explanation:

  • The Console.WriteLine function is used to print text to the console.
  • This is the simplest C# program and a great starting point.

2. Add Two Numbers

Code:

using System;

class Program
{
    static void Main()
    {
        int num1 = 10;
        int num2 = 20;
        int sum = num1 + num2;

        Console.WriteLine("The sum of " + num1 + " and " + num2 + " is: " + sum);
    }
}

Output:

The sum of 10 and 20 is: 30

Explanation:

  • Declares two integers (num1 and num2).
  • Adds them together using the + operator and stores the result in sum.

3. Check If a Number Is Even or Odd

Code:

using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter a number: ");
        int num = Convert.ToInt32(Console.ReadLine());

        if (num % 2 == 0)
        {
            Console.WriteLine(num + " is an even number.");
        }
        else
        {
            Console.WriteLine(num + " is an odd number.");
        }
    }
}

Output:

Enter a number: 15
15 is an odd number.

Explanation:

  • Uses the modulus operator (%) to check divisibility by 2.
  • If the remainder is 0, the number is even; otherwise, it’s odd.

4. Find the Largest of Three Numbers

Code:

using System;

class Program
{
    static void Main()
    {
        int a = 10, b = 20, c = 15;

        if (a > b && a > c)
        {
            Console.WriteLine(a + " is the largest.");
        }
        else if (b > a && b > c)
        {
            Console.WriteLine(b + " is the largest.");
        }
        else
        {
            Console.WriteLine(c + " is the largest.");
        }
    }
}

Output:

20 is the largest.

Explanation:

  • Compares the three numbers using logical operators (&&).
  • Prints the largest number based on the conditions.

5. Simple Calculator

Code:

using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter first number: ");
        double num1 = Convert.ToDouble(Console.ReadLine());

        Console.Write("Enter an operator (+, -, *, /): ");
        char op = Console.ReadKey().KeyChar;
        Console.WriteLine();

        Console.Write("Enter second number: ");
        double num2 = Convert.ToDouble(Console.ReadLine());

        double result = 0;

        switch (op)
        {
            case '+':
                result = num1 + num2;
                break;
            case '-':
                result = num1 - num2;
                break;
            case '*':
                result = num1 * num2;
                break;
            case '/':
                result = num1 / num2;
                break;
            default:
                Console.WriteLine("Invalid operator!");
                return;
        }

        Console.WriteLine("Result: " + result);
    }
}

Output:

Enter first number: 10
Enter an operator (+, -, *, /): +
Enter second number: 5
Result: 15

Explanation:

  • Takes two numbers and an operator as input.
  • Uses a switch statement to perform the corresponding operation.

6. Reverse a String

Code:

using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter a string: ");
        string str = Console.ReadLine();

        char[] charArray = str.ToCharArray();
        Array.Reverse(charArray);

        Console.WriteLine("Reversed string: " + new string(charArray));
    }
}

Output:

Enter a string: hello
Reversed string: olleh

Explanation:

  • Converts the string into a character array.
  • Reverses the array using Array.Reverse and converts it back to a string.

7. Factorial of a Number

Code:

using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter a number: ");
        int num = Convert.ToInt32(Console.ReadLine());
        int factorial = 1;

        for (int i = 1; i <= num; i++)
        {
            factorial *= i;
        }

        Console.WriteLine("Factorial of " + num + " is: " + factorial);
    }
}

Output:

Enter a number: 5
Factorial of 5 is: 120

Explanation:

  • Multiplies numbers from 1 to the entered number to calculate the factorial.

8. Fibonacci Sequence

Code:

using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter the number of terms: ");
        int terms = Convert.ToInt32(Console.ReadLine());
        int a = 0, b = 1, next;

        Console.WriteLine("Fibonacci sequence:");
        for (int i = 1; i <= terms; i++)
        {
            Console.WriteLine(a);
            next = a + b;
            a = b;
            b = next;
        }
    }
}

Output:

Enter the number of terms: 5
Fibonacci sequence:
0
1
1
2
3

Explanation:

  • Uses a loop to generate the Fibonacci sequence.

Conclusion

These examples demonstrate key programming concepts in C#. Practice them to build a strong understanding of the language. At The Coding College, we aim to provide you with practical examples and resources to enhance your coding journey.

Explore more C# tutorials on our website to take your programming skills to the next level! 🚀

Leave a Comment