Java Program of Sum of the maximum and the minimum element

Question:

Anjali gets n numbers in an array. Write a Java program to print the sum of the maximum and the minimum element in the array. If the size of an array is 0 or less print “Invalid Array Size”.

Sample Input 1:

Enter the size of an array:

5

Enter the elements:

45

23

48

90

89

Sample Output 1:

113

Sample Input 2:

Enter the size of an array:

0
Sample Output 2:

Invalid Array Size

Code:

Sum.java

import java.util.*;
public class Sum 
{
    int max(int[] arr)
    {
        int max=0;
        for(int i=0;i<arr.length;i++)
        {
            if(arr[i]>max)
            {
                max=arr[i];
            }
        }
        return max;
    }
    
    int min(int[] arr)
    {
        int min=arr[0];
        for(int i=0;i<arr.length;i++)
        {
            if(arr[i]<min)
            {
                min=arr[i];
            }
        }
        return min;
    }
    
    public static void main (String[] args) {
        Scanner sc=new Scanner(System.in);
        Sum obj=new Sum();
        
        System.out.println("Enter the size of an array:");
        int n=sc.nextInt();
        
        if(n>0)
        {
            int[] arr=new int[n];
            
            System.out.println("Enter the elements:");
            for(int i=0;i<n;i++)
            {
                arr[i]=sc.nextInt();
            }
            
            System.out.println(obj.max(arr)+obj.min(arr));
        }
        else
        {
            System.out.println("Invalid Array Size");
        }
        
        
        
    }
}

Recommended:

  1. Array square
  2. Generate number using odd digits
  3. Alternate Number Difference
  4. Next Greatest number
  5. Mark Comparison
  6. Print the characters in descending order
  7. Vowels in a fishBowl
  8. Least offer
  9. Ascending and descending order
  10. Mail Domain
  11. Count repeating words
  12. Sentence – Convert to upper and lower
  13. Count consecutive repeating characters
  14. Zig zag Array
  15. Pass and Fail Count
  16. Search a Course
  17. Average and Grade Calculation
  18. String – Find and replace the character (first occurrence)
  19. Sort the first and second half of an array
  20. Retail Shop
  21. Palindrome
  22. Numerology
  23. InitCap
  24. Array Compatiblilty
  25. Sum of the maximum and the minimum element
  26. String Concatenation
  27. Find Average Age
  28. Login
  29.  

Leave a Comment

Your email address will not be published. Required fields are marked *

x