Write C# program to count even and odd elements in an array


Introduction

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability.


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. using System;
  2. class Program
  3. {
  4. static void Main()
  5. {
  6. int[] arr = new int[100];
  7. int i, num, evennum, oddnum;
  8.  
  9. ////Reads size and elements in array
  10. Console.WriteLine("Enter size of the array: ");
  11. num = Convert.ToInt32(Console.ReadLine());
  12. Console.WriteLine("Enter "+num+" elements in the array: ");
  13.  
  14. for(i=0; i<num; i++)
  15. {
  16. arr[i] = Convert.ToInt32(Console.ReadLine());
  17. }
  18.  
  19. evennum = 0; // Assuming 0 even numbers
  20. oddnum = 0; // Assuming 0 odd numbers
  21.  
  22. for (i = 0; i < num; i++)
  23. {
  24. /* If the current element of array is evennumber then increment evennumber count */
  25. if (arr[i] % 2 == 0)
  26. {
  27. evennum++;
  28. }
  29. else
  30. {
  31. oddnum++; // increment oddnumber count
  32. }
  33. }
  34.  
  35. Console.WriteLine("Total even numbers: "+evennum);
  36. Console.WriteLine("Total odd numbers: " + oddnum);
  37.  
  38.  
  39. Console.ReadLine();
  40. }
  41.  
  42. }

Result

C# language techstudy

     





Related Topics




Popular Posts