Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Finding Largest and Smallest Number in C#

  1. using System;
  2.  
  3. namespace forgetCode
  4. {
  5. class Program
  6. {
  7. public static void Main()
  8. {
  9. int n;
  10. float large, small;
  11. int[] a = new int[50];
  12. Console.WriteLine("Enter the size of Array");
  13. string s = Console.ReadLine();
  14. n = Int32.Parse(s);
  15. Console.WriteLine("Enter the array elements");
  16. for (int i = 0; i < n; i++)
  17. {
  18. string s1 = Console.ReadLine();
  19. a[i] = Int32.Parse(s1);
  20. }
  21. Console.Write("");
  22. large = a[0];
  23. small = a[0];
  24. for (int i = 1; i < n; i++)
  25. {
  26. if (a[i] > large)
  27. large = a[i];
  28. else if (a[i] < small)
  29. small = a[i];
  30. }
  31. Console.WriteLine("Largest element in the array is {0}", large);
  32. Console.WriteLine("Smallest element in the array is {0}", small);
  33. }
  34. }
  35.  
  36. }


Output:
Enter the size of Array
6
Enter the array elements
23
11
98
65
54
43
Largest element in the array is 98
Smallest element in the array is 11


..