Write a Program in Java to input ten integers and find the largest and the smallest
Answers
Answer:
now days you are illegible
Answer:
ANSWER IS BELOWW...
Explanation:
Hello guys, if you have gone through any coding interview or have done some professional Software development then you know that a good understanding of array data structure is crucial for any software developer but it doesn't come for free, you need to spend time and effort. The best way to develop this understanding by solving coding problems and there are lots of programming exercises beginners can do. One of them is writing a program to find the smallest and largest number in an integer array. Java programmers are no different than others, so they can do this program in Java, not just to understand array but also different relational operators available in Java.
In this program, you need to write a method, yes we call the function a method in Java, which will accept an integer array and then print the largest and smallest number from that array. Use of any third-party library or API method is not allowed, which means you need to do this exercise by using essential tools of Java programming language, which includes operators, control statements, keyword, and some classes from java.lang package.
This problem is also known as finding maximum and minimum numbers in an array, and the technique mentioned here can be used in any other programming language as well. As a bonus point, you can also write JUnit test cases to test your method, I have not done so and relied on simple main method to test my code to show the output and keep it short, essential for any example or demo.
Btw, if you preparing for a programming job interview, then let me repeat that a good knowledge of essential data structures like an array, linked list, binary tree, the hash table goes a long way in doing well on the interview. You should spend some time learning those and filling gaps in your understanding.
And, if you need an online course, then I highly recommend checking out Data Structures and Algorithms: Deep Dive Using Java course on Udemy. It's a hands-on course and covers all essential data structures. It's also very affordable, and you can get in just $10 on Udemy flash sales, which happens every month.
Java Program to find the smallest and largest number in an integer array
Here is a full code example of a Java program to find the smallest and largest number from an integer array. You can create a Java source file with the name MaximumMinimumArrayDemo.java and copy code there to compile and execute it in your favorite IDE. If you don't have an IDE setup, you can also compile and run this program by following steps I have shown on HelloWorld in Java.
If you look at the code here, we have created a method called the largestAndSmallest(int[] numbers) to print the largest and smallest number from int array passed to the program. We have used two variables largest and smallest, to store the maximum and minimum values from the array. Initially, the largest is initialized with Integer.MIN_VALUE and smallest is initialized with Integer.MAX_VALUE.
In each iteration of the loop, we compare the current number with the largest and smallest and update them accordingly. Since if a number is larger than largest, it can't be smaller than smallest, which means you don't need to check if the first condition is true, that's why we have used if-else code block, where else part will only execute if the first condition is not true.
Here is another logical diagram or flow chart to find the largest element from an array in Java, here instead of assigning the variable with Integer.MAX_VALUE, we have assigned the first element from the array.

Since array doesn't override the toString method in Java, we have used Arrays.toString() to print the contents of an array. Remember, this function is outside of core logic, so it's ok to use it. Since this is a static method, we can directly call this from the main method in Java, and so does our test code.
We pass the random array to this method and see if the largest and smallest number returned by the method is correct or not. For automated testing, a Unit test is better, but for a demonstration, you can use the main method.
Btw, if you preparing for a programming job interview, then don't forget to check the Cracking the Coding Interview book. It contains 150 Programming Questions and Solutions, which is more than enough for many coding interviews.