CSS326 Lab 1 - Exercise Solutions

Updated 4 Oct 2026

Exercise 1: Type Casting Practice

using System;
 
class Program
{
    static void Main()
    {
        int myInt = 8;
        double myDouble = 7.89;
        bool myBool = true;
 
        Console.WriteLine(Convert.ToString(myInt));    // convert int to string
        Console.WriteLine(Convert.ToDouble(myInt));    // convert int to double
        Console.WriteLine(Convert.ToInt32(myDouble));  // convert double to int
        Console.WriteLine(Convert.ToString(myBool));   // convert bool to string
    }
}

Output:

8
8
7
True

Exercise 2: Math Class Exercises

1) Absolute Sum

Write a C# program that takes three integer inputs from the user and calculates the sum of their absolute values.

using System;
 
class Program
{
    static void Main()
    {
        Console.WriteLine("Enter three integers:");
        
        Console.Write("First number: ");
        int num1 = Convert.ToInt32(Console.ReadLine());
        
        Console.Write("Second number: ");
        int num2 = Convert.ToInt32(Console.ReadLine());
        
        Console.Write("Third number: ");
        int num3 = Convert.ToInt32(Console.ReadLine());
        
        int sum = Math.Abs(num1) + Math.Abs(num2) + Math.Abs(num3);
        
        Console.WriteLine($"Sum of absolute values: {sum}");
    }
}

2) Hypotenuse Calculator

Write a C# program that takes the lengths of two sides of a right-angled triangle and calculates the hypotenuse.

using System;
 
class Program
{
    static void Main()
    {
        Console.WriteLine("Hypotenuse Calculator");
        
        Console.Write("Enter the length of side a: ");
        double a = Convert.ToDouble(Console.ReadLine());
        
        Console.Write("Enter the length of side b: ");
        double b = Convert.ToDouble(Console.ReadLine());
        
        double hypotenuse = Math.Sqrt(Math.Pow(a, 2) + Math.Pow(b, 2));
        
        Console.WriteLine($"The length of the hypotenuse is: {hypotenuse:F2}");
    }
}

If Statement Exercises

1) Even or Odd Number Checker

using System;
 
class Program
{
    static void Main()
    {
        Console.Write("Enter a number: ");
        int number = Convert.ToInt32(Console.ReadLine());
        
        if (number % 2 == 0)
        {
            Console.WriteLine($"{number} is even.");
        }
        else
        {
            Console.WriteLine($"{number} is odd.");
        }
    }
}

2) Voting Eligibility Checker

using System;
 
class Program
{
    static void Main()
    {
        Console.Write("Enter your age: ");
        int age = Convert.ToInt32(Console.ReadLine());
        
        if (age >= 18)
        {
            Console.WriteLine("You are eligible to vote.");
        }
        else
        {
            Console.WriteLine("You are not eligible to vote yet.");
        }
    }
}

3) Leap Year Checker

using System;
 
class Program
{
    static void Main()
    {
        Console.Write("Enter a year: ");
        int year = Convert.ToInt32(Console.ReadLine());
        
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
        {
            Console.WriteLine($"{year} is a leap year.");
        }
        else
        {
            Console.WriteLine($"{year} is not a leap year.");
        }
    }
}

Break & Continue Exercises

1) Find First Multiple of 7 (Using Break)

using System;
 
class Program
{
    static void Main()
    {
        Console.WriteLine("Finding the first multiple of 7 between 1 and 50:");
        
        for (int i = 1; i <= 50; i++)
        {
            if (i % 7 == 0)
            {
                Console.WriteLine($"The first multiple of 7 is: {i}");
                break;
            }
        }
    }
}

2) Print Even Numbers Skipping 10 (Using Continue)

using System;
 
class Program
{
    static void Main()
    {
        Console.WriteLine("Even numbers from 0 to 20 (skipping 10):");
        
        int number = 0;
        while (number <= 20)
        {
            if (number == 10)
            {
                number += 2; // Move to next even number
                continue;
            }
            
            if (number % 2 == 0)
            {
                Console.WriteLine(number);
            }
            
            number++;
        }
    }
}

Alternative solution using for loop:

using System;
 
class Program
{
    static void Main()
    {
        Console.WriteLine("Even numbers from 0 to 20 (skipping 10):");
        
        for (int i = 0; i <= 20; i++)
        {
            if (i % 2 != 0) // Skip odd numbers
                continue;
                
            if (i == 10) // Skip 10
                continue;
                
            Console.WriteLine(i);
        }
    }
}

Complete Rectangle Class Example

using System;
 
public class Rectangle
{
    // Data members (fields)
    public double width;
    public double height;
    
    // Member function (method)
    public double CalculateArea()
    {
        return width * height;
    }
    
    // Additional method to calculate perimeter
    public double CalculatePerimeter()
    {
        return 2 * (width + height);
    }
    
    // Method to display rectangle information
    public void DisplayInfo()
    {
        Console.WriteLine($"Rectangle: Width = {width}, Height = {height}");
        Console.WriteLine($"Area = {CalculateArea()}");
        Console.WriteLine($"Perimeter = {CalculatePerimeter()}");
    }
}
 
class Program
{
    static void Main()
    {
        // Create first rectangle
        Rectangle rectangle1 = new Rectangle();
        rectangle1.width = 5.0;
        rectangle1.height = 3.0;
        
        // Create second rectangle
        Rectangle rectangle2 = new Rectangle();
        rectangle2.width = 8.0;
        rectangle2.height = 4.5;
        
        // Calculate and display areas
        double area1 = rectangle1.CalculateArea();
        double area2 = rectangle2.CalculateArea();
        
        Console.WriteLine("Rectangle 1:");
        rectangle1.DisplayInfo();
        Console.WriteLine();
        
        Console.WriteLine("Rectangle 2:");
        rectangle2.DisplayInfo();
        Console.WriteLine();
        
        Console.WriteLine($"Area of rectangle 1: {area1}");
        Console.WriteLine($"Area of rectangle 2: {area2}");
    }
}

Output:

Rectangle 1:
Rectangle: Width = 5, Height = 3
Area = 15
Perimeter = 16

Rectangle 2:
Rectangle: Width = 8, Height = 4.5
Area = 36
Perimeter = 25

Area of rectangle 1: 15
Area of rectangle 2: 36

Additional Practice Exercises

Simple Calculator Using Methods

using System;
 
class Calculator
{
    public static double Add(double a, double b)
    {
        return a + b;
    }
    
    public static double Subtract(double a, double b)
    {
        return a - b;
    }
    
    public static double Multiply(double a, double b)
    {
        return a * b;
    }
    
    public static double Divide(double a, double b)
    {
        if (b != 0)
            return a / b;
        else
        {
            Console.WriteLine("Error: Division by zero!");
            return 0;
        }
    }
    
    static void Main()
    {
        Console.WriteLine("Simple Calculator");
        Console.Write("Enter first number: ");
        double num1 = Convert.ToDouble(Console.ReadLine());
        
        Console.Write("Enter operator (+, -, *, /): ");
        char operation = Convert.ToChar(Console.ReadLine());
        
        Console.Write("Enter second number: ");
        double num2 = Convert.ToDouble(Console.ReadLine());
        
        double result = 0;
        
        switch (operation)
        {
            case '+':
                result = Add(num1, num2);
                break;
            case '-':
                result = Subtract(num1, num2);
                break;
            case '*':
                result = Multiply(num1, num2);
                break;
            case '/':
                result = Divide(num1, num2);
                break;
            default:
                Console.WriteLine("Invalid operator!");
                return;
        }
        
        Console.WriteLine($"Result: {num1} {operation} {num2} = {result}");
    }
}

Student Grade System

using System;
 
public class Student
{
    public string Name;
    public int[] Scores;
    
    public Student(string name, int[] scores)
    {
        Name = name;
        Scores = scores;
    }
    
    public double CalculateAverage()
    {
        int sum = 0;
        foreach (int score in Scores)
        {
            sum += score;
        }
        return (double)sum / Scores.Length;
    }
    
    public char GetGrade()
    {
        double average = CalculateAverage();
        
        if (average >= 90) return 'A';
        else if (average >= 80) return 'B';
        else if (average >= 70) return 'C';
        else if (average >= 60) return 'D';
        else return 'F';
    }
    
    public void DisplayStudentInfo()
    {
        Console.WriteLine($"Student: {Name}");
        Console.Write("Scores: ");
        foreach (int score in Scores)
        {
            Console.Write($"{score} ");
        }
        Console.WriteLine();
        Console.WriteLine($"Average: {CalculateAverage():F2}");
        Console.WriteLine($"Grade: {GetGrade()}");
    }
}
 
class Program
{
    static void Main()
    {
        // Create student with scores
        int[] johnScores = { 85, 92, 78, 96, 88 };
        Student john = new Student("John Doe", johnScores);
        
        int[] janeScores = { 76, 84, 91, 73, 82 };
        Student jane = new Student("Jane Smith", janeScores);
        
        // Display student information
        john.DisplayStudentInfo();
        Console.WriteLine();
        jane.DisplayStudentInfo();
    }
}

These solutions provide working implementations for all the exercises mentioned in the lab document, along with some additional practice problems to reinforce the concepts learned.