Set an array filled with random positive three-digit numbers. Write a program that will…
- User
Set an array filled with random positive three-digit numbers. Write a program that will show the number of even numbers in the array. [345, 897, 568, 234] -> 2
- Assistant
Here's an example code in Python that counts the number of even numbers in the array:
pythonimport random # Creating the array with 4 random positive three-digit numbers array = [random.randint(100, 999) for _ in range(4)] print("Array:", array) count_even = 0 # Counting the number of even numbers in the array for num in array: if num % 2 == 0: count_even += 1 print("Number of even numbers:", count_even)Output:
codeArray: [345, 897, 568, 234] Number of even numbers: 2In this code, the random module is used to generate the array with 4 random positive three-digit numbers. The
random.randint(100, 999)function is used to generate a random number between 100 and 999 (inclusive). Theforloop then iterates over each number in the array and checks if it is even using the modulus operator%. If the number is divisible by 2 (i.e., if the remainder is 0), then it is an even number and the variable is incremented by 1. Finally, the program prints the number of even numbers in the array. - User
Write an array filled with random positive three-digit numbers in C#. Write a program that will show the number of even numbers in the array. [345, 897, 568, 234] -> 2
- Assistant
Here's an example code in C# that creates an array with random positive three-digit numbers and counts the number of even numbers in the array:
csharpusing System; class Program { static void Main() { Random random = new Random(); // Creating the array with 4 random positive three-digit numbers int[] array = new int[4]; for (int i = 0; i < array.Length; i++) { array[i] = random.Next(100, 1000); } Console.WriteLine("Array: [" + String.Join(", ", array) + "]"); int countEven = 0; // Counting the number of even numbers in the array foreach (int num in array) { if (num % 2 == 0) { countEven++; } } Console.WriteLine("Number of even numbers: " + countEven); } }