Answer to Question #330353 in C# for Babar302

Question #330353

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.


1
Expert's answer
2022-04-19T10:02:05-0400
var fibonacci = Calc.GetFibonacci(5);
Console.WriteLine(fibonacci);

var numberSum = Calc.CalculateNumberSum(1234);
Console.WriteLine(numberSum);

var quadraticEquation = new QuadraticEquation(1, 2, 1);
var roots = Calc.SolveQuadraticEquation(quadraticEquation);
roots.PrintRoots();

Calc.DoActionWithMatrix();


public static class Calc
{
   public static readonly int[,] firstMatrix =  { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
   public static readonly int[,] secondMatrix =  { { 2, 3 }, { 4, 5 }, { 6, 7 }, { 8, 9 } };

   public static void DoActionWithMatrix()
   {
      Console.WriteLine("Do matrix addition");
      var matrixAdditionResult = DoMatrixAddition(firstMatrix, secondMatrix);
      PrintMatrix(matrixAdditionResult);

      Console.WriteLine("Do matrix multiplication");
      var matrixMultiplicationResult = DoMatrixMultiplication(firstMatrix, secondMatrix);
      PrintMatrix(matrixMultiplicationResult);
   }

   public static int GetFibonacci(int n)
   {
      if (n == 0)
      {
         return 0;
      }

      if (n == 1)
      {
         return 1;
      }

      var firstNumber = 0;
      var secondNumber = 1;
      var result = 0;
      for (var i = 2; i<= n; i++)
      {
         result = firstNumber + secondNumber;
         firstNumber = secondNumber;
         secondNumber = result;
      }

      return result;
   }

   public static int CalculateNumberSum(int n)
   {
      if (n == 0)
      {
         return 0;
      }

      if (n == 1)
      {
         return 1;
      }

      return ToDigitArray(n).Sum();
   }

   public static QuadraticEquationRoots SolveQuadraticEquation(QuadraticEquation equation)
   {
      double x1 = 0;
      double x2 = 0;

      var d= equation.B * equation.B - 4 * equation.A * equation.C; 

      if (d == 0)
      {
         x1 = -equation.B/(2.0 * equation.A);
         x2 = x1;
      }
      else if (d>0)
      {
         x1 = (-equation.B + Math.Sqrt(d)) / (2 * equation.A);
         x2 = (-equation.B - Math.Sqrt(d))/(2 * equation.A);
      }
      else
      {
         throw new Exception("No Solution.");
      }

      return new QuadraticEquationRoots(x1, x2);
   }

   public static int[,] DoMatrixAddition(int[,] firstMatrix, int[,] secondMatrix)
   {
      var rowNumber = firstMatrix.GetLength(0);
      var columnNumber = firstMatrix.GetLength(1);

      var resultMatrix = new int[rowNumber, columnNumber];

      for (var i = 0; i < rowNumber; i++) {
         for (var j = 0; j < columnNumber; j++) {
            resultMatrix[i, j] = firstMatrix[i, j] + secondMatrix[i, j];
         }
      }

      return resultMatrix;
   }

   public static int[,] DoMatrixMultiplication(int[,] firstMatrix, int[,] secondMatrix)
   {
      var rA = firstMatrix.GetLength(0);
      var cA = firstMatrix.GetLength(1);
      var rB = secondMatrix.GetLength(0);
      var cB = secondMatrix.GetLength(1);

      var temp = 0;
      var resultMatrix = new int[rA, cB];
      if (cA != rB)
      {
         Console.WriteLine("matrix can't be multiplied !!");
         return null;
      }

      for (var i = 0; i < rA; i++)
      {
         for (var j = 0; j < cB; j++)
         {
            temp = 0;
            for (var k = 0; k < cA; k++)
            {
               temp += firstMatrix[i, k] * secondMatrix[k, j];
            }
            resultMatrix[i, j] = temp;
         }
      }

      return resultMatrix;
   }

   private static IEnumerable<int> ToDigitArray(int i)
   {
      var result = new List<int>();
      while (i != 0)
      {
         result.Add(i % 10);
         i /= 10;
      }

      result.Reverse();

      return result.ToArray();
   }

   private static void PrintMatrix(int[,] matrix)
   {
      for (var i = 0; i < matrix.GetLength(0); i++)
      {
         for (var j = 0; j < matrix.GetLength(1); j++)
         {
            Console.Write("{0}\t", matrix[i, j]);
         }

         Console.WriteLine();
      }
   }
}


public class QuadraticEquation
{
   public QuadraticEquation(int a, int b, int c)
   {
      A = a;
      B = b;
      C = c;
   }

   public int A { get; }

   public int B { get; }

   public int C { get; }
}


public class QuadraticEquationRoots
{
   public QuadraticEquationRoots(double x1, double x2)
   {
      X1 = x1;
      X2 = x2;
   }

   public double X1 { get; }

   public double X2 { get; }

   public void PrintRoots()
   {
      Console.WriteLine("x1 = " + X1);
      Console.WriteLine("x2 = " + X2);
   }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS