0% found this document useful (0 votes)
14 views6 pages

Partt 1 Dinu

The document contains various C# programming examples covering topics such as console applications, array manipulation, sorting, and class definitions. It includes code snippets for combining arrays, removing duplicates, counting elements, generating patterns, testing for prime numbers, and calculating salaries. Additionally, it provides an HTML example for setting a background color on a webpage.

Uploaded by

harsxxuofficial
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

Partt 1 Dinu

The document contains various C# programming examples covering topics such as console applications, array manipulation, sorting, and class definitions. It includes code snippets for combining arrays, removing duplicates, counting elements, generating patterns, testing for prime numbers, and calculating salaries. Additionally, it provides an HTML example for setting a background color on a webpage.

Uploaded by

harsxxuofficial
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Simple Console Application:

using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello, World!");
}
}

2. Differences between Array and ArrayList:

// Arrays are fixed-size and type-safe, while ArrayLists are dynamically sized and
store objects.
// Example:
int[] numbers = new int[5]; // Fixed-size array
ArrayList arrayList = new ArrayList(); // Dynamic size, stores objects

3. Combine Two Arrays Without Duplicates Using Union():

using System;
using System.Linq;
class Program
{
static void Main()
{
int[] array1 = {1, 2, 3, 4};
int[] array2 = {3, 4, 5, 6};
var result = array1.Union(array2);
Console.WriteLine(string.Join(", ", result));
}
}

4. Remove Duplicates from an Array:

using System;
using System.Linq;
class Program
{
static void Main()
{
int[] numbers = {1, 2, 2, 3, 4, 4, 5};
int[] distinctNumbers = numbers.Distinct().ToArray();
Console.WriteLine(string.Join(", ", distinctNumbers));
}
}

5. Count Total Elements in an Array Using Count():


using System;
using System.Linq;
class Program
{
static void Main()
{
int[] numbers = {1, 2, 3, 4, 5};
Console.WriteLine("Total Count: " + numbers.Count());
}
}

6. Get Comma-Separated String from an Array:

using System;
class Program
{
static void Main()
{
string[] words = {"Apple", "Banana", "Cherry"};
string result = string.Join(", ", words);
Console.WriteLine(result);
}
}

7. Sort a One-Dimensional Array Using Sort() and LINQ:

using System;
using System.Linq;
class Program
{
static void Main()
{
int[] numbers = {5, 2, 8, 1};
Array.Sort(numbers);
Console.WriteLine("Sorted using Sort(): " + string.Join(", ", numbers));
var sortedLinq = numbers.OrderBy(n => n);
Console.WriteLine("Sorted using LINQ: " + string.Join(", ", sortedLinq));
}
}

8. Difference Between Array and ArrayList (Table Format):

using System;
using System.Collections;
class Program
{
static void Main()
{
Console.WriteLine("------------------------------------------------");
Console.WriteLine("| Feature | Array | ArrayList
|");
Console.WriteLine("------------------------------------------------");
Console.WriteLine("| Size | Fixed | Dynamic
|");
Console.WriteLine("| Type Safety | Strongly Typed | Stores Objects
|");
Console.WriteLine("| Performance | Faster | Slower
|");
Console.WriteLine("| Methods | Limited | Rich Collection
|");
Console.WriteLine("| Usage | Best for fixed data | Best for dynamic
data |");
Console.WriteLine("------------------------------------------------");
}
}

13. Generate Patterns (Triangle, Diamond):

using System;
class Program
{
static void Main()
{
int n = 5;
for (int i = 1; i <= n; i++)
{
Console.WriteLine(new string(' ', n - i) + new string('*', 2 * i - 1));
}
for (int i = n - 1; i >= 1; i--)
{
Console.WriteLine(new string(' ', n - i) + new string('*', 2 * i - 1));
}
}
}

14. Test for Prime Numbers:

using System;
class Program
{
static void Main()
{
Console.Write("Enter a number: ");
int num = int.Parse(Console.ReadLine());
bool isPrime = num > 1;
for (int i = 2; i < num; i++)
{
if (num % i == 0)
{
isPrime = false;
break;
}
}
Console.WriteLine(isPrime ? "Prime" : "Not Prime");
}
}
15. Function Overloading to Swap Integers and Floats:

using System;
class Program
{
static void Swap(ref int a, ref int b)
{
int temp = a; a = b; b = temp;
}
static void Swap(ref float a, ref float b)
{
float temp = a; a = b; b = temp;
}
static void Main()
{
int x = 5, y = 10;
Swap(ref x, ref y);
Console.WriteLine($"Swapped Integers: {x}, {y}");
float p = 3.5f, q = 7.2f;
Swap(ref p, ref q);
Console.WriteLine($"Swapped Floats: {p}, {q}");
}
}

16. **Class for Staff Data and Displaying HODs:**


```csharp
using System;
class Staff
{
public string Name, Post;
public Staff(string name, string post)
{
Name = name;
Post = post;
}
}
class Program
{
static void Main()
{
Staff[] staffList = new Staff[5]
{
new Staff("Alice", "HOD"),
new Staff("Bob", "Lecturer"),
new Staff("Charlie", "HOD"),
new Staff("David", "Assistant"),
new Staff("Eve", "HOD")
};
Console.WriteLine("HODs:");
foreach (var staff in staffList)
{
if (staff.Post == "HOD")
Console.WriteLine(staff.Name);
}
}
}
```

17. **Class with Default DA & HRA Values and Salary Calculation:**
```csharp
using System;
class Employee
{
public double Basic, DA = 0.1, HRA = 0.2;
public Employee(double basic)
{
Basic = basic;
}
public double CalculateSalary()
{
return Basic + (Basic * DA) + (Basic * HRA);
}
}
class Program
{
static void Main()
{
Employee emp = new Employee(50000);
Console.WriteLine("Total Salary: " + emp.CalculateSalary());
}
}
```

18. **Compute Calories for Fat, Carbohydrate, and Protein:**


```csharp
using System;
class Program
{
static void Main()
{
Console.Write("Enter grams of fat: ");
int fat = int.Parse(Console.ReadLine());
Console.Write("Enter grams of carbohydrate: ");
int carb = int.Parse(Console.ReadLine());
Console.Write("Enter grams of protein: ");
int protein = int.Parse(Console.ReadLine());
int totalCalories = (fat * 9) + (carb * 4) + (protein * 4);
Console.WriteLine("Total Calories: " + totalCalories);
}
}
```

19. **Apply Same Background Color for Webpages:**


```html
<!DOCTYPE html>
<html>
<head>
<style>
body { background-color: lightblue; }
</style>
</head>
<body>
<h1>Welcome to BMS, BAF, and UG Students Page</h1>
</body>
</html>
```

You might also like