Asp.Net Lab Manual

Shri Ramswaroop Memorial University

Experiment 1: Basic Arithmetic Operations in C# Console Application

Objective

Implement a C# console application to perform basic arithmetic operations: addition, subtraction, multiplication, and division.

Procedure
  1. Create a new C# console application.
  2. Accept two numbers as input from the user.
  3. Perform addition, subtraction, multiplication, and division.
  4. Display the results on the console.
Code Implementation
using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter first number: ");
        double num1 = Convert.ToDouble(Console.ReadLine());
        
        Console.Write("Enter second number: ");
        double num2 = Convert.ToDouble(Console.ReadLine());
        
        Console.WriteLine($"Addition: {num1 + num2}");
        Console.WriteLine($"Subtraction: {num1 - num2}");
        Console.WriteLine($"Multiplication: {num1 * num2}");
        Console.WriteLine($"Division: {(num2 != 0 ? (num1 / num2).ToString() : "Cannot divide by zero")}");
    }
}
        
Key Concepts
  • Console Input and Output in C#
  • Basic Arithmetic Operations
  • Handling Division by Zero

Experiment 2: Display First 10 Natural Numbers and Their Sum in C# Console Application

Objective

Implement a C# console application to display the first 10 natural numbers and their sum.

Procedure
  1. Create a new C# console application.
  2. Use a loop to display the first 10 natural numbers.
  3. Calculate their sum within the loop.
  4. Display the sum on the console.
Code Implementation
using System;

class Program
{
    static void Main()
    {
        int sum = 0;
        Console.WriteLine("The first 10 natural numbers are:");
        for (int i = 1; i <= 10; i++)
        {
            Console.Write(i + " ");
            sum += i;
        }
        Console.WriteLine("\nSum of the first 10 natural numbers: " + sum);
    }
}
        
Key Concepts
  • Looping Constructs in C#
  • Natural Number Sequence
  • Summation using Loops

Experiment 3: Addition Using Windows Application in C#

Objective

Implement a Windows Forms application in C# to perform the addition of two numbers.

Procedure
  1. Create a new Windows Forms application in Visual Studio.
  2. Add two textboxes for user input and a button to perform the addition.
  3. Display the result in a label when the button is clicked.
Code Implementation
using System;
using System.Windows.Forms;

public class AdditionForm : Form
{
    private TextBox txtNum1, txtNum2;
    private Button btnAdd;
    private Label lblResult;

    public AdditionForm()
    {
        this.Text = "Addition Program";
        this.Size = new System.Drawing.Size(300, 200);

        txtNum1 = new TextBox { Location = new System.Drawing.Point(20, 20) };
        txtNum2 = new TextBox { Location = new System.Drawing.Point(20, 50) };
        btnAdd = new Button { Text = "Add", Location = new System.Drawing.Point(20, 80) };
        lblResult = new Label { Location = new System.Drawing.Point(20, 110), AutoSize = true };

        btnAdd.Click += (sender, e) =>
        {
            if (double.TryParse(txtNum1.Text, out double num1) && double.TryParse(txtNum2.Text, out double num2))
            {
                lblResult.Text = "Result: " + (num1 + num2);
            }
            else
            {
                lblResult.Text = "Please enter valid numbers.";
            }
        };

        this.Controls.Add(txtNum1);
        this.Controls.Add(txtNum2);
        this.Controls.Add(btnAdd);
        this.Controls.Add(lblResult);
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new AdditionForm());
    }
}
        
Key Concepts
  • Windows Forms Application
  • Event Handling in C#
  • Basic UI Elements (TextBox, Button, Label)