Thursday, October 20, 2011

mencari 2nd biggest dalam c#.

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Masukkan jumlah data : ");
            int n = int.Parse(Console.ReadLine());
            int[] a = new int[100];
            Random r = new Random();

            for (int i=1; i<=n; i++)
            {
                a[i] = r.Next(100);
                Console.Write("{0}", a[i]);
                Console.Write(" ");
            }

            int bigest = a[1];
            for (int i = 2; i <= n; i++)
            {
                if (a[i] >= bigest)
                    bigest = a[i];
            }

            int x=a[1];
            for (int i=2; i<=n; i++)
            {
                if (a[i]>=x)
                {
                    if (a[i]<bigest)
                    {
                        x=a[i];
                    }
                }
            }
            Console.WriteLine(" ");
            Console.WriteLine(" 2nd bigest : {0}", x);


        }
    }
}

No comments:

Post a Comment