Question

In Java*

​​​​​​​Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear student,

The solution to the above problem statement in Java programming language is given as below:

Code:

import java.io.*;

import java.util.*;

public class Main

{

public static int countEven(int[] inputArray)//calculates the total number of even integers in array

{

int count=0;//stores number of even numbers

for(int i=0;i<20;i++)

{

if(inputArray[i]%2==0)

{

count++;

}

}

return count;

}

  

public static int countOdd(int[] inputArray)//calculates the total number of odd integers in array

{

int count=0;//stores number of odd numbers

for(int i=0;i<20;i++)

{

if(inputArray[i]%2!=0)

{

count++;

}

}

return count;

}

  

public static void printDividedBy5(int[] inputArray)

{

int count=0;//stores the number of integers divisible by printDividedBy5

for(int i=0;i<20;i++)

{

if(inputArray[i]%5==0)

{

System.out.print(inputArray[i]+",");

count++;

}

}

if(count==0)

{

System.out.println("Good bye!");//prints this if no number is divisible by 5

}

}

  

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("Enter 20 numbers between 0 to 100 inclusive of 0 and 100");

int nums[]=new int[20];//array of integers

int space=20;

int index=0;

while(space!=0)//till there is space left in array

{

int entry=sc.nextInt();

if(entry>=0&&entry<=100)

{

nums[index]=entry;

index++;

space--;

}

else

{

System.out.println("Wrong input. Please Enter a number between 0 to 100 inclusive of 0 and 100 ");

continue;

}

}//input done

  

int numEven=countEven(nums);

int numOdd=countOdd(nums);

  

System.out.println("Number of Even integers entered: "+numEven);

System.out.println("Number of Odd integers entered: "+numOdd);

printDividedBy5(nums);

}

}

----------------------------------------------------

Output:

Enter 20 numbers between 0 to 100 inclusive of 0 and 100 20 23 111 Wrong input. Please Enter a number between 0 to 100 inclus

​​​​​

I hope the given solution helps clear your doubt.

Don't forget to give it a thumbs up.

Regards

Add a comment
Know the answer?
Add Answer to:
In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java Write a program that reads an arbitrary number of 25 integers that are positive...

    In Java Write a program that reads an arbitrary number of 25 integers that are positive and even. The program will ask the user to re-enter an integer if the user inputs a number that is odd or negative or zero. The inputted integers must then be stored in a two dimensional array of size 5 x 5. Please create 3 methods: 1. Write a method public static int sum2DArray( int [1] inputArray ) The method sums up all elements...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • Write a program that instantiates an array of integers named scores. Let the size of the...

    Write a program that instantiates an array of integers named scores. Let the size of the array be 10. The program then first invokes randomFill to fill the scores array with random numbers in the range of 0 -100. Once the array is filled with data, methods below are called such that the output resembles the expected output given. The program keeps prompting the user to see if they want to evaluate a new set of random data. Find below...

  • Write a program that reads a file containing an arbitrary number of text integers that are...

    Write a program that reads a file containing an arbitrary number of text integers that are in the range 0 to 99 and counts how many fall into each of eleven ranges. The ranges are 0-9, 10-19, 20-29,..., 90-99 and an eleventh range for "out of range." Each range will correspond to a cell of an array of ints. Find the correct cell for each input integer using integer division. After reading in the file and counting the integers, write...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • JAVA Code: Complete the method, sumOdds(), below. The method takes in an array of integers, numbers,...

    JAVA Code: Complete the method, sumOdds(), below. The method takes in an array of integers, numbers, and returns the sum of all the odd numbers in the array. The method should return 0 if the array contains no odd numbers. For example, if the given array is [12, 10, 5, 8, 13, 9] then the method should return 27 (5+13+9=27). Starter Code: public class Odds { public static int sumOdds(int[] numbers) { //TODO: complete this method    } }

  • java 1. Write an application that generates n random integers in the range provided the user....

    java 1. Write an application that generates n random integers in the range provided the user. Use method genrate Even to generate even numbers only and generateOdd to generate odd numbers void generateOdd(int n, int r1, int r2); void generateEven(int n, int r1, int r2); Sample run: How many integers you have?5 What are your number ranges? 3 10 The generated even numbers are 6 4 8 10 6 The generated odd numbers are 37 9 35

  • 14.8 Prog 8 Overload methods (find avg) Write a program to find the average of a...

    14.8 Prog 8 Overload methods (find avg) Write a program to find the average of a set numbers (assume that the numbers in the set is less than 20) using overload methods. The program first prompts the user to enter a character; if the character is 'I' or 'i', then invokes the appropriate methods to process integer data set; if the character is 'R' or 'r', then invokes the appropriate methods to process real number data set; if the inputted...

  • Write a program which: Prints out the Multiplication Table for a range of numbers (positive integers)....

    Write a program which: Prints out the Multiplication Table for a range of numbers (positive integers). Prompts the user for a starting number: say 'x' Prompts the user for an ending number: say 'y' Prints out the multiplication table of 'x' up to the number 'y' Sample outputs include:    SPECIFIC REQUIREMENTS You must use the following method to load the array: public static void loadArray(int table[ ][ ], int x, int y) You must use the following method to...

  • Java question Q1) Use the following code snippet which generates a random sized array with random...

    Java question Q1) Use the following code snippet which generates a random sized array with random contents to complete the following problems: public int [] createRandomArray() {         int size = (int) (Math.random() * 10) + 1;         int[] array = new int [size];         for (int i = 0; i < array.length; i++) {             array[i] = (int) (Math.random() * 10 ) + 1;         }         return array;     } Assignment...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT