find largest and 2nd largest using index in c#

SOURCE CODE :

//programed by Paras Wadher
MCA
Nagpur university


// Write a Program in c# to find largest and second largest using index .

using System;
using System.Collections.Generic;
using System.Text;

namespace Index_largest
{
class Number
{
private int[] data = new int[5];
public int this[int index]
{

get
{

return data[index];
}
set
{
data[index] = value;
}
}
}
class largest
{
static void Main(string[] args)
{
int []a=new int [10];
int i,j,k,p=0,l=0,m,w;
Console.WriteLine(“Enter the size of the array:-“);
k=Convert.ToInt32(Console.ReadLine());
Console.WriteLine(“Enter the elements :-“);
for(i=1;i<=k;i++)
{
Console.Write(i+” =”);
a[i]=Convert.ToInt32(Console.ReadLine());
}
j=a[0];
for(i=1;i<=k;i++)
{
if(a[i]>j)
{
j=a[i];
p = i;
}
}
w = a[p];
a[p] = 0;
m = a[0];
for (i = 1; i <= k; i++)
{
if (a[i] > m )//&& a[i] !=w)
{
l = a[i];
m = a[i];

}
}
Number obj = new Number();
obj[0]=j;
obj[1]=l;
Console.WriteLine(“Largest is:- “+ j);
Console.WriteLine(“Second Largest no .is:- ” + l);

}
}
}

/* OUTPUT
Enter the size of the array:-
8
Enter the elements :-
1 =44
2 =77
3 =99
4 =66
5 =33
6 =22
7 =21
8 =57

Largest is:- 99
Second Largest no .is:- 77
Press any key to continue . . .

*/

Leave a Reply

Your email address will not be published. Required fields are marked *