Skip to main content

Java Program To Find Minimun and Maximum Element From An Array

 Java Program To Find Minimum and Maximum Element From An Array

===========================================================================

import java.util.Scanner;

public class MinAndMaxElementFromArray {

    public static void main(String[] args) {

        // Accept the Array Size and Array Elements from the console (User)
        int arr[]; // Defining an Array

        System.out.println("Enter the size of an Array:");
        Scanner sc = new Scanner(System.in);
        Integer size = sc.nextInt(); // Accept the Size Of Array From console

        arr = new int[size]; // Declaration of an array

        // Accepting the elements from console (User)
        for (int i = 0; i < arr.length; i++) {
            System.out.println("Enter the " + (i + 1) + " Element");
            Integer element = sc.nextInt(); // Accepting one by one element from the User / Console
            arr[i] = element; // Copy one by one element into Array
        }

        // Printing the Elements Of an array To Console
        // This loop is used for displaying elements on the console
        System.out.println("The Elements of an Array are :");
        System.out.print("{");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + ",");
        }
        System.out.println("}");

        // Find Out The Max Element From An Array
        // Loop for finding out Max Element From An Array
        Integer max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];

            }

        }
        System.out.println("The Maximum Element Of An Array is:" + max);

        // Find Out The Min Element From An Array
        // Loop for finding out Min Element From An Array
        Integer min = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] < min) {
                min = arr[i];
            }
        }

        System.out.println("The Minimum Element Of An Array is:" + min);

    }

}


Comments

Popular posts from this blog

Java Programs Asked In An Interview | Java 8 | Core Java

 Q1) // Write a Java Program To Find Out the Character Occurnaces In A String. import java . util . HashMap ; import java . util . Map ; public class CharOccurnacesInString {     // Write a Java Program To Find Out the Character Occurnaces In A String.     public static void main ( String [] args ) {         String str = "I am Sandeep Aswar" ;         str = str . replaceAll ( " " , "" ). toLowerCase ();         char [] ch = str . toCharArray ();         Map < Character , Integer > charMap = new HashMap <>();         for ( char c : ch ) {             if ( charMap . containsKey ( c )) {                 charMap . put ( c , charMap . get ( c ) + 1 );             } else {                 charMap . pu...

Java 8 Programs Asked In Interview For Experienced Professionals | Java 8 Coding Interview Questions Asked In The Interview.

 Q1) // Given Two Strings. Find Out the Two Strings are Anagrams or not.     // Anagram means a String which has same characters present with the another     // String,     // Only the sequence of the Characters are different. package J ava C oncept O f D ay ; import java . util . stream . Collectors ; import java . util . stream . Stream ; public class AnagramStrings {     // Given Two Strings. Find Out the Two Strings are Anagrams or not.     // Anagram means a String which has same characters present with the another     // String,     // Only the sequence of the Characters are different.     public static void main ( String [] args ) {         String str1 = "Listen" ;         String str2 = "Silent" ;         str1 = Stream . of ( str1 . split ( "" )). map ( String :: toLowerCase ). sorted (). collect ( Collectors . joining ());...

How many primitive data types are there in Java?

Java includes a set of fundamental data types known as primitive data types. These data types are the essential components for storing and managing various kinds of data, including numbers, characters, and boolean values. Java encompasses a total of eight primitive data types, and this article aims to provide an in-depth explanation of each type in a simplified manner. 1.      byte: The byte data type is designed for storing small integer values, with a range spanning from -128 to 127. 2.      short: Slightly more accommodating than byte , the short data type can be employed for storing relatively larger integer values, ranging from -32,768 to 32,767. 3.      int: Among the most commonly used primitive data types, int can hold larger integer values, with a range extending to approximately -2 billion to 2 billion. 4.      long: When dealing with very large integer values, the long data t...