Dot Net Imp
Dot Net Imp
Q3. Programs.
1. Write a program to show a list of doctors visiting “Sahyadri
Multispecialty Hospital.”
Ans:-
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<string> doctors = new List<string>
{
"Dr. John Smith - Cardiologist",
"Dr. Emily Johnson - Neurologist",
"Dr. Michael Brown - Orthopedic Surgeon",
"Dr. Sarah Davis - Pediatrician"
};
class Program
{
static void Main()
{
Console.WriteLine("Prime numbers between 2 and 20:");
for (int num = 2; num <= 20; num++)
{
bool isPrime = true;
for (int i = 2; i <= Math.Sqrt(num); i++)
{
if (num % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
Console.WriteLine(num);
}
}
}
}
3. Write a program in C# for multiplication of two numbers.
Ans:-
using System;
class Program
{
static void Main()
{
Console.Write("Enter first number: ");
int num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter second number: ");
int num2 = Convert.ToInt32(Console.ReadLine());
int product = num1 * num2;
Console.WriteLine($"The product of {num1} and {num2} is {product}.");
}
}
4. Write a program in C# to calculate the area of a square.
Ans:-
using System;
class Program
{
static void Main()
{
Console.Write("Enter the side length of the square: ");
double side = Convert.ToDouble(Console.ReadLine());
double area = side * side;
Console.WriteLine($"The area of the square is {area}.");
}
}
5. Write a VB.NET program to check if a given number is a palindrome or
not.
Ans:-
Module Module1
Sub Main()
Console.Write("Enter a number: ")
Dim number As String = Console.ReadLine()
Dim reversed As String = StrReverse(number)
class Program
{
static void Main()
{
Console.Write("Enter first number: ");
int num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter second number: ");
int num2 = Convert.ToInt32(Console.ReadLine());
// Swapping
int temp = num1;
num1 = num2;
num2 = temp;
class Program
{
static void Main()
{
int[,] matrixA = { { 1, 2 }, { 3, 4 } };
int[,] matrixB = { { 5, 6 }, { 7, 8 } };
int[,] result = new int[2, 2];
For i As Integer = 1 To 10
result &= number & " x " & i & " = " & (number * i) & vbCrLf
Next
class Program
{
static void Main()
{
Console.Write("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());
int reversed = 0;
class Program
{
static void Main()
{
Console.Write("Enter the radius of the circle: ");
double radius = Convert.ToDouble(Console.ReadLine());
double area = Math.PI * radius * radius;
Console.WriteLine($"The area of the circle is {area}.");
}
}
17. Write a VB.NET program to accept a character from the user and check
whether it is a vowel or not.
Ans:-
Module Module1
Sub Main()
Console.Write("Enter a character: ")
Dim ch As Char = Console.ReadLine()(0)
If "aeiouAEIOU".Contains(ch) Then
Console.WriteLine(ch & " is a vowel.")
Else
Console.WriteLine(ch & " is not a vowel.")
End If
End Sub
End Module
18. Write a program in C# to create a function to find the factorial of a
given number.
Ans:-
using System;
class Program
{
static void Main()
{
Console.Write("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());
long factorial = Factorial(number);
Console.WriteLine($"The factorial of {number} is {factorial}.");
}
class Program
{
static void Main()
{
Console.Write("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());
int sum = 0, temp = number, digits = number.ToString().Length;
if (sum == number)
{
Console.WriteLine($"{number} is an Armstrong number.");
}
else
{
Console.WriteLine($"{number} is not an Armstrong number.");
}
}
}
21. Write a VB.NET program to display today’s date on the screen.
Ans:-
Module Module1
Sub Main()
Console.WriteLine("Today's date: " & DateTime.Now.ToString("d"))
End Sub
End Module
2. JIT Compilers
Just-In-Time (JIT) compilers are part of the Common Language Runtime (CLR)
in the .NET framework that convert Intermediate Language (IL) code into
native machine code at runtime. This process allows for optimizations
specific to the current execution environment, improving performance. JIT
compilation occurs the first time a method is called, and the compiled code is
cached for subsequent calls, enhancing execution speed.
3. Method Overloading
Method overloading is a feature in C# that allows multiple methods in the
same class to have the same name but different parameters (different type,
number, or order of parameters). This enables developers to create methods
that perform similar functions but with different input types or quantities,
enhancing code readability and usability.
Example:
public class MathOperations
{
public int Add(int a, int b) { return a + b; }
public double Add(double a, double b) { return a + b; }
public int Add(int a, int b, int c) { return a + b + c; }
}
4. Object-Oriented Concept in C#
C# is an object-oriented programming language that supports the four main
principles of OOP:
1. Encapsulation: Bundling data (attributes) and methods (functions) that
operate on the data into a single unit called a class. Access to the data
is restricted through access modifiers (public, private, protected).
2. Inheritance: A mechanism that allows one class (derived class) to
inherit properties and methods from another class (base class),
promoting code reusability.
3. Polymorphism: The ability to present the same interface for different
underlying data types. It allows methods to be defined in multiple
forms, typically through method overriding and overloading.
4. Abstraction: Hiding complex implementation details and showing only
the essential features of an object. This is achieved through abstract
classes and interfaces.
7. Inheritance
Inheritance is a fundamental object-oriented programming concept that
allows a class (derived class) to inherit properties and methods from another
class (base class). This promotes code reusability and establishes a
hierarchical relationship between classes. In C#, inheritance is implemented
using the : symbol.
Example:
public class Animal // Base class
{
public void Eat()
{
Console.WriteLine("Eating...");
}
}
// Usage
Dog myDog = new Dog();
myDog.Eat(); // Inherited method
myDog.Bark(); // Own method
~SampleClass() // Destructor
{
Console.WriteLine("Destructor called");
}
}