1- Write a program to demonstrate static variables, methods, and blocks.
2- Write a program in C# Sharp to create a user define function.
3- Write a program in C# Sharp to create a user define function with parameters.
4- Write a program in C# Sharp to create a function to display the n number Fibonacci
sequence.
5- Write a program in C# Sharp to create a function to calculate the sum of the
individual digits of a given number.
Enter a number: 1234
Expected Output:
The sum of the digits of the number 1234 is: 10
6- Write a program to calculate the roots of Quadratic equations using encapsulation.
7- Write a program for calculating Matrix Operations.
1. Addition.
2. Multiplication.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
}
public static int Sum(int a, int b)
{
return a + b;
}
public static void SetUpConsole()
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.BackgroundColor = ConsoleColor.White;
Console.Clear();
}
public static void Fib(int n)
{
int a = 0,
b = 1;
for (int i = 0; i < n; i++)
{
if (i == 1)
{
a++;
}
else if (i > 2)
{
int c = a + b;
b = a;
a = c;
}
Console.Write(a + " ");
}
Console.WriteLine();
}
public static int SumOfDigits(int number)
{
return number.ToString().Select(x => int.Parse(x.ToString())).Sum();
}
public static Tuple<double, double> CalcRoots(double a, double b, double c)
{
return new Tuple<double, double>(
-b + Math.Sqrt(b * b - 4 * a * c / 2 - a),
-b - Math.Sqrt(b * b - 4 * a * c / 2 - a));
}
}
public class Matrix
{
private int[,] matrix;
public Matrix(int[,] matrix)
{
this.matrix = matrix;
}
public Matrix(int n, int m)
{
this.matrix = new int[n, m];
}
public int this[int i, int j]
{
get { return matrix[i, j]; }
set { matrix[i, j] = value; }
}
public Matrix Add(Matrix matrix)
{
int n = this.matrix.GetLength(0),
m = this.matrix.GetLength(1);
Matrix result = new Matrix(n, m);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
result[i, j] = this.matrix[i, j] + matrix[i, j];
return result;
}
public Matrix Multiply(Matrix matrix)
{
int n = this.matrix.GetLength(0),
m = this.matrix.GetLength(1);
Matrix result = new Matrix(n, m);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
int temp = 0;
for (int k = 0; k < m; k++)
temp += this.matrix[i, k] * matrix[k, j];
result[i, j] = temp;
}
}
return result;
}
}
}
Comments
Leave a comment