Question

In: Computer Science

write code to count the number of odd integers in an array of 100 random integers...

write code to count the number of odd integers in an array of 100 random integers in the range [0,99].

Solutions

Expert Solution

import java.util.Random;
public class OddNumbers{
//code to count the number of odd integers in an array of 100 random integers in range 0-99
public static void main(String []args)
{
Random r=new Random();
int count=0;
int[] arr=new int[100];
  
for(int i=0;i<100;i++)// loop to generate random numbers in range 0-99
arr[i]=r.nextInt(100);
  
for (int i=0;i<100;i++) // loop to check odd test for 100 numbers
{
   if(arr[i]%2 != 0)
    count++;//to count if a number not divided by 2
}
System.out.println("number of odd numbers="+count);
}//end main
}//end class

//output generated

$javac OddNumbers.java
$java -Xmx128M -Xms16M OddNumbers
number of odd numbers=53

Related Solutions

*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then prints the following output: a. every element (on a single line) b. every element at an even index (on a single line) c. every even element (on a single line) d. all elements in reverse order (on a single line) e. only the first and last elements (on a single line)
write a code for given an array of integers where wachelement represents the maximum number...
write a code for given an array of integers where wach element represents the maximum number of jumps to reach the end of the array(starting from the first element) if an element O,then no jump can be made from that element if it is not possible to reach the end then output in c
Write a program that does the following: Generate an array of 20 random integers between -100...
Write a program that does the following: Generate an array of 20 random integers between -100 and 100. Compute the average of the elements of the array and find the number of elements which are above the average. For example, if the elements of the array were 5 2 4 1 3 then your program should output The average is 3.0 There are two elements above the average Find the smallest element of the array as well as its index...
Write code that would allocate the space for an array of 20 integers. It will be...
Write code that would allocate the space for an array of 20 integers. It will be pointed to by a variable named "junk" Write code that puts “file did not open” into an error stream.
Write c code to determine if a binary number is even or odd. If it is...
Write c code to determine if a binary number is even or odd. If it is odd, it outputs 1, and if it is even, it outputs 0. It has to be less than 12 operations. The operations have to be logical, bitwise, and arithmetic.
Written in JAVA Code Write a program that inserts 25 random integers from 0 to 100...
Written in JAVA Code Write a program that inserts 25 random integers from 0 to 100 in order into a LinkedList object. The program should sort the elements, then calculate the sum of the elements and the floating-point average of the elements.
Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
Creates a 100-element array, either statically or dynamically Fills the array with random integers between 1...
Creates a 100-element array, either statically or dynamically Fills the array with random integers between 1 and 100 inclusive Then, creates two more 100-element arrays, one holding odd values and the other holding even values. Prints both of the new arrays to the console. In C++. Thank you!
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++, using pass by reference
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT