int[] arr = new int[] {5, 3, 8, 2, 9, 1};int max = arr[0];int min = arr[0];for (int i = 1; i < arr.Length; i++) {if (arr[i] > max) {max = arr[i];}if (arr[i] < min) {min = arr[i];}}Console.WriteLine("Maximum value in the array is: " + max);Console.WriteLine("Minimum value in the array is: " + min);
Write a program to reverse a string without using any built-in functions.
string str = "Hello, World!";char[] chars = str.ToCharArray();int length = chars.Length;for (int i = 0; i < length / 2; i++) {char temp = chars[i];chars[i] = chars[length - 1 - i];chars[length - 1 - i] = temp;}string reversed = new string(chars);
Console.WriteLine(reversed);
Write a program to check whether a given string is a palindrome or not.
string str = "racecar";bool isPalindrome = true;int length = str.Length;for (int i = 0; i < length / 2; i++) {if (str[i] != str[length - 1 - i]) {isPalindrome = false;break;}}if (isPalindrome) {Console.WriteLine(str + " is a palindrome.");} else {Console.WriteLine(str + " is not a palindrome.");}
Write a program to count the number of occurrences of a given character in a string.
string str = "Hello, World!";char c = 'l';int count = 0;for (int i = 0; i < str.Length; i++) {if (str[i] == c) {count++;}}Console.WriteLine("The character " + c + " occurs " + count + " times in the string.");
Write a program to remove duplicates from an array.
int[] arr = new int[] {5, 3, 8, 2, 9, 1, 5, 2, 9};int[] result = new int[arr.Length];int k = 0;for (int i = 0; i < arr.Length; i++) {bool isDuplicate = false;for (int j = 0; j < k; j++) {if (arr[i] == result[j]) {isDuplicate = true;break;}}if (!isDuplicate) {result[k] = arr[i];k++;}}for (int i = 0; i < k; i++) {Console.Write(result[i] + " ");}
Write a program to find the second largest number in an array.
int[] arr = new int[] {5, 3, 8, 2, 9, 1};int max = arr[0];int secondMax = arr[0];for (int i = 1; i < arr.Length; i++) {if (arr[i] > max) {secondMax = max;max = arr[i];} else if (arr[i] > secondMax && arr[i] != max) {secondMax = arr[i];}}Console.WriteLine("The second largest number in the array is: " + secondMax);
Write a program to check whether two given strings are anagrams or not.
string str1 = "listen";string str2 = "silent";bool isAnagram = true;if (str1.Length != str2.Length) {isAnagram = false;} else {int[] counts = new int[26];for (int i = 0; i < str1.Length; i++) {counts[str1[i] - 'a']++;counts[str2[i] - 'a']--;}for (int i = 0; i < counts.Length; i++) {if (counts[i] != 0) {isAnagram = false;break;}}}if (isAnagram) {Console.WriteLine(str1 + " and " + str2 + " are anagrams.");} else {Console.WriteLine(str1 + " and " + str2 + " are not anagrams.");}
Write a program to implement a stack using an array.
class Stack {private int[] arr;private int top;public Stack(int capacity) {arr = new int[capacity];top = -1;}public void Push(int item) {if (top == arr.Length - 1) {Console.WriteLine("Stack overflow.");} else {top++;arr[top] = item;}}public int Pop() {if (top == -1) {Console.WriteLine("Stack underflow.");return -1;} else {int item = arr[top];top--;return item;}}public bool IsEmpty() {return top == -1
Others.....
- Write a program to implement a queue using an array.
- Write a program to implement a linked list in C#.
- Write a program to implement a binary search tree in C#.
- Write a program to sort an array using the bubble sort algorithm.
- Write a program to sort an array using the quicksort algorithm.
- Write a program to find the factorial of a given number.
- Write a program to check whether a given number is prime or not.
- Write a program to find the sum of all the elements in an array.
- Write a program to find the average of all the elements in an array.
- Write a program to implement a binary search algorithm in C#.
- Write a program to implement a linear search algorithm in C#.
- Write a program to implement a recursive function in C#.
0 Comments