Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
176 views47 pages

C# File

This document contains 10 programs written in C# to perform various tasks. These include programs to display text, convert between temperature scales, calculate area of geometric shapes, find roots of quadratic equations, determine the largest of three numbers, print tables, calculate factorials, perform binary search, sort arrays with bubble sort, and perform operations on matrices. For each program, the problem is defined and the C# code solution is provided with comments explaining the logic.
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)
176 views47 pages

C# File

This document contains 10 programs written in C# to perform various tasks. These include programs to display text, convert between temperature scales, calculate area of geometric shapes, find roots of quadratic equations, determine the largest of three numbers, print tables, calculate factorials, perform binary search, sort arrays with bubble sort, and perform operations on matrices. For each program, the problem is defined and the C# code solution is provided with comments explaining the logic.
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/ 47

C# PRACTICAL FILEMCA-3 RASHPREET KAUR

02413704418
Program 1.Write a program to display "Hello World".

Solution 1.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
classProgram
{
staticvoid Main(string[] args)
{
Console.WriteLine("Hello world");
}
}
}

Program 2. Write a program to perform temperature conversion

(i) Fahrenheit to Celsius

Solution 2

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

namespace Fahrenheit_to_Celsius
{
classProgram
{

1
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
staticvoid Main(string[] args)
{
double celsius;
Console.Write("Enter Fahrenheit temperature : ");
double fahrenheit = Convert.ToDouble(Console.ReadLine());
celsius = (fahrenheit - 32) * 5 / 9;
Console.WriteLine("The converted Celsius temperature is" + celsius);
Console.ReadLine();
}
}
}

(ii) Celsius to Fahrenheit

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

namespace temperature_conversion
{
classProgram
{
staticvoid Main(string[] args)
{
int celsius, faren;
Console.WriteLine("Enter the Temperature in Celsius(°C) : ");
celsius = int.Parse(Console.ReadLine());
faren = (celsius * 9) / 5 + 32;
Console.WriteLine("Temperature in Fahrenheit is(°F) : " + faren);
Console.ReadLine();

}
}

2
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418

Program 3. Write a program to find

(i) Area of Circle

Solution 3

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

namespace Circle
{
classProgram
{
staticvoid Main(string[] args)
{
double radius, area;
Console.WriteLine("Enter Radius: ");
radius = Convert.ToDouble(Console.ReadLine());
area = Math.PI * radius * radius;
Console.WriteLine("\nArea of circle: " + area);
Console.ReadKey();

}
}
}

3
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
(ii) Area of Triangle given its three sides

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

namespace Triangle
{
classProgram
{
staticvoid Main(string[] args)
{
double area_t, s1, s2, s3;
Console.WriteLine("\n----Sides of Triangle are:---- ");
s1 = double.Parse(Console.ReadLine());
s2 = double.Parse(Console.ReadLine());
s3 = double.Parse(Console.ReadLine());
double s = (s1 + s2 + s3) / 2.0;
area_t = Math.Sqrt(s * (s - s1) * (s - s2) * (s - s3));
Console.WriteLine("\nArea of Triangle is:" + area_t);

}
}
}

4
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
Program 4. Write a program to find the roots of a quadratic equation.

Solution 4

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

namespace quadratic
{
classProgram
{
staticvoid Main(string[] args)
{
int a, b, c;

double d, x1, x2;


Console.Write("\n\n");
Console.Write("Calculate root of Quadratic Equation :\n");
Console.Write("----------------------------------------");
Console.Write("\n\n");

Console.Write("Input the value of a : ");


a = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the value of b : ");
b = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the value of c : ");
c = Convert.ToInt32(Console.ReadLine());

d = b * b - 4 * a * c;
if (d == 0)
{
Console.Write("Both roots are equal.\n");
x1 = -b / (2.0 * a);
x2 = x1;
Console.Write("First Root Root1= {0}\n", x1);
Console.Write("Second Root Root2= {0}\n", x2);
}
elseif (d > 0)
{
Console.Write("Both roots are real and diff-2\n");

x1 = (-b + Math.Sqrt(d)) / (2 * a);


x2 = (-b - Math.Sqrt(d)) / (2 * a);

5
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
Console.Write("First Root Root1= {0}\n", x1);
Console.Write("Second Root root2= {0}\n", x2);
}
else
Console.Write("Root are imaginary;\nNo Solution. \n\n");
}

}
}

Program5. Write a program to find largest of three numbers.

Solution5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace largest
{
classProgram
{
staticvoid Main(string[] args)
{
int num1, num2, num3;
Console.Write("\n\n");
Console.Write("Find the largest of three numbers:\n");
Console.Write("------------------------------------");
Console.Write("\n\n");

Console.Write("Input the 1st number :");


num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the 2nd number :");
num2 = Convert.ToInt32(Console.ReadLine());

6
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
Console.Write("Input the 3rd number :");
num3 = Convert.ToInt32(Console.ReadLine());

if (num1 > num2)


{
if (num1 > num3)
{
Console.Write("The 1st Number is the greatest among three. \n\n");
}
else
{
Console.Write("The 3rd Number is the greatest among three. \n\n");
}
}
elseif (num2 > num3)
Console.Write("The 2nd Number is the greatest among three \n\n");
else
Console.Write("The 3rd Number is the greatest among three \n\n");
}
}
}

Program 6. Write a program to display the table of a given number.

Solution6

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

namespace Table
{
classProgram

7
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
{
staticvoid Main(string[] args)
{
int i, num;

//Reading number
Console.WriteLine("Enter number to print table: ");
num = Convert.ToInt32(Console.ReadLine());

for (i = 1; i <= 10; i++)


{
//Printing table of number entered by user
Console.Write("{0} X {1} = {2} \n", num, i, num * i);
}
Console.ReadLine();
}
}
}

Program 7. Write a program to find factorial of a number.

Solution 7
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace factorial
{
classProgram
{
staticvoid Main(string[] args)
{
int i, number, fact;

8
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
Console.WriteLine("Enter the Number");
number = int.Parse(Console.ReadLine());
fact = number;
for (i = number - 1; i >= 1; i--)
{
fact = fact * i;
}
Console.WriteLine("\nFactorial of Given Number is: " + fact);
Console.ReadLine();
}
}
}

Program 8. Write a program to perform binary search on an array.

Solution 8
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
classProgram
{
staticvoid Main(string[] args)
{
int[] a = newint[100];
Console.WriteLine("Number of elements in the array ?");
string s = Console.ReadLine();
int x = Int32.Parse(s);
Console.WriteLine("-----------------------");
Console.WriteLine(" Enter array elements ");
Console.WriteLine("-----------------------");
for (int i = 0; i < x; i++)
{

9
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
string s1 = Console.ReadLine();
a[i] = Int32.Parse(s1);
}
Console.WriteLine("--------------------");
Console.WriteLine("Enter Search element");
Console.WriteLine("--------------------");
string s3 = Console.ReadLine();
int x2 = Int32.Parse(s3);
int low = 0;
int high = x - 1;
while (low <= high)
{
int mid = (low + high) / 2;
if (x2 < a[mid])
high = mid - 1;
elseif (x2 > a[mid])
low = mid + 1;
elseif (x2 == a[mid])
{
Console.WriteLine("-----------------");
Console.WriteLine("Search successful");
Console.WriteLine("-----------------");
Console.WriteLine("Element {0} found at location {1}\n", x2, mid + 1);
return;
}
}
Console.WriteLine("Search unsuccessful");
}
}
}

10
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
Program 9. Write a program to sort an array using bubble sort.

Solution 9

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

namespace BubbleSort
{
classProgram
{
staticvoid Main(string[] args)
{
int[] a = { 3, 2, 5, 4, 1 };
int t;
Console.WriteLine("The Array is : ");
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i]);
}
for (int j = 0; j <= a.Length - 2; j++)
{
for (int i = 0; i <= a.Length - 2; i++)
{
if (a[i] > a[i + 1])
{
t = a[i + 1];
a[i + 1] = a[i];
a[i] = t;
}
}
}
Console.WriteLine("The Sorted Array :");
foreach (int aray in a)
Console.Write(aray + " ");
Console.ReadLine();
}

}
}

11
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418

Program 10. Write a program to perform following operations on matrix

(i) Addition

(ii) Subtraction

(iii) Multiplication

(iv) Transpose

Write user-defined functions for above operations.

Solution10

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

namespace Matrixx
{
classProgram
{
publicstaticvoid addition()
{
int m, n, i, j;
Console.Write("Enter Number Of Rows And Columns Of Matrices A and B : \n");
m = Convert.ToInt16(Console.ReadLine());
n = Convert.ToInt16(Console.ReadLine());
int[,] A = newint[10, 10];
Console.Write("\nEnter The First Matrix : \n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
A[i, j] = Convert.ToInt16(Console.ReadLine());

12
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
}
}

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


Console.Write("\nEnter The Second Matrix : \n");
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 = newint[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 : \n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{

13
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
Console.Write(C[i, j] + "\t");

}
Console.WriteLine();
}
}
publicstaticvoid subtraction()
{
int m, n, i, j;
Console.Write("Enter Number Of Rows And Columns Of Matrices A and B : \n");
m = Convert.ToInt16(Console.ReadLine());
n = Convert.ToInt16(Console.ReadLine());
int[,] A = newint[10, 10];
Console.Write("\nEnter The First Matrix : \n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
A[i, j] = Convert.ToInt16(Console.ReadLine());
}
}

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


Console.Write("\nEnter The Second Matrix : \n");
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 : \n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(A[i, j] + "\t");

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

14
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
{
Console.Write(B[i, j] + "\t");

}
Console.WriteLine();
}
int[,] C = newint[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("\nDifference Matrix : \n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(C[i, j] + "\t");

}
Console.WriteLine();
}
}
publicstaticvoid multiplication()
{
int i, j, m, n;
Console.WriteLine("Enter the Number of Rows and Columns : ");
m = Convert.ToInt32(Console.ReadLine());
n = Convert.ToInt32(Console.ReadLine());
int[,] a = newint[m, n];
Console.WriteLine("Enter the First Matrix");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
a[i, j] = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("First matrix is:");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(a[i, j] + "\t");

15
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
}
Console.WriteLine();
}
int[,] b = newint[m, n];
Console.WriteLine("Enter the Second Matrix");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
b[i, j] = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("Second Matrix is :");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
Console.Write(b[i, j] + "\t");
}
Console.WriteLine();
}
Console.WriteLine("Matrix Multiplication is :");
int[,] c = newint[m, n];
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
c[i, j] = 0;
for (int k = 0; k < 2; k++)
{
c[i, j] += a[i, k] * b[k, j];
}
}
}
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(c[i, j] + "\t");
}
Console.WriteLine();
}
}
publicstaticvoid transpose()
{
int m, n, i, j;

16
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
Console.Write("Enter the Order of the Matrix : \n");
m = Convert.ToInt16(Console.ReadLine());
n = Convert.ToInt16(Console.ReadLine());
int[,] A = newint[10, 10];
Console.Write("\nEnter The Matrix Elements : \n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
A[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("Transpose Matrix : ");

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


{
for (j = 0; j < n; j++)
{
Console.Write(A[j, i] + "\t");

}
Console.WriteLine();
}
}
staticvoid Main(string[] args)
{
int n;
Console.WriteLine("Choose any One Operation to Perform");
Console.WriteLine("1. Addition");
Console.WriteLine("2. Subtraction");
Console.WriteLine("3. Multiplication");
Console.WriteLine("4. Transpose");
Console.WriteLine("\n Enter Operation no : ");
n = Convert.ToInt16(Console.ReadLine());
switch (n)

17
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
{

case 1:
addition();
break;

case 2:
subtraction();
break;

case 3:
multiplication();
break;

case 4:
transpose();
break;

default:
Console.WriteLine("Invalid Selection...!!");
break;
}
Console.Read();

}
}
}

18
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418

19
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418

Program 11. Write a program to demonstrate the use of const and readonly.
20
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
Solution11

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

namespace Const
{
classProgram
{
constint a = 10;
staticvoid Main(string[] args)
{
constint b = 20;
constint c = b + a;
Console.WriteLine(c);
Console.ReadLine();
}
}
}

Program 12. Write a program using user-defined function to return all factors of a number
and display them.

21
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
Solution 12
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Factors
{
classProgram
{
staticvoid Main(string[] args)
{
int num, x;
Console.WriteLine("Enter the Number ");
num = int.Parse(Console.ReadLine());
Console.WriteLine("The Factors are : ");
for (x = 1; x <= num; x++)
{
if (num % x == 0)
{
Console.WriteLine(x);
}
}
Console.ReadLine();
}
}
}

22
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
Program 13. Write a program to perform following operations on array:-

(i) insert

(ii) delete

(iii) frequency of an element

Solution 13

23
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
Program 14. Write a program using user defined functions that takes an n-digit number
and performs following operations

(i) count the number of digits

(ii) find sum of digits

(ii) find reverse of digits

Solution 14
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
classProgram
{
staticvoid Main(string[] args)
{
int number, sum = 0;
Console.WriteLine("Enter a number:");
int input = int.Parse(Console.ReadLine());
number = input;
while (number != 0)
{
int m = number % 10;
number = number / 10;
sum = sum + m;
}
Console.WriteLine("Sum of digits of the number:" + sum);
Console.ReadLine();
number = input;

int reverse = 0;

while (number != 0)
{
reverse = reverse * 10;
reverse = reverse + number % 10;
number = number / 10;
}
Console.WriteLine("Reversed Number is:" + reverse);
Console.ReadLine();

24
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418

}
}
}

Program 15. Write a program to display the count of objects created

Solution 15
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Object
{
classProgram
{
staticvoid Main(string[] args)
{
test obj = newtest();
test obj1 = newtest();
test obj2 = newtest();
test obj3 = newtest();
Console.WriteLine("Number of objects created:" + test.count);
}
}
classtest
{
publicstaticint count=0;
public test()
{
count = count + 1;
}
}
}

25
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418

Program 16 .Write a program to display the marks in 3 subjects, total marks and
percentage of each student of a class of 20 students with displaying 5 records per screen.

Solution 16

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

namespace ConsoleApplication2
{
classProgram
{
staticvoid Main(string[] args)
{
double rl, phy, che, ca, total;
double per;
string nm, div;

Console.Write("\n\n");
Console.Write("Calculate the total, percentage and division to take marks of three subjects:\n");
Console.Write("-------------------------------------------------------------------------------");
Console.Write("\n\n");

Console.Write("Input the Roll Number of the student :");


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

Console.Write("Input the Name of the Student :");


nm = Console.ReadLine();

Console.Write("Input the marks of Physics : ");


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

26
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
Console.Write("Input the marks of Chemistry : ");
che = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the marks of Computer Application : ");
ca = Convert.ToInt32(Console.ReadLine());

total = phy + che + ca;


per = total / 3.0;
if (per >= 60)
div = "First";
else
if (per < 60 && per >= 48)
div = "Second";
else
if (per < 48 && per >= 36)
div = "Pass";
else
div = "Fail";

Console.Write("\nRoll No : {0}\nName of Student : {1}\n", rl, nm);


Console.Write("Marks in Physics : {0}\nMarks in Chemistry : {1}\nMarks in Computer
Application : {2}\n", phy, che, ca);
Console.Write("Total Marks = {0}\nPercentage = {1}\nDivision = {2}\n", total, per, div);
}
}
}

Program 17 .Write program that randomly generates a number from a range 10-99 and
checks whether it is Armstrong or not.

27
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
Solution 17

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

namespace ConsoleApp9
{
classProgram
{
staticvoid Main(string[] args)
{
int n, r, sum = 0, temp;
Console.Write("Enter the Number= ");
n = int.Parse(Console.ReadLine());
temp = n;
while (n > 0)
{
r = n % 10;
sum = sum + (r * r * r);
n = n / 10;
}
if (temp == sum)
Console.Write("Armstrong Number.");
else
Console.Write("Not Armstrong Number.");
Console.ReadLine();
}
}
}

28
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418

Program 18.Write a program to calculate the standard deviation of an array of values.

Use methods Standard and Mean to calculate standard deviation and mean of values.

Solution 18

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

namespace ConsoleApp3
{
classProgram
{
staticvoid Main(string[] args)
{
int n;
float mean, median, std;
Console.Write("Enter number of data points:");
n = int.Parse(Console.ReadLine());
if (n < 3)
{
Console.WriteLine("The number of data points should be greater than 2.");

}
else
{

//declare an array of n size to store integral data points


int[] dataset = newint[n];
//allow user inputs
int i = 0;
for (i = 0; i < n; i++)
{
Console.Write("[{0}]:", i);
dataset[i] = int.Parse(Console.ReadLine());

29
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
}

//sort the data set


bubblesort(dataset, n);

//calculate the mean


int sum = 0;
int j = 0;
while (j < n)
{
sum = sum + dataset[j];
j++;
}

mean = (float)sum / n;

//calculate median
//If n is odd, median=dataset[n/2]
//If n is even, median=(dataset[n/2]+dataset[1+n/2])/2
//The index of array starts from 0, so you need to subtract 1 from the //indices used in
calculating the median
if (n % 2 != 0) median = dataset[n / 2];
else median = (dataset[(n / 2) - 1] + dataset[n / 2]) / (float)2;

//calculate the mode


int[,] mode = newint[n, 2];
//initialize 2D array storing numbers of occurences, and values
for (i = 0; i < 2; i++)
for (j = 0; j < n; j++) mode[j, i] = 0;
mode[0, 0] = 1;

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


for (j = 0; j < n - 1; j++)
if (dataset[i] == dataset[j + 1]) { ++mode[i, 0]; mode[i, 1] = dataset[i]; }

int max;
int k = 0;
max = mode[0, 0];
for (j = 0; j < n; j++)
if (max < mode[j, 0]) { max = mode[j, 0]; k = j; }

//calculate standard deviation, std


float temp = 0.0f;

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

30
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
{
temp = temp + (float)Math.Pow(dataset[j] - mean, 2);
}

std = (float)Math.Sqrt(temp / (n - 1));

//Show results

Console.WriteLine("Statistical Information:");
Console.WriteLine("..................................................");
Console.WriteLine("Arithmetic mean:{0}", mean);
Console.WriteLine("Median:{0}", median);
if (mode[k, 1] != 0)
Console.WriteLine("Mode:{0}", mode[k, 1]);
else Console.WriteLine("Mode: no mode");
Console.WriteLine("Standard deviation:{0}", std);

}
Console.ReadLine();

///bubble sort

staticvoid bubblesort(int[] dataset, int n)


{
int i, j;
for (i = 0; i < n; i++)
for (j = n - 1; j > i; j--)
if (dataset[j] < dataset[j - 1])
{
int temp = dataset[j];
dataset[j] = dataset[j - 1];
dataset[j - 1] = temp;
}
}
}
}

31
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418

Program 19. Write a program to demonstrate the working of any Ten String class
functions.

Solution 19

32
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
Program20.Write a program to sort a list of strings.

Solution 20

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

namespace String
{
classProgram
{
staticvoid Main(string[] args)
{
string str;
char[] arr1;
char ch;
int i, j, l;
Console.Write("\n\nSort a string array in ascending order :\n");
Console.Write("--------------------------------------------\n");
Console.Write("Input the string : ");
str = Console.ReadLine();
l = str.Length;
arr1 = str.ToCharArray(0, l);

for (i = 1; i < l; i++)


for (j = 0; j < l - i; j++)

if (arr1[j] > arr1[j + 1])


{
ch = arr1[j];
arr1[j] = arr1[j + 1];
arr1[j + 1] = ch;
}
Console.Write("After sorting the string appears like : \n");
foreach (char c in arr1)
{
ch = c;
Console.Write("{0} ", ch);
}
Console.WriteLine("\n");
}

}
}

33
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418

Program21.Write a program that takes two arrays as input and produces a third array by
merging them.

Solution 21

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

namespace ConsoleApp11
{
classProgram
{
staticvoid Main(string[] args)
{
int[] array1 = newint[5];
int[] array2 = newint[5];
int[] array3 = newint[array1.Length + array2.Length];
Console.WriteLine("Enter Any 5 Elements for the First Array :");
for (int i = 0; i < 5; i++)
{
array1[i] = int.Parse(Console.ReadLine());
}
Console.WriteLine("Enter Any 5 Elements for the Second Array :");
for (int i = 0; i < 5; i++)
{
array2[i] = int.Parse(Console.ReadLine());
}
Buffer.BlockCopy(array1, 0, array3, 0, array1.Length * sizeof(int));

34
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
Buffer.BlockCopy(array2, 0, array3, array1.Length * sizeof(int), array2.Length *
sizeof(int));
Console.WriteLine("Elements in the Third Array After Merging First and Second Arrays
:");
foreach (int value in array3)
{
Console.WriteLine(value);
}
Console.ReadLine();
}
}
}

Program22.Define a jagged array and write methods to input its elements and display.

Solution 22

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

namespace ConsoleApp7
{
classProgram
{

35
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
staticvoid Main(string[] args)
{
//Creations of jaggedArray
string[][] jaggedArray = newstring[3][];

//Array of string Array


jaggedArray[0] = newstring[3];
jaggedArray[1] = newstring[1];
jaggedArray[2] = newstring[2];

//values of 1st string Array

jaggedArray[0][0] = "Hello";
jaggedArray[0][1] = "How are u";
jaggedArray[0][2] = "Rashpreet";

//values of 2nd string Array

jaggedArray[1][0] = "Hello";

//values of 3rd string Array

jaggedArray[2][0] = "Hello";
jaggedArray[2][1] = "How are u";

//for getting string array in jagged array


for (int i = 0; i < jaggedArray.Length; i++)
{
string[] innerArray = jaggedArray[i];
//for getting values of innerArray or string Array
for (int j = 0; j < innerArray.Length; j++)
{
//print the values
Console.WriteLine(innerArray[j]);
}
//for blank space
Console.WriteLine();
Console.ReadLine();

}
}
}

36
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418

Program 23.Write a program to demonstrate the methods of Array class.

Solution 23

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

namespace ConsoleApp4
{
classProgram
{
staticvoid printarray(int[] arr)
{
Console.WriteLine("\nElements of array is:\n");
foreach (int i in arr)
{
Console.Write("\t{0}", i);
}
Console.WriteLine("\n");
}
staticvoid Main(string[] args)
{
//Initializing and storing value in arr1
int[] arr1 = newint[5] { 43, 25, 33, 14, 5 };
int[] arr2 = newint[5];
int len, rank;
bool fixedsize, read_only;

37
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
//Check array length
len = arr1.Length;
Console.WriteLine("Length:\t{0}", len);

//Check array rank


rank = arr1.Rank;
Console.WriteLine("Rank:\t{0}", rank);

//Check whether array is fixed size or not


fixedsize = arr1.IsFixedSize;
Console.WriteLine("Fixed Size:\t{0}", fixedsize);

//Check whether array is read only or not


read_only = arr1.IsReadOnly;
Console.WriteLine("Read Only:\t{0}", read_only);

//Sorting an array
Array.Sort(arr1);
printarray(arr1);

//Returning Lenght from specified position


Console.WriteLine("Get Length:\t{0}", arr1.GetLength(0));

//Returns value of specied position


Console.WriteLine("Get Value:\t{0}", arr1.GetValue(2));

//Returns Index position of specified value


Console.WriteLine("Get Index:\t{0}", Array.IndexOf(arr1, 33));

//Copying arr1's items to arr2


Array.Copy(arr1, arr2, 5);
printarray(arr2);

//Removing items from array.


Array.Clear(arr1, 0, 5);
printarray(arr1);

Console.ReadLine();

}
}
}

38
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418

Program 24.Write a program to demonstrate the methods of ArrayList class.

Solution 24

using System;
using System.Collections;

namespace ConsoleApp5
{
classProgram
{
staticvoid Main(string[] args)
{
ArrayList al = new ArrayList();

Console.WriteLine("Adding some numbers:");


al.Add(55);
al.Add(77);
al.Add(22);
al.Add(52);
al.Add(11);
al.Add(16);
al.Add(10);

Console.WriteLine("Capacity: {0} ", al.Capacity);


Console.WriteLine("Count: {0}", al.Count);

Console.Write("Content: ");
foreach (int i in al)

39
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
{
Console.Write(i + " ");
}

Console.WriteLine();
Console.Write("Sorted Content: ");
al.Sort();
foreach (int i in al)
{
Console.Write(i + " ");
}
Console.WriteLine();
Console.ReadKey();

}
}
}

Program 25.Write a program to perform following operations on strings.

a. Count the number of character.

b. Display the frequency of a character and a word.

c. Count the number of words

d. Find whether the string is palindrome or not.

e. Reverse the string

40
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418

Solution 25 (a)Count the number of character.

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

namespace ConsoleApp12
{
classProgram
{
staticvoid Main(string[] args)
{
string str;
int alp, digit, splch, i, l;
alp = digit = splch = i = 0;

Console.Write("\n\nCount total number of alphabets, digits and special characters :\n");


Console.Write("--------------------------------------------------------------------\n");
Console.Write("Input the string : ");
str = Console.ReadLine();
l = str.Length;

/* Checks each character of string*/

while (i < l)
{
if ((str[i] >= 'a'&& str[i] <= 'z') || (str[i] >= 'A'&& str[i] <= 'Z'))
{
alp++;
}
elseif (str[i] >= '0'&& str[i] <= '9')
{
digit++;
}
else
{
splch++;
}

i++;
}

Console.Write("Number of Alphabets in the string is : {0}\n", alp);

41
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
Console.Write("Number of Digits in the string is : {0}\n", digit);
Console.Write("Number of Special characters in the string is : {0}\n\n", splch);
Console.ReadLine();
}
}
}

(b)Display the frequency of a character and a word.

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

namespace ConsoleApp13
{
classProgram
{
staticvoid Main(string[] args)
{

string input = "Rashpreet Kaur";


Console.WriteLine("Input String = Rashpreet Kaur");

while (input.Length > 0)

Console.Write(input[0] + " : ");

int count = 0;

42
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
for (int j = 0; j < input.Length; j++)

if (input[0] == input[j])

count++;

Console.WriteLine(count);

input = input.Replace(input[0].ToString(), string.Empty);

Console.ReadLine();

}
}
}

43
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418
c. Count the number of words

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

namespace ConsoleApp14
{
classProgram
{
staticvoid Main(string[] args)
{
string str;
int wrd, l;

Console.Write("\n\nCount the total number of words in a string :\n");


Console.Write("------------------------------------------------------\n");
Console.Write("Input the string : ");
str = Console.ReadLine();

l = 0;
wrd = 1;

/* loop till end of string */


while (l <= str.Length - 1)
{
/* check whether the current character is white space or new line or tab character*/
if (str[l] == ' ' || str[l] == '\n' || str[l] == '\t')
{
wrd++;
}

l++;
}

Console.Write("Total number of words in the string is : {0}\n", wrd);


Console.ReadLine();

}
}
}

44
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418

d. Find whether the string is palindrome or not.

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

namespace ConsoleApp14
{
classProgram
{
staticvoid Main(string[] args)
{
string s, revs = "";
Console.WriteLine(" Enter string");
s = Console.ReadLine();
for (int i = s.Length - 1; i >= 0; i--) //String Reverse
{
revs += s[i].ToString();
}
if (revs == s) // Checking whether string is palindrome or not
{
Console.WriteLine("String is Palindrome \n Entered String Was {0} and reverse string
is {1}", s, revs);
}
else
{
Console.WriteLine("String is not Palindrome \n Entered String Was {0} and reverse
string is {1}", s, revs);
}
Console.ReadKey();
}
}
}

45
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418

e. Reverse the string

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

namespace ConsoleApp14
{
classProgram
{
staticvoid Main(string[] args)
{
string Str, reversestring = "";
int Length;
Console.Write("Enter A String : ");
Str = Console.ReadLine();
Length = Str.Length - 1;
while (Length >= 0)
{
reversestring = reversestring + Str[Length];
Length--;
}
Console.WriteLine("Reverse String Is : {0}", reversestring);
Console.ReadLine();
}

}
}

46
C# PRACTICAL FILEMCA-3 RASHPREET KAUR
02413704418

47

You might also like