Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This question is related to java Please do not answer by handwriting Thank you Q

ID: 3580306 • Letter: T

Question

This question is related to java
Please do not answer by handwriting

Thank you

Q3. Write a java program that allows to: Ask the user to give a positive number between 10 and 20. Create an array of integer. The length of the array is the given number by the user. Fill the array with positive random numbers less than 200 Print the content of the array. Find and print o The odd numbers. o The maximum and the minimum numbers. o The sum o The average. NB: you have to run your program then join a screenshot of the execution to your answer

Explanation / Answer

import java.util.Scanner;
import java.util.*;
public class HelloWorld{
  
/* function to print odd numbers */
public static void oddNumbers(int[] arr){
System.out.println();
System.out.println("Odd numbers are:");
for(int i=0;i<arr.length;i++){
if(arr[i] % 2 != 0) /* if number is not divisible by 2 i.e odd,print the number */
System.out.print(arr[i]+" ");
}

}

/* method to print maximum number in the array */
public static void maximum(int[] arr){
int max=arr[0]; /* declaring a max variable and storing first value of the array */
/* loop till the length of the array to find the maximum number of the array */
for(int i=1;i<arr.length;i++){
if(arr[i]>max)
max=arr[i];
}
/* printing the maximum number */
System.out.println("The maximum is "+max);
}

/* method to print minimum number in the array */
public static void minimum(int[] arr){
int min=arr[0]; /* declaring a min variable and storing first value of the array */
/* loop till the length of the array to find the minimum number of the array */
for(int i=1;i<arr.length;i++){
if(arr[i]<min)
min=arr[i];
}
/* printing the minimum number */
System.out.println("The minimum is "+min);
}
  
/* method to find sum and average of the array */
public static void sumAvg(int[] arr){
double sum=0.0; /* declaring a double variable sum to store sum of the values */
/* loop to find the sum of the array */
for(int i=0;i<arr.length;i++)
sum=sum+arr[i];
/* printing sum of the array */
System.out.println("The sum is "+sum);
/* storing length of the array in a double variable length */
double length=arr.length;
/* finding average and storing that in a double variable avg */
double avg=sum/length;
/* printing average */
System.out.println("The average is "+avg);
}

public static void main(String []args){
int number; /* variable to store number entered by the user */
Random r = new Random(); /* creating a object of random class to generate random number */
Scanner input=new Scanner(System.in); /* variable for taking input from the user */
System.out.println("Give a positive number between 10 and 20");
number=input.nextInt(); /* storing the user entered input in number */
/* prompting the user to enter number untill number is not between 10 and 20 */
while(number < 10 || number > 20){
System.out.println("Give a positive number between 10 and 20");
number=input.nextInt();
}
/* declaring an array arr of length number entered by the user */
int[] arr=new int[number];
/* filling the array arr with random integers less than 200 */
for(int i=0;i<number;i++)
arr[i]=r.nextInt(201)+0; /* generating random integers less than 200 */
  
/* Printing the content of the array */
System.out.println("The content of your array is: ");
for(int i=0;i<number;i++)
System.out.print(arr[i]+" ");
  
oddNumbers(arr); /* calling oddNumbers method */
maximum(arr); /* calling method maximum */
minimum(arr); /* calling method minimum */
sumAvg(arr); /* calling method sumAvg */
  
  
}
}


/**********OUTPUT****************
Give a positive number between 10 and 20
8   
Give a positive number between 10 and 20
23
Give a positive number between 10 and 20
15
The content of your array is:   
122 46 42 98 183 77 190 45 129 161 139 184 26 18 111
Odd numbers are:
183 77 45 129 161 139 111 The maximum is 190   
The minimum is 18
The sum is 1571.0
The average is 104.73333333333333

***********OUTPUT**********/

/* Please do ask in case of any doubt,would glad to help you,thanks !! */

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote