Wednesday 22 April 2015

How to find the Greatest and the smallest number in a given Array in c# asp.net ?

Here I am writing a program in c# to find out the Greatest and the samllest number in gievn array.This program will ask to enter the size of Array then will all numbers one by finally it will print the results.


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

namespace ConsoleApp
{
    class Program
    {
        public static void Main()
        {
            int n;
            float largest, smallest;
            int[] a = new int[50];
            Console.WriteLine("Please enter the size of Array");
            string s = Console.ReadLine();
            n = Int32.Parse(s);
            for (int i = 0; i < n; i++)
            {
                Console.WriteLine("Please enter the {0} elements of the Array", + i+1);
                string s1 = Console.ReadLine();
                a[i] = Int32.Parse(s1);
            }
            Console.Write("");
            largest = a[0];
            smallest = a[0];
            for (int i = 1; i < n; i++)
            {
                if (a[i] > largest)
                    largest = a[i];
                else if (a[i] < smallest)
                    smallest = a[i];
            }
            Console.WriteLine("The Largest element in the array is {0}", largest);
            Console.WriteLine("The Smallest element in the array is {0}", smallest);
            Console.ReadKey();
        }
    }

}


Output




How find the HCF of any given two number in c# asp.net ?


Here I written a C# Program to Finds and Display the H.C.F of a Given Number. In other words the H.C.F is the largest of all the common factors.

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

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
          
            int num1,num2,i;
            int hcf =0;
            Console.Write("\n Please Enter the First Number : ");
            num1 = int.Parse(Console.ReadLine());
            Console.Write("\n Please Enter the Second Number : ");
            num2 = int.Parse(Console.ReadLine());
            for(i=1;i<=num1||i<=num2;++i)
            {
                if(num1%i==0 && num2%i==0)
                {
                    hcf=i;
                }
            }    
            Console.Write("\nThe Common Factor (HCF) is : ");
            Console.WriteLine(hcf);
            Console.Read();
        }
        
    }
}

Output :-