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);
}
}