Find largest and smallest numbers number using Arrays - java

Trying to let users enter number of integers so I can set array length then find the max and min value. However, I can't find max and min. Please help.
import java.util.Scanner;
import java.util.Arrays;
public class ExerciseC{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the number of integers you would like to enter:");
int numberEnter = keyboard.nextInt();
System.out.println("Enter some integers:");
int integers = keyboard.nextInt();
int numbers [] = new int [numberEnter];
int maxValue = numbers[0];
int minValue = numbers[0];
int max = 0;
int min = 0;
for (int index = 1; index < numbers.length; index ++) {
if (numbers[index] > maxValue) {
maxValue = numbers [index];
}
}
System.out.println("Print: " + maxValue);
System.out.println("The difference between the largest and the smallest is: ");
}
}

You don't seem to be entering more then one value (and you never store integers in your array). Also, you aren't setting the min. I think you wanted
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Number of integers to enter:");
int numberEnter = keyboard.nextInt();
int numbers[] = new int[numberEnter];
int pos = 0;
do {
System.out.printf("Please enter integer #%d/%d:%n", pos, numberEnter);
numbers[pos++] = keyboard.nextInt();
} while (pos < numberEnter && keyboard.hasNextInt());
int min = numbers[0];
int max = numbers[0];
for (pos = 1; pos < numbers.length; pos++) {
if (numbers[pos] < min) { // <-- test min.
min = numbers[pos];
}
if (numbers[pos] > max) { // <-- test max.
max = numbers[pos];
}
}
// Display everything.
System.out.printf("%s Min: %d Max: %d%n", Arrays.toString(numbers),
min, max);
}

Your numbers[] is empty. The user's input is not stored into the array.
Here is your fixed code:
package com.company;
import java.util.Scanner;
public class ExerciseC{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the number of integers you would like to enter:");
int numberEnter = keyboard.nextInt();
int numbers [] = new int [numberEnter];
for (int i = 0; i < numberEnter; i++) {
System.out.println("Enter integer:");
numbers[i] = keyboard.nextInt();
}
int maxValue = numbers[0];
int minValue = numbers[0];
for (int index = 1; index < numbers.length; index ++) {
if (numbers[index] > maxValue) {
maxValue = numbers [index];
}
}
System.out.println("Print: " + maxValue);
System.out.println("The difference between the largest and the smallest is: ");
}
}

import java.util.Scanner;
class StdR {
public static void main(String[] args) {
// TODO Auto-generated method stub
StdR st = new StdR();
st.stdR();
//System.out.println(st.stdR();
}
void stdR()
{
char[] grade = {'A','B','C','D','E','F'};
Scanner input = new Scanner(System.in);
byte[] st = new byte[3];
double[] percentage = new double[st.length];
for(byte s = 0; s < st.length; s++){
System.out.println("\nStudent " + s);
short noOfMarks = 0;
short totalMarks = 450;
percentage[s] = 0.0;
byte[] marks = new byte[5];
for (byte i = 0; i < marks.length; i++){
System.out.println("Enter marks of Chapter "+ i + ": ");
marks[i] = input.nextByte();
noOfMarks += marks[i];
percentage[s] += (marks[i] * 100) / totalMarks;
}
System.out.print("No of marks: " + noOfMarks + "\t");
System.out.print("Percentage: " + percentage[s] + "\t");
if (percentage[s] > 79.0 && percentage[s] < 100.1)
System.out.print("Grade: " + grade[0]);
else if (percentage[s] > 69.0 && percentage[s] < 80.0)
System.out.print("Grade: " + grade[1]);
else if (percentage[s] > 59.0 && percentage[s] < 70.0)
System.out.print("Grade: " + grade[2]);
else if (percentage[s] > 49.0 && percentage[s] < 60.0)
System.out.print("Grade: " + grade[3]);
else if (percentage[s] > 39.0 && percentage[s] < 50.0)
System.out.print("Grade: " + grade[4]);
else if (percentage[s] < 40.0)
System.out.print("Grade: " + grade[5]);
}
double smallest = percentage[0] , largest= percentage[0];
for (int i=0 ;i< percentage.length; i++) {
if (percentage[i] < smallest) {
smallest = percentage[i];
} // end finding smallest
if (percentage[i] > largest) {
largest = percentage[i];
}
}
System.out.println("\n1st Position and Top percentage is " + largest);
System.out.println("\nLast Position and Least percentage is "+smallest);
}
}

Related

Varied amounts of input data

Problem: Statistics are often calculated with varying amounts of input data. Write a program that takes any number of non-negative integers as input, and outputs the average and max. A negative integer ends the input and is not included in the statistics.
Ex: When the input is:
15 20 0 5 -1
the output is:
10 20
You can assume that at least one non-negative integer is input.
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner (System.in);
int num = 0;
int count = 0;
int max = 0;
int total = 0;
int avg = 0;
do {
total += num;
num = scnr.nextInt();
count = ++count;
if (num >= max) {
max = num;
}
} while (num >= 0);
avg = total/(count-1);
System.out.println(avg + " " + max);
}
}
I had a lot of trouble with this problem. Is there any way I could have done this without having to do count -1 while computing the average?
Also, is this this the most efficient way I could have done it?
How about this? If you have questions from the implementation, please ask.
public static void main(String[] args) throws IOException {
Scanner scnr = new Scanner(System.in);
int count = 0, max = 0, total = 0;
int num = scnr.nextInt();
while (num >= 0) {
count++;
total += num;
max = Math.max(max, num);
num = scnr.nextInt();
}
int avg = count == 0 ? 0 : total/count;
System.out.println(avg + " " + max);
}
If you use while loop instead of do-while loop, you don't have to count the negative number input anymore. And no, it's not the most efficient way, but it's a good start!
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner (System.in);
int userNum;
int maxNum = 0;
int totalSum = 0;
int averageNum;
int count = 0;
userNum = scnr.nextInt();
while (userNum >= 0) {
if (userNum > maxNum) {
maxNum = userNum;
}
totalSum += userNum;
++count;
userNum = scnr.nextInt();
}
averageNum = totalSum / count;
System.out.println("" + averageNum + " " + maxNum);
}
}
int Num;
int max = 0;
int total = 0;
int average;
int count = 0;
Num = scnr.nextInt();
while (Num >= 0) {
if (Num > max) {
max = Num;
}
total += Num;
++count;
Num = scnr.nextInt();
}
average = total / count;
System.out.println("" + average + " " + max);
}
}

Count all values greater than 5.5 in an array java

I'm trying to print the count of numbers in the array that are greater than 5.5, but I have no idea where to start. I got the following code:
package les5;
import java.util.*;
public class Les5 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("How many numbers would you like to add? ");
int totalNumbers = s.nextInt();
double[] number = new double[totalNumbers];
for (int i = 1; i <= totalNumbers; i++) {
System.out.print("Number 1 " + i + ": ");
number[i - 1] = s.nextDouble();
}
int numbCount = number.length;
double avgNumber = Arrays.stream(number).sum() / number.length;
System.out.println("Numbers count: " + numbCount);
System.out.println("Average: " + avgNumber);
}
}
At the end it has to say: "Total numbers greater than 5.5: x"
Could anyone help me out?
Just iterate over the array and at each step check whether the current array element is greater than 5.5. If it is, increase a counter variable by 1.
double[] number = {10, 2, 3, 5, 6, 5.6};
int count = 0;
for (int i = 0; i < number.length; i++) {
if (number[i] > 5.5) {
count++;
}
}
System.out.println("Total numbers greater than 5.5: " + count);
public class Les5 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("How many numbers would you like to add? ");
int N = s.nextInt();
double[] number = new double[N];
//
int greaterThan5 = 0;
for (int i = 0; i <= totalNumbers; i++) {
System.out.print("Number " + i + ": ");
number[i] = s.nextDouble();
if(number[i] >5.5)
greaterThan5++;
}
int numbCount = number.length;
double avgNumber = Arrays.stream(number).sum() / number.length;
System.out.println("Numbers count: " + numbCount);
System.out.println("Average: " + avgNumber);
System.out.println("Greater than 5: " + greaterThan5);
}
}

How to get minimum and maximum individually and not as a whole?

Program does this:
Check if the max and min changes over the whole course of runs, and then prints them to the respective variables after each instance is complete based on user input.
But what I want it to do is:
Check the max and min for each individual run, then print them to the respective variables.
How can I change the code below so that it gets the max and min of each run instead of the overall max and min?
Here is the code:
package test;
import java.util.*;
public class Test {
public static void main(String[] args) {
Random generator = new Random();
float smallest, largest;
int years;
float[] array = new float[12];
smallest = Integer.MAX_VALUE;
largest = Integer.MIN_VALUE;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter desired years: ");
years = keyboard.nextInt();
for (int i = 1; i <= years; i++){
System.out.println("Year " + i);
for (int month = 1; month <= array.length; month++){
array[i] = generator.nextFloat() * 100;
System.out.println("Month " + month + ": " + array[i]);
if(array[i] < smallest)
smallest = array[i];
if(array[i] > largest)
largest = array[i];
}
System.out.println("Max = " + largest);
System.out.println("Min = " + smallest);
}
You aren't resetting largest and smallest at the end of the loop.
package test;
import java.util.*;
public class Test {
public static void main(String[] args) {
Random generator = new Random();
float smallest, largest;
int years;
float[] array = new float[12];
smallest = Integer.MAX_VALUE;
largest = Integer.MIN_VALUE;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter desired years: ");
years = keyboard.nextInt();
for (int i = 1; i <= years; i++) {
System.out.println("Year " + i);
for (int month = 1; month <= array.length; month++) {
array[i] = generator.nextFloat() * 100;
System.out.println("Month " + month + ": " + array[i]);
if (array[i] < smallest)
smallest = array[i];
if (array[i] > largest)
largest = array[i];
}
System.out.println("Max = " + largest);
System.out.println("Min = " + smallest);
// HERE
// Reset largest and smallest
largest = Integer.MIN_VALUE;
smallest = Integer.MAX_VALUE;
}
}
}

Even, Odd with min and max for the Odd only [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Prompt the user to enter the size of an array and allow the user to
input integer values to your array. Check each element if it is even
or odd. If even, print solved elemets (ascending order), If odd, get
the max and min using conditional statement. Output the result.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
int n = sc.nextInt();
int s[] = new int[n];
for (int i = 0; i < n; i++) {
int e = sc.nextInt();
s[i] = e;
}
Arrays.sort(s);
System.out.println("\nEven numbers in ascending order:");
for (int j = 0; j < n; j++) {
if (s[j] % 2 == 0) {
System.out.print(s[j] + " ");
}
}
System.out.println("\nOdd numbers in descending order:");
for(int j = (n -1); j >= 0; j--) {
if (s[j] % 2 == 1) {
System.out.print(s[j] + " ");
}
}
}
I don't know how to add min/max for the Odd
If I understand your question, you could try this:
int min = Integer.MAX_VALUE; // init min to the most max value
int max = 0; // store max value
for(int j = (n -1); j >= 0; j--) {
if (s[j] % 2 == 1) {
if (min > s[j]) {
min = s[j];
}
if (max < s[j]) {
max = s[j];
}
System.out.print(s[j] + " ");
}
}
System.out.println("min = " + min);
System.out.println("max = " + max);
Full code:
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
int n = sc.nextInt();
int s[] = new int[n];
for (int i = 0; i < n; i++) {
int e = sc.nextInt();
s[i] = e;
}
Arrays.sort(s);
System.out.println("\nEven numbers in ascending order:");
for (int j = 0; j < n; j++) {
if (s[j] % 2 == 0) {
System.out.print(s[j] + " ");
}
}
//===========================================
System.out.println("\nOdd numbers in descending order:");
int min = Integer.MAX_VALUE; // init min to the most max value
int max = 0; // store max value
for(int j = (n -1); j >= 0; j--) {
if (s[j] % 2 == 1) {
if (min > s[j]) {
min = s[j];
}
if (max < s[j]) {
max = s[j];
}
System.out.print(s[j] + " ");
}
}
System.out.println(); // for new line
System.out.println("min = " + min);
System.out.println("max = " + max);
}
The solution to my problem is:
Scanner sc = new Scanner(System.in);
while (true) {
System.out.print("Enter a Value: ");
int val = s.nextInt();
if (val == 0) {
break;
}
if (val < min) {
min = val;
}
if (val > max) {
max = val;
}
}
System.out.println("min: " + min);
System.out.println("max: " + max);

Java program to find the largest & smallest number in n numbers without using arrays [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 months ago.
Improve this question
I could get the largest without using arrays but, unable to get the smallest one.
public static void main(String[] args)
{
int smallest=0;
int large=0;
int num;
System.out.println("enter the number");
Scanner input=new Scanner(System.in);
int n=input.nextInt();
for(int i=0;i<n;i++)
{
num=input.nextInt();
if(num>large)
{
large=num;
}
System.out.println("the largest is:"+large);
//gives the largest number in n numbers
code for the smallest..
if(i==0&&num>0)
small=num;
if(num<small)
small=num;
System.out.println(small);
}
Try this :
int smallest = Integer.MAX_VALUE;
for(int i=0;i<n;i++)
{
num=input.nextInt();
if(num>large)
{
large=num;
}
if(num<smallest){
smallest=num;
}
public static void main(String[] args) {
int smallest = 0;
int large = 0;
int num;
System.out.println("enter the number");//how many number you want to enter
Scanner input = new Scanner(System.in);
int n = input.nextInt();
num = input.nextInt();
smallest = num; //assume first entered number as small one
// i starts from 2 because we already took one num value
for (int i = 2; i < n; i++) {
num = input.nextInt();
//comparing each time entered number with large one
if (num > large) {
large = num;
}
//comparing each time entered number with smallest one
if (num < smallest) {
smallest = num;
}
}
System.out.println("the largest is:" + large);
System.out.println("Smallest no is : " + smallest);
}
Try this...This simple
import java.util.Scanner;
class numbers
{
public static void main(String args[])
{
int x, y, z;
System.out.println("Enter three integers ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
z = in.nextInt();
if ( x > y && x > z )
System.out.println("First number is largest.");
else if ( y > x && y > z )
System.out.println("Second number is largest.");
else if ( z > x && z > y )
System.out.println("Third number is largest.");
else
System.out.println("Entered numbers are not distinct");
}
}
public class Main {
public static void main(String[] args) {
int i = 10;
int j = 20;
int k = 5;
int x = (i > j && i > k) ? i : (j > k) ? j : k;
int y = (i < j && i < k) ? i : (j < k) ? j : k;
System.out.println("Largetst Number : "+x);
System.out.println("Smallest Number : "+y);
}
}
Output:
Largetst Number : 20
Smallest Number : 5
Try the code mentioned below
public static void main(String[] args) {
int smallest=0; int large=0; int num;
System.out.println("enter the number");
Scanner input=new Scanner(System.in);
int n=input.nextInt();
num=input.nextInt();
smallest = num;
for(int i=0;i<n-1;i++)
{
num=input.nextInt();
if(num<smallest)
{
smallest=num;
}
}
System.out.println("the smallest is:"+smallest);
}
#user3168844: try the below code:
import java.util.Scanner;
public class LargestSmallestNum {
public void findLargestSmallestNo() {
int smallest = Integer.MAX_VALUE;
int large = 0;
int num;
System.out.println("enter the number");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
for (int i = 0; i < n; i++) {
num = input.nextInt();
if (num > large)
large = num;
if (num < smallest)
smallest = num;
System.out.println("the largest is:" + large);
System.out.println("Smallest no is : " + smallest);
}
}
public static void main(String...strings){
LargestSmallestNum largestSmallestNum = new LargestSmallestNum();
largestSmallestNum.findLargestSmalestNo();
}
}
import java.util.Scanner;
public class LargestSmallestNum {
public void findLargestSmallestNo() {
int smallest = Integer.MAX_VALUE;
int large = 0;
int num;
System.out.println("enter the number");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
for (int i = 0; i < n; i++) {
num = input.nextInt();
if (num > large)
large = num;
if (num < smallest)
smallest = num;
System.out.println("the largest is:" + large);
System.out.println("Smallest no is : " + smallest);
}
}
public static void main(String...strings){
LargestSmallestNum largestSmallestNum = new LargestSmallestNum();
largestSmallestNum.findLargestSmalestNo();
}
}
import java.util.Scanner;
public class LargestSmallestNumbers {
private static Scanner input;
public static void main(String[] args) {
int count,items;
int newnum =0 ;
int highest=0;
int lowest =0;
input = new Scanner(System.in);
System.out.println("How many numbers you want to enter?");
items = input.nextInt();
System.out.println("Enter "+items+" numbers: ");
for (count=0; count<items; count++){
newnum = input.nextInt();
if (highest<newnum)
highest=newnum;
if (lowest==0)
lowest=newnum;
else if (newnum<=lowest)
lowest=newnum;
}
System.out.println("The highest number is "+highest);
System.out.println("The lowest number is "+lowest);
}
}

Categories