6 Code
6 Code
1. Swapping Program in C#
Swapping two numbers involves exchanging their values. You can do this using a temporary variable or without a
temporary variable.
Example (with a temporary variable):
using System; csharp
class Program
{
static void Main()
{
int a = 5, b = 10;
Console.WriteLine($"Before Swap: a = {a}, b = {b}");
class Program
{
static void Main()
{
int a = 5, b = 10;
Console.WriteLine($"Before Swap: a = {a}, b = {b}");
class Program
{
static void Main()
{
int n = 10;
int first = 0, second = 1;
Console.WriteLine("Fibonacci Series:");
3. Prime Numbers in C#
A prime number is a number greater than 1 that is divisible only by 1 and itself.
Example:
using System; csharp
class Program
{
static void Main()
{
int num = 29;
bool isPrime = true;
4. Palindrome Program in C#
A palindrome is a word or number that reads the same forward and backward.
Example:
using System; csharp
class Program
{
static void Main()
{
string str = "madam";
string reversed = new string(str.ToCharArray().Reverse().ToArray());
if (str == reversed)
Console.WriteLine($"{str} is a palindrome.");
else
Console.WriteLine($"{str} is not a palindrome.");
}
}
class Program
{
static void Main()
{
int num = 12345;
int reversed = 0;
class Program
{
static void Main()
{
int num = 153;
int sum = 0;
int temp = num;
int digits = num.ToString().Length;
while (temp != 0)
{
int digit = temp % 10;
sum += (int)Math.Pow(digit, digits);
temp /= 10;
}
if (sum == num)
Console.WriteLine($"{num} is an Armstrong number.");
else
Console.WriteLine($"{num} is not an Armstrong number.");
}
}
class Program
{
static void Main()
{
int num = 5;
int factorial = 1;
class Program
{
static void Main()
{
int num = 12345;
int sum = 0;
while (num != 0)
{
sum += num % 10;
num /= 10;
}
class Program
{
static void Main()
{
int num = 13;
string binary = Convert.ToString(num, 2);
class Program
{
static void Main()
{
string binary = "1101";
int decimalValue = Convert.ToInt32(binary, 2);
class Program
{
static void Main()
{
string str = "programming";
char ch = 'g';
int count = 0;
class Program
{
static void Main()
{
string str = "hello";
string reversed = new string(str.ToCharArray().Reverse().ToArray());
class Program
{
static void Main()
{
string sentence = "hello world";
string[] words = sentence.Split(' ');
class Program
{
static void Main()
{
string str = "programming";
string result = string.Concat(str.Distinct());
class Program
{
static void Main()
{
string str = "abc";
for (int i = 0; i < str.Length; i++)
{
for (int j = i + 1; j <= str.Length; j++)
{
Console.WriteLine(str.Substring(i, j - i));
}
}
}
}
class Program
{
static void Main()
{
int[,] twoDArray = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
int rows = twoDArray.GetLength(0);
int cols = twoDArray.GetLength(1);
int[] oneD
---
#### **Example**:
```csharp
using System;
class Program
{
static void Main()
{
int[] oneDArray = { 1, 2, 3, 4, 5, 6 };
int rows = 2, cols = 3;
int[,] twoDArray = new int[rows, cols];
int index = 0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
twoDArray[i, j] = oneDArray[index++];
}
}
Console.WriteLine("Two-Dimensional Array:");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
Console.Write(twoDArray[i, j] + " ");
}
Console.WriteLine();
}
}
}
class Program
{
static void Main()
{
int[] arr = { 1, 2, 3, 4, 5 };
int n = arr.Length;
int temp = arr[0];
arr[n - 1] = temp;
class Program
{
static void Main()
{
int[] arr = { 1, 2, 3, 4, 5 };
int n = arr.Length;
int temp = arr[n - 1];
arr[0] = temp;
Console.WriteLine("Array after right circular rotation:");
foreach (var item in arr)
{
Console.Write(item + " ");
}
}
}
For brevity, the other programs (like angle calculation, removing duplicate elements, and checking various types of
numbers) follow a similar pattern where you check conditions or apply mathematical formulas, similar to the examples
above.
Let me know if you'd like the details for the rest of the programs!
Certainly! Below is the continuation of the programs you requested, with detailed explanations and code examples:
20. How to Find the Angle Between Hour and Minute Hands of a Clock at Any Given Time in
C#
To find the angle between the hour and minute hands, we can use a formula based on the positions of the hour and
minute hands on a clock.
Formula:
Minute hand angle =
Error
Hour hand angle =
Error
Angle between hour and minute hands =
Error
If the angle is greater than 180°, subtract it from 360°.
Example:
using System; csharp
class Program
{
static void Main()
{
int hour = 3, minute = 15;
// Calculate the angle of the minute hand
double minuteAngle = 6 * minute;
// Get the smallest angle (if angle > 180, subtract from 360)
if (angle > 180)
angle = 360 - angle;
Console.WriteLine($"The angle between the hour and minute hands is: {angle} deg
rees");
}
}
class Program
{
static void Main()
{
int[] arr = { 1, 2, 2, 3, 4, 4, 5 };
class Program
{
static void Main()
{
int num = 1023;
class Program
{
static void Main()
{
int num = 135;
int sum = 0;
int length = num.ToString().Length;
if (sum == num)
Console.WriteLine($"{num} is a Disarium Number.");
else
Console.WriteLine($"{num} is not a Disarium Number.");
}
}
class Program
{
static bool IsPrime(int num)
{
if (num <= 1)
return false;
for (int i = 2; i <= Math.Sqrt(num); i++)
{
if (num % i == 0)
return false;
}
return true;
}
if (num % 7 == 0 || num.ToString().EndsWith('7'))
Console.WriteLine($"{num} is a Buzz Number.");
else
Console.WriteLine($"{num} is not a Buzz Number.");
}
}
class Program
{
static int Factorial(int num)
{
int fact = 1;
for (int i = 1; i <= num; i++)
{
fact *= i;
}
return fact;
}
if (sum == num)
Console.WriteLine($"{num} is a Strong Number.");
else
Console.WriteLine($"{num} is not a Strong Number.");
}
}
class Program
{
static void Main()
{
int num = 45;
int square = num * num;
string squareStr = square.ToString();
int len = squareStr.Length;
class Program
{
static int Factorial(int num)
{
int fact = 1;
for (int i = 1; i <= num; i++)
{
fact *= i;
}
return fact;
}
if (sum == num)
Console.WriteLine($"{num} is a Krishnamurthy Number.");
else
Console.WriteLine($"{num} is not a Krishnamurthy Number.");
}
}
class Program
{
static bool IsVampireNumber(int num)
{
string numStr = num.ToString();
int len = numStr.Length;
if (IsV
---
A Happy Number is a number that, when replaced by the sum of the squares of its digits
repeatedly, results in 1.
#### **Example**:
```csharp
using System;
class Program
{
static int GetSumOfSquares(int num)
{
int sum = 0;
while (num > 0)
{
int digit = num % 10;
sum += digit * digit;
num /= 10;
}
return sum;
}
do
{
slow = GetSumOfSquares(slow); // move slow by one step
fast = GetSumOfSquares(GetSumOfSquares(fast)); // move fast by two steps
} while (slow != fast);
if (slow == 1)
Console.WriteLine($"{num} is a Happy Number.");
else
Console.WriteLine($"{num} is not a Happy Number.");
}
}
class Program
{
static void Main()
{
int num = 25;
int square = num * num;
if (square.ToString().EndsWith(num.ToString()))
Console.WriteLine($"{num} is an Automorphic Number.");
else
Console.WriteLine($"{num} is not an Automorphic Number.");
}
}
This concludes the detailed explanation for all the listed programs, continuing the numbering as you requested.
Certainly! Below are detailed explanations and code examples for the requested topics:
class Program
{
static void Main()
{
string binary = "101101"; // Binary number
int decimalNumber = Convert.ToInt32(binary, 2); // Convert binary to decimal
class Program
{
static string DecimalToOctal(int num)
{
string result = "";
while (num > 0)
{
result = (num % 8) + result;
num /= 8;
}
return result;
}
class Program
{
static void Main()
{
string hex = "1A"; // Hexadecimal number
int decimalNumber = Convert.ToInt32(hex, 16); // Convert hexadecimal to decimal
class Program
{
static void Main()
{
string octal = "17"; // Octal number
int decimalNumber = Convert.ToInt32(octal, 8); // Convert octal to decimal
class Program
{
static void Main()
{
string octal = "17"; // Octal number
int decimalNumber = Convert.ToInt32(octal, 8); // Convert octal to decimal
Console.WriteLine($"Octal {octal} is Decimal {decimalNumber}");
}
}
Summary of Conversions:
Conversion Steps Example Example
Input Output
Binary to Octal 1. Convert binary to decimal. 2. Convert decimal to "101101" "55"
octal.
Decimal to Octal Repeatedly divide the decimal number by 8 and 123 "173"
store the remainders.
Hexadecimal to 1. Convert hexadecimal to decimal. 2. Convert "1A" "32"
Octal decimal to octal.
Octal to Binary 1. Convert octal to decimal. 2. Convert decimal to "17" "11111"
binary.
Octal to Decimal Treat each digit of the octal as a power of 8 and "17" "15"
sum the results.
These examples demonstrate how to perform the different base conversions in C# efficiently using built-in methods or
custom logic.
Certainly! Below are C# programs for the requested star patterns and number patterns, along with the output.
class Program
{
static void Main()
{
int n = 5; // Number of rows
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
}
Output:
*
**
***
****
*****
class Program
{
static void Main()
{
int n = 5; // Number of rows
for (int i = 1; i <= n; i++)
{
for (int j = i; j < n; j++)
{
Console.Write(" ");
}
for (int j = 1; j <= i; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
}
Output:
*
**
***
****
*****
class Program
{
static void Main()
{
int n = 5; // Number of rows
for (int i = n; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
}
Output:
*****
****
***
**
*
class Program
{
static void Main()
{
int n = 5; // Number of rows
for (int i = n; i >= 1; i--)
{
for (int j = 1; j <= n - i; j++)
{
Console.Write(" ");
}
for (int j = 1; j <= i; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
}
Output:
*****
****
***
**
*
class Program
{
static void Main()
{
int n = 5; // Number of rows
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
// Print '*' on the first or last row, or at the start/end of each row
if (j == 1 || j == i || i == n)
Console.Write("*");
else
Console.Write(" ");
}
Console.WriteLine();
}
}
}
Output:
*
**
* *
* *
*****
class Program
{
static void Main()
{
int n = 5; // Number of rows
for (int i = n; i >= 1; i--)
{
for (int j = 1; j <= n - i; j++)
{
Console.Write(" ");
}
for (int j = 1; j <= (2 * i - 1); j++)
{
Console.Write(i);
}
Console.WriteLine();
}
}
}
Output:
55555
4444
333
22
1
class Program
{
static void Main()
{
int n = 5; // Number of rows
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
Console.Write(j);
}
Console.WriteLine();
}
}
}
Output:
1
12
123
1234
12345
Summary of Patterns:
Pattern Description Example Output
Right Triangle Star Pattern Prints a right-angle triangle with stars increasing by *********
row.
Mirrored Right Angle Triangle Right-angled triangle with stars aligned to the right. *********
Downward Triangle Star Prints a downward triangle, starting with the largest *****
Pattern row.
Downward Mirrored Right Downward triangle with stars aligned to the right. **** *
Triangle
Hollow Right Triangle Star Right triangle with hollow spaces except for the **
Pattern edges.
Inverted Pyramid of Numbers Inverted pyramid of numbers, starting with the 55555 4444 333 22
largest number. 1
Half Pyramid of Numbers Half-pyramid of numbers from 1 up to the row 112123123412345
number.
These patterns help practice loops and logic control in C#, and you can easily adjust the number of rows ( n ) to get
different sizes of patterns.
Below are C# programs for the requested sorting algorithms and array manipulation operations, along with their
expected output.
class Program
{
static void BubbleSort(int[] arr)
{
int n = arr.Length;
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
// Swap
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
Console.WriteLine("Sorted array:");
foreach (int num in arr)
{
Console.Write(num + " ");
}
}
}
Output:
Sorted array:
1 2 4 5 8
class Program
{
static void MergeSort(int[] arr, int left, int right)
{
if (left < right)
{
int mid = left + (right - left) / 2;
MergeSort(arr, left, mid);
MergeSort(arr, mid + 1, right);
Merge(arr, left, mid, right);
}
}
static void Merge(int[] arr, int left, int mid, int right)
{
int n1 = mid - left + 1;
int n2 = right - mid;
int[] leftArray = new int[n1];
int[] rightArray = new int[n2];
int i = 0, j = 0, k = left;
while (i < n1 && j < n2)
{
if (leftArray[i] <= rightArray[j])
{
arr[k] = leftArray[i];
i++;
}
else
{
arr[k] = rightArray[j];
j++;
}
k++;
}
Console.WriteLine("Sorted array:");
foreach (int num in arr)
{
Console.Write(num + " ");
}
}
}
Output:
Sorted array:
3 9 10 27 38 43 82
class Program
{
static void InsertionSort(int[] arr)
{
for (int i = 1; i < arr.Length; i++)
{
int key = arr[i];
int j = i - 1;
Console.WriteLine("Sorted array:");
foreach (int num in arr)
{
Console.Write(num + " ");
}
}
}
Output:
Sorted array:
5 6 11 12 13
class Program
{
static void LeftRotateByOne(int[] arr)
{
int temp = arr[0];
for (int i = 0; i < arr.Length - 1; i++)
{
arr[i] = arr[i + 1];
}
arr[arr.Length - 1] = temp;
}
Output:
Array after left rotation by 1:
2 3 4 5 1
class Program
{
static void RightRotateByOne(int[] arr)
{
int temp = arr[arr.Length - 1];
for (int i = arr.Length - 1; i > 0; i--)
{
arr[i] = arr[i - 1];
}
arr[0] = temp;
}
static void Main()
{
int[] arr = { 1, 2, 3, 4, 5 };
RightRotateByOne(arr);
Output:
Array after right rotation by 1:
5 1 2 3 4
class Program
{
static void RotateLeftByK(int[] arr, int k)
{
int n = arr.Length;
k = k % n;
int[] temp = new int[k];
Output:
Array after left rotation by 2 positions:
3 4 5 1 2
class Program
{
static void Reverse(int[] arr, int start, int end)
{
while (start < end)
{
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
Output:
Array after 2 rotations:
3 4 5 1 2
class Program
{ static void ReverseArray(int[] arr) { int start = 0, end = arr.Length - 1; while (start < end) { int temp = arr[start];
arr[start] = arr[end]; arr[end] = temp; start++; end--; } }
static void Main()
{
int[] arr = { 1, 2, 3, 4, 5 };
ReverseArray(arr);
Console.WriteLine("Reversed array:");
foreach (int num in arr)
{
Console.Write(num + " ");
}
}
}
#### **Output**:
Reversed array: 5 4 3 2 1
---
| **Program** | **Description**
| **Example Output** |
|-------------------------------------------|------------------------------------------
------------------------------|------------------------|
| **Bubble Sort** | Sorts the array by repeatedly swapping ad
jacent elements. | 1 2 4 5 8 |
| **Merge Sort** | Divides and merges the array in sorted or
der. | 3 9 10 27 38 43 82 |
| **Insertion Sort** | Sorts the array by inserting elements in
their correct position. | 5 6 11 12 13 |
| **Left Rotation by 1** | Rotates the array left by one position.
| 2 3 4 5 1 |
| **Right Rotation by 1** | Rotates the array right by one position.
| 5 1 2 3 4 |
| **Rotate by K Position (Unshifting)** | Rotates the array to the left by K positio
ns using unshifting. | 3 4 5 1 2 |
| **Rotate by K Position (Reversal)** | Rotates the array by K positions using th
e reversal algorithm. | 3 4 5 1 2 |
| **Reverse an Array** | Reverses the entire array.
| 5 4 3 2 1 |
These programs cover essential sorting and array manipulation techniques in C# and can
be extended to larger datasets as needed.