Visual Programming Practical Sheet for class 12
Definition of C#:
C# (pronounced "C-sharp") is a modern, object-oriented programming language developed
by Microsoft as part of the .NET framework. It is used for building a wide range of
applications, including desktop, web, and mobile applications. C# is known for its
simplicity, powerful features, and compatibility with Windows-based systems.
Development of C#.NET
• C# was created by Anders Hejlsberg in 2000 as part of Microsoft’s .NET initiative.
Features of C#.NET
• Object-Oriented: Supports classes, objects, inheritance, polymorphism.
• Type-Safe: Ensures data types are correct.
• Rich Library: Extensive library for handling files, databases, networking, etc.
Program 1: If-Else Ladder
This program demonstrates the use of multiple conditions using if, else if, and else.
using System;
class Program
static void Main()
int x = 20;
if (x > 50)
{
Console.WriteLine("Greater than 50");
else if (x == 20)
Console.WriteLine("Equal to 20");
else
Console.WriteLine("Less than 20");
Explanation:
• The program checks whether the value of x is greater than 50, equal to 20, or less
than 20 and prints the corresponding message.
Program 2: For Loop
This program uses a for loop to print the first 5 numbers.
using System;
class Program
static void Main()
// Print numbers from 0 to 4
for (int i = 0; i < 5; i++)
Console.WriteLine(i);
Explanation:
• The for loop starts from i = 0 and runs until i reaches 4, printing the value of i at each
iteration.
Program 3: Multi-dimensional Array
This program demonstrates the use of a 2D array (matrix) in C#.
using System;
class Program
static void Main()
// Declare and initialize a 2x2 matrix
int[,] matrix = { { 1, 2 }, { 3, 4 } };
// Access and display elements from the matrix
Console.WriteLine("Matrix Elements:");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
Console.Write(matrix[i, j] + " ");
Console.WriteLine();
Explanation:
• This program creates a 2x2 matrix and prints the elements of the matrix row by row.
Program 4: String Manipulation
This program demonstrates the use of string methods to manipulate and display a string.
using System;
class Program
static void Main()
// Create a string
string message = "Hello, C# World!";
// Convert to uppercase
string upperMessage = message.ToUpper();
Console.WriteLine("Uppercase: " + upperMessage);
// Get substring from the message
string substring = message.Substring(7, 3);
Console.WriteLine("Substring: " + substring);
// Replace characters in the string
string replacedMessage = message.Replace("World", "Universe");
Console.WriteLine("Replaced Message: " + replacedMessage);
Explanation:
• The program shows how to manipulate a string by converting it to uppercase,
extracting a substring, and replacing a word in the string.
Program 5: Simple Calculator Using Switch Statement
This program demonstrates the use of a switch statement to perform basic arithmetic
operations based on user input.
using System;
class Program
static void Main()
double num1, num2, result;
string operation;
// User input for numbers and operation
Console.WriteLine("Enter first number:");
num1 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter second number:");
num2 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter operation (+, -, *, /):");
operation = Console.ReadLine();
// Switch statement to perform the operation
switch (operation)
case "+":
result = num1 + num2;
Console.WriteLine("Result: " + result);
break;
case "-":
result = num1 - num2;
Console.WriteLine("Result: " + result);
break;
case "*":
result = num1 * num2;
Console.WriteLine("Result: " + result);
break;
case "/":
if (num2 != 0)
result = num1 / num2;
Console.WriteLine("Result: " + result);
else
Console.WriteLine("Error: Division by zero is not allowed.");
break;
default:
Console.WriteLine("Invalid operation entered.");
break;
Explanation:
• The program prompts the user for two numbers and a mathematical operator.
• It uses a switch statement to perform the correct operation based on the user's
input (+, -, *, or /).
• If the user enters an invalid operation or attempts to divide by zero, it handles the
error accordingly