[go: up one dir, main page]

0% found this document useful (0 votes)
35 views12 pages

Documentdotnet

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)
35 views12 pages

Documentdotnet

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/ 12

Dot Net Lab

Ex.No.1

I. Write a program to C# to find the smallest single digit factor for a given
value.

using System;

class SmallestFactor

static void Main(string[] args)

Console.Write("Enter a value: ");

int value = Convert.ToInt32(Console.ReadLine());

int smallestFactor = FindSmallestFactor(value);

Console.WriteLine("Smallest single digit factor: " + smallestFactor);

static int FindSmallestFactor(int value)

for (int i = 2; i <= 9; i++)

if (value % i == 0)

return i;

}
}

return -1; // return -1 if no single digit factor found

Ex.No.2

Write a program in C# to print a number if it is prime; otherwise display


the largest factor of that number.

using System;

namespace PrimeOrLargestFactor

class Program

static void Main(string[] args)


{

Console.Write("Enter a number: ");

int number = Convert.ToInt32(Console.ReadLine());

if (IsPrime(number))

Console.WriteLine(number + " is a prime number");

else

int largestFactor = FindLargestFactor(number);

Console.WriteLine("Largest factor of " + number + ": " +


largestFactor);

static bool IsPrime(int number)

if (number <= 1) return false;

for (int i = 2; i * i <= number; i++)

if (number % i == 0) return false;

return true;

}
static int FindLargestFactor(int number)

for (int i = number / 2; i >= 2; i--)

if (number % i == 0) return i;

return 1; // return 1 if no factor found

Ex.No.3

Write a program in C# to find the magnitude of a number.


using System;

namespace MagnitudeCalculator

public class Program

public static double CalculateMagnitude(double number)

return Math.Abs(number);

public static void Main(string[] args)

Console.WriteLine("Enter a number:");

if (double.TryParse(Console.ReadLine(), out double inputNumber))

double magnitude = CalculateMagnitude(inputNumber);

Console.WriteLine($"Magnitude of {inputNumber} is
{magnitude}");

else

Console.WriteLine("Invalid input. Please enter a valid number.");

}
}

Ex.No.4

Write a C# program for addition and multiplication of two matrices

using System;

class MatrixOperations

static void Main()

int[,] matrix1 = {

{1, 2, 3},

{4, 5, 6},

{7, 8, 9}

};
int[,] matrix2 = {

{9, 8, 7},

{6, 5, 4},

{3, 2, 1}

};

int[,] resultAddition = AddMatrices(matrix1, matrix2);

int[,] resultMultiplication = MultiplyMatrices(matrix1, matrix2);

Console.WriteLine("Matrix Addition:");

PrintMatrix(resultAddition);

Console.WriteLine("\nMatrix Multiplication:");

PrintMatrix(resultMultiplication);

static int[,] AddMatrices(int[,] matrix1, int[,] matrix2)

int rows = matrix1.GetLength(0);

int cols = matrix1.GetLength(1);

int[,] result = new int[rows, cols];

for (int i = 0; i < rows; i++)

{
for (int j = 0; j < cols; j++)

result[i, j] = matrix1[i, j] + matrix2[i, j];

return result;

static int[,] MultiplyMatrices(int[,] matrix1, int[,] matrix2)

int rows1 = matrix1.GetLength(0);

int cols1 = matrix1.GetLength(1);

int cols2 = matrix2.GetLength(1);

int[,] result = new int[rows1, cols2];

for (int i = 0; i < rows1; i++)

for (int j = 0; j < cols2; j++)

for (int k = 0; k < cols1; k++)

result[i, j] += matrix1[i, k] * matrix2[k, j];

}
}

return result;

static void PrintMatrix(int[,] matrix)

int rows = matrix.GetLength(0);

int cols = matrix.GetLength(1);

for (int i = 0; i < rows; i++)

for (int j = 0; j < cols; j++)

Console.Write(matrix[i, j] + " ");

Console.WriteLine();

}
Ex.No.5

Write a C# program to display the digits of an integer in words.

using System;

namespace Application

class DigitToWords

static void Main()

Console.Write("Enter an integer: ");

int number = int.Parse(Console.ReadLine());

Console.WriteLine("Digits in words:");

DisplayDigitsInWords(number);

static void DisplayDigitsInWords(int number)

if (number == 0)

Console.WriteLine("Zero");

return;

}
// Convert negative numbers to positive

if (number < 0)

Console.Write("Negative ");

number = Math.Abs(number);

// Convert the number to an array of digits

int[] digits = GetDigits(number);

// Display each digit in words

for (int i = 0; i < digits.Length; i++)

Console.WriteLine($"{digits[i]}: {DigitToWord(digits[i])}");

static int[] GetDigits(int number)

int numDigits = (int)Math.Floor(Math.Log10(number)) + 1;

int[] digits = new int[numDigits];

for (int i = numDigits - 1; i >= 0; i--)

digits[i] = number % 10;

number /= 10;
}

return digits;

static string DigitToWord(int digit)

switch (digit)

case 0: return "Zero";

case 1: return "One";

case 2: return "Two";

case 3: return "Three";

case 4: return "Four";

case 5: return "Five";

case 6: return "Six";

case 7: return "Seven";

case 8: return "Eight";

case 9: return "Nine";

default: return "";

You might also like