0% found this document useful (0 votes)
120 views5 pages

C# Program To Demonstrate Multilevel Inheritance

The document contains code examples demonstrating C# programming concepts like single inheritance, multilevel inheritance, array operations like finding the sum of elements, and matrix addition. The code samples show how to define classes that inherit from other classes to represent real-world relationships, store and process data in arrays and matrices, and perform arithmetic operations on matrices by adding corresponding elements.

Uploaded by

piyushi gulati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
120 views5 pages

C# Program To Demonstrate Multilevel Inheritance

The document contains code examples demonstrating C# programming concepts like single inheritance, multilevel inheritance, array operations like finding the sum of elements, and matrix addition. The code samples show how to define classes that inherit from other classes to represent real-world relationships, store and process data in arrays and matrices, and perform arithmetic operations on matrices by adding corresponding elements.

Uploaded by

piyushi gulati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

 C# Program to Illustrate Single Inheritance

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Inheritance
{
class Program
{
static void Main(string[] args)
{
Teacher d = new Teacher();
d.Teach();
Student s = new Student();
s.Learn();
s.Teach();
Console.ReadKey();
}
class Teacher
{
public void Teach()
{
Console.WriteLine("Teach");
}
}
class Student : Teacher
{
public void Learn()
{
Console.WriteLine("Learn");
}
}
}
}

 C# Program to Demonstrate Multilevel Inheritance


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Inherit
{
class inheri : vehicle
{
public void Noise()
{
Console.WriteLine("All Vehicles Creates Noise !! ");
}
static void Main(string[] args)
{
inheri obj = new inheri();
obj.mode();
obj.feature();
obj.Noise();
Console.Read();
}
}
class Mode
{
public void mode()
{
Console.WriteLine("There are Many Modes of Transport !!");
}
}
class vehicle : Mode
{
public void feature()
{
Console.WriteLine("They Mainly Help in Travelling !!");
}
}
}

Output

Find sum of array


using System;
public class Exercise3
{
public static void Main()
{
int[] a= new int[100];
int i, n, sum=0;

Console.Write("\n\nFind sum of all elements of array:\n");


Console.Write("--------------------------------------\n");

Console.Write("Input the number of elements to be stored in the array :");


n = Convert.ToInt32(Console.ReadLine());

Console.Write("Input {0} elements in the array :\n",n);


for(i=0;i<n;i++)
{
Console.Write("element - {0} : ",i);
a[i] = Convert.ToInt32(Console.ReadLine());
}

for(i=0; i<n; i++)


{
sum += a[i];
}

Console.Write("Sum of all elements stored in the array is : {0}\n\n", sum);


}
}

Output

C# Program to Perform Matrix Addition

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication8
{
class Program
{
public static void Main(string[] args)
{
int m, n,i,j;
Console.Write("Enter Number Of Rows And Columns Of Matrices A and B : ");
m = Convert.ToInt16(Console.ReadLine());
n = Convert.ToInt16(Console.ReadLine());
int[,] A = new int[10, 10];
Console.Write("\nEnter The First Matrix : ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
A[i, j] = Convert.ToInt16(Console.ReadLine());
}
}

int[,] B = new int[10, 10];


Console.Write("\nEnter The Second Matrix:");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
B[i, j] = Convert.ToInt16(Console.ReadLine());
}
}
Console.Clear();
Console.WriteLine("\nMatrix A : ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(A[i, j] + "\t");

}
Console.WriteLine();
}
Console.WriteLine("\nMatrix B: ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(B[i, j] + "\t");

}
Console.WriteLine();
}
int[,] C = new int[10, 10];
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
C[i, j] = A[i, j] + B[i, j];
}
}
Console.Write("\nSum Matrix :");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(C[i, j] + "\t");

}
Console.WriteLine();
}
Console.Read();
}
}
}

You might also like