Determine the highest and lowest number - java

I'm currently writing a program where the user must input 10 numbers and then the output will be the highest number and the lowest number. There is something wrong in my code but couldn't find it.
int highest=0, lowest=0, num=0;
Scanner scan = new Scanner(System.in);
for (int i=0; i<10; i++) {
System.out.print("Enter a number:");
num = scan.nextInt();
}
if (num > highest) {
highest = num;
}
else if(num < lowest) {
lowest = num;
}
System.out.println("Highest number is: " + highest);
System.out.println("Lowest number is: " + lowest);

Initialise your values differently:
int highest = Integer.MIN_VALUE;
int lowest = Integer.MAX_VALUE;
If you initialise them both to zero, you will never have a "highest" value below zero, or a "lowest" value above zero

You should put your two if conditions in the for loop else you will only compare the last number. And lowest shouldn't be set to 0 but to Integer.MAX_VALUE

You have some issues with your initialization and your logic:
int highest=Math.MIN_VALUE;
int lowest=Math.MAX_VALUE;
int num=0;
Scanner scan = new Scanner(System.in);
for(int i=0; i<10; i++){
System.out.print("Enter a number:");
num = scan.nextInt();
if (num > highest){
highest = num;
}
if(num < lowest){
lowest = num;
}
}
System.out.println("Highest number is: " + highest);
System.out.println("Lowest number is: " + lowest);
You should also use 2 if conditions rather than an else if. If you have only one number, chances are that you will end up with something similar to highest being equal to some digit you entered while lowest will still be equal to Math.MAX_VALUE. This can cause confusion.

You are implicitly assuming that lowest and largest are 0, that might now be the case,
try this code snippet..
class Main{
public static void main(String args[]){
int highest=0, lowest=0, num=0;
Scanner scan = new Scanner(System.in);
highest = lowest = scan.nextInt();
for(int i=1; i<10; i++){
System.out.print("Enter a number:");
num = scan.nextInt();
if (num > highest){
highest = num;
}
if(num < lowest){
lowest = num;
}
System.out.println("Highest number is: " + highest);
System.out.println("Lowest number is: " + lowest);
}
}
}

Related

WAP to find the second largest number without using an array in java

I have received a question in my school programming assignment, which goes like this:
Enter n count of numbers and find the largest and smallest among the lot. Also find the second largest number.
Now, the main problem is the second smallest number part.. We have still not been taught arrays, so I want a solution to this question without arrays. This is what I have made till now, through this code, I am able to find the largest and the smallest number correctly, but I find the second smallest number part tough!
import java.util.*;
class SecondLargest
{
public static void main(String args[])
{
System.out.println('\u000C'); //used to clear screen
Scanner y=new Scanner(System.in);
System.out.println("Enter the number of values to be entered");
int n=y.nextInt();
System.out.println("Enter number 1");
int num=y.nextInt();
int max=num,min=num,temp=num;
for(int i=2;i<=n;i++)
{
System.out.println("Enter number "+i);
num=y.nextInt();
if(num>max)
max=num;
else if(num<min)
min=num;
}
System.out.println("The largest number is "+max);
System.out.println("The smallest number is "+min);
}
}
Just make another variable like your min and max variables.
if (num<max && num >min) {
}
Create additional variable max2 and shift previous max value when a new maximum is detected, or check if there is a value over max2:
Scanner y = new Scanner(System.in);
System.out.println("Enter the number of values to be entered");
int n = y.nextInt();
System.out.println("Enter number 1");
int num = y.nextInt();
int max = num, max2 = num, min = num;
for (int i = 2; i <= n; i++) {
System.out.println("Enter number " + i);
num = y.nextInt();
if (num < min) {
min = num;
} else if (num > max) {
max2 = max; // shift previous max to become the 2nd largest
max = num; // store max
} else if (num > max2) {
max2 = num;
}
}
System.out.println("The largest number is " + max);
System.out.println("The 2nd largest number is " + max2);
System.out.println("The smallest number is " + min);

Java program not producing correct result

I am trying to write a program that takes a set of 10 numbers from the user, finds the smallest and largest value within the set, and displays it to the user. I kind of got the program to work but I am experiencing an issue. It looks like it skips through the first number and assigns the second number to be the smallest value. In class we haven't gotten to arrays yet so, I have to assume that the first value entered in the smallest and largest value Here's what I have:
int smallestValue;
int largestValue;
int numInput;
int counter = 1;
int numSets = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter a set of 10 numbers: ");
numInput = input.nextInt();
smallestValue = input.nextInt();
largestValue = input.nextInt();
counter++;
while (counter != 9)
{
numInput = input.nextInt();
counter++;
if (numInput > largestValue)
{
largestValue = numInput;
}
else if (numInput < smallestValue )
{
smallestValue = numInput;
}
}
System.out.println( "Smallest is " + smallestValue );
System.out.println("Largest is " + largestValue);
Thanks in advance!
It skips value because you call input.nextInt() before the loop, you don't need to, and use Integer.MAX_VALUE and Integer.MIN_VALUE to initialize the min and max. Also when you iterate depending on a counter, it's commonly with a for loop
int numInput;
int smallestValue = Integer.MAX_VALUE;
int largestValue = Integer.MIN_VALUE;
Scanner input = new Scanner(System.in);
System.out.print("Enter a set of 10 numbers: ");
for(int i=0; i<10; i++){
numInput = input.nextInt();
if (numInput > largestValue){
largestValue = numInput;
}else if (numInput < smallestValue ){
smallestValue = numInput;
}
}
System.out.println( "Smallest is " + smallestValue );
System.out.println("Largest is " + largestValue);

Basic Java, Largest and smallest integer

The purpose for the code is to prompt the user for 10 integers and then display the largest and smallest integer. I was able to ask the user for the 10 integers and display the largest number; however, the smallest number isn't shown. I believe the issue is with me setting smallest to 0.
import java.util.Scanner;
public class LargeSmall {
public static void main(String[] args) {
int counter = 1;
int largest = 0;
int smallest = 0;
int number = 0;
Scanner input= new Scanner(System.in);
while (counter <= 10) {
System.out.print("Enter number: ");
number = input.nextInt();
if (number > largest) {
largest = number;
}else if(number < smallest) {
smallest = number;
}else {
System.out.print("Number isn't distinct");
counter = counter + 1;
}
System.out.println("Largest number is: " + largest);
System.out.println("Smallest number is: " + smallest);
}
}
Initialize your values like this so we guarantee that we start with the smallest/largest possible reference for our numbers to compare against to:
int largest = Integer.MIN_VALUE;
int smallest = Integer.MAX_VALUE;
int number;
int old = Integer.MIN_VALUE;
...and then some tweaks have to be made:
Change the way you check for dupe numbers (use and old variable).
Check if every new number is smaller than smallest number always (so, in the first iteration we assign the first value to smallest and largest)
Like this:
number = input.nextInt();
if (old != number) {
old = number;
if (number > largest) {
largest = number;
}
if(number < smallest) {
smallest = number;
}
/* consider moving next line here (this will guarantee to go to the next
iteration only if the numbers are different, and get in the final messages,
valid values for `smallest` and `largest` */
// counter = counter + 1;
} else {
System.out.print("Number isn't distinct");
}
Just make > into >= and < into `<=
Since 0 < 0 evaluates to false, your code block does not run. Thus the correction.
Variable smallest is always 0 so the program never go into this block else if(number < smallest)
You should add this code before if(number < smallest)
if(smallest == 0) {
smallest = number;
}
Also your code doesn't catch the distinct numbers. To find the distinct number you should define a list List<Integer> numbers = null; at the start of the program.
Then you can check if the input is entered or not in the while loop like this:
if(numbers.contains(number))
System.out.print("Number isn't distinct");
else
numbers.add(number);
Sets the largest and smallert variables with the user's first input and then compare
import java.util.Scanner;
public class LargeSmall {
public static void main(String[] args) {
System.out.print("Enter number: ");
Scanner input= new Scanner(System.in);
int number = input.nextInt();
int counter = 1;
int largest = number;
int smallest = number;
while (counter < 10) {
System.out.print("Enter number: ");
number = input.nextInt();
if (number > largest) {
largest = number;
}else if(number < smallest) {
smallest = number;
}else
System.out.print("Number isn't distinct");
counter = counter + 1;
}
System.out.println("Largest number is: " + largest);
System.out.println("Smallest number is: " + smallest);
}
}

How to use Java to calculate average from input?

I need a program that should add x numbers. The numbers should come from user input so I need some sort of loop. I have gotten as far as shown below, but I'm stuck since I have no idea how to add a new number without deleting the previous?
System.out.println("How many numbers to use?");
int number = keyboard.nextInt();
for (int i = 0; i<number ; i++) {
System.out.println("whats the number");
double first = keyboard.nextDouble();
}
If all you need is the average, you don't need to keep all the numbers you get from user input. Just keep one variable that holds their sum.
define the sum variable outside the loop (initialized to 0), and add to it each number you get from user input.
int number = keyboard.nextInt();
double sum = 0;
for (int i = 0; i<number ; i++)
{
System.out.println("whats the number");
sum += keyboard.nextDouble();
}
double average = sum / number;
public class Average {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double sum = 0;
int num;
System.out.println("enter how many num");
num = sc.nextInt();
System.out.println("please enter " + num + " numbers");
for (int i = 0; i < num; i++) {
sum += sc.nextDouble();
}
double avg = sum / num;
System.out.println("Average of " + num + " numbers is:" + avg);
}
}

Getting the lowest and highest value from integers without using arrays?

I'm trying to write a class which reads 5 integers from the user and returns the highest and lowest value back. This must be done using loops and without using arrays and Integer.MIN.Value/Integer.MAX.Value. I've already succeeded writing code that gets 5 integers from the user and returns the highest value but I just can't get both the highest and the lowest value returned in the same class.
Here is the code I mentioned above:
import java.util.Scanner;
public class Ovning_321 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int number;
int max = 0;
for (int x = 0; x<5; x++){
System.out.print("Give me an integer: ");
number = input.nextInt();
if (number > max){
max = number;
}
}
System.out.println("Highest value: " + max);
}
}
here you go :)
import java.util.Scanner;
public class Ovning_321 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int number;
int max = 0;
int min = 0;
for (int x = 0; x<5; x++){
System.out.print("Give me an integer: ");
number = input.nextInt();
if (x == 0 || number > max){
max = number;
}
if (x == 0 || number < min){
min = number;
}
}
System.out.println("Highest value: " + max);
System.out.println("Lowest value: " + min);
}
}
Why not just repeat your max logic for min?
public class Ovning_321 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Give me an integer: ");
number = input.nextInt();
int max = number;
int min = number;
for (int x = 0; x<4; x++){
System.out.print("Give me an integer: ");
number = input.nextInt();
if (number > max){
max = number;
}
if (number < min){
min = number;
}
}
System.out.println("Highest value: " + max);
System.out.println("Lowest value: " + min);
}
}
Note that max and min are initially set to the first number that the user enters, so there will be no false 0's and no need to MAX_INT or MIN_INT. This in turn makes the loop run once less so terminate at i == 4 instead of 5.

Categories