0% found this document useful (0 votes)
46 views3 pages

C# Array Manipulation Techniques

The document contains multiple C# programs demonstrating different algorithms, including finding the second largest number in an array, identifying duplicate elements, counting occurrences of elements, generating the Fibonacci series recursively, and reversing a string. Each program is structured with a main method and utilizes loops and conditionals to achieve its functionality. The examples illustrate basic programming concepts and data structures in C#.

Uploaded by

msuryaka2025
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)
46 views3 pages

C# Array Manipulation Techniques

The document contains multiple C# programs demonstrating different algorithms, including finding the second largest number in an array, identifying duplicate elements, counting occurrences of elements, generating the Fibonacci series recursively, and reversing a string. Each program is structured with a main method and utilizes loops and conditionals to achieve its functionality. The examples illustrate basic programming concepts and data structures in C#.

Uploaded by

msuryaka2025
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

Finding the Second Largest Number in an Array (Custom Logic) :-

-------------------------------------------------------------
using System;
class Program
{
static void Main()
{
int[] Data = { 10, 90, 80, 60, 40, 30, 100 };

// Initialize two variables to track the largest and second largest numbers

int largest = [Link];


int secondLargest = [Link];

// Iterate through each number in the array


foreach (int number in Data)
{
// If current number is greater than the largest
if (number > largest)
{
// Update secondLargest to be the previous largest
secondLargest = largest;
// Update largest to be the current number
largest = number;
}
// Else if current number is greater than secondLargest but not equal
to largest
else if (number > secondLargest && number != largest)
{
secondLargest = number;
}
}
[Link]("The second largest number is: " + secondLargest);
}
}
-----------------------------------------------------------------------------------
--------------------------

Finding Duplicate Elements in an Array (C#)


-----------------------------------------------------------------------------------
--------------------------
using System;

class Program
{
static void Main()
{
int[] arr = { 1, 6, 7, 6, 2, 1, 3 };

[Link]("Duplicate elements in the array:");

// Iterate through each element in the array

for (int i = 0; i < [Link]; i++)


{
// Compare with all subsequent elements

for (int j = i + 1; j < [Link]; j++)


{
if (arr[i] == arr[j])
{
[Link](arr[i]);
break; // Once a duplicate is found, move to next element
}
}
}
}
}*************************************************** how many times it appears
static void Main()
{
int[] arr = { 1, 6, 7, 6, 2, 1, 3, 1 };

[Link]("Number of occurrences of each element:");


Dictionary<int,int > numbers=new Dictionary<int, int>();

foreach (int i in arr)


{
if ([Link](i))
{
numbers[i]++;
}
else
{
numbers[i]=1;
}
}
foreach(var pair in numbers)
{
if ([Link] > 1)
{
[Link]($"{[Link]}appears in {[Link]}Times");
}
}
}
-----------------------------------------------------------------------------------
-----------------
Fibonacci Series?
The Fibonacci series is a sequence where each number is the sum of the two
preceding ones, starting from 0 and 1. The sequence goes: 0, 1, 1, 2, 3, 5, 8, 13,
21, etc.

-------------------------------------------------------------------------
using System;

class Program
{
static int FibonacciRecursive(int n)
{
if (n <= 1)
return n;
return FibonacciRecursive(n - 1) + FibonacciRecursive(n - 2);
}

static void Main()


{
[Link]("Enter the number of terms: ");
int terms = [Link]([Link]());
[Link]("Fibonacci Series (Recursive):");
for (int i = 0; i < terms; i++)
{
[Link](FibonacciRecursive(i) + " ");
}
}
}
-----------------------------------------------------------------------------------
--------------------------------------------------------
String Reverse
----------------------------------

using System;

class Program
{
static void Main(string[] args)
{
[Link]("Enter a String : ");
string input = [Link]();
////////////////////////////////////////////////////
//char[] chars= [Link]();
//[Link](chars);
//string reversed=new string(chars);

//////////////////////////////////////////////////////
string reversed = new string([Link]().ToArray());

[Link]("reversed Chars is : " + reversed);

}
}

You might also like