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.
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 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
Write a function called randFill that fills the entries of an array with random integers in...
Write a function called randFill that fills the entries of an array with random integers in the range from 10 to 99 (inclusive). (You should use a standard Java method to generate the values. Your solution should use no more than 6 lines of code.) For example, a program that uses the function randFill follows. public class P4 { public static void main(String args[]) { int x[]; x = randFill(5); for (int i = 0; i < 5; i++) System.out.print(x[i]...
Q.1: Use the NumPy’s random number generation to create an array of five random integers that...
Q.1: Use the NumPy’s random number generation to create an array of five random integers that represent summertime temperatures in the range 60–100, then perform the following tasks: a. Convert the array into the Series named temperatures and display it. b. Determine the lowest, highest and average temperatures. c. Produce descriptive statistics for the Series. Q.2: Given the following dictionary; temps = {'Mon': [68, 89], 'Tue': [71, 93], 'Wed': [66, 82], 'Thu': [75, 97], 'Fri': [62, 79]} perform the following...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT