Sum of the maximum and the minimum element Program in java

In this post, we will learn Sum of the maximum and the minimum element Program in java Programming language.

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:

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");
        }
    }
}
public class Student
{
    private int id;
    private String name;
    private int[] marks;
    private float average;
    private char grade;
    public void setId(int id)
    {
        this.id=id;
    }
    public int getId()
    {
        return this.id;
    }
    public void setName(String name)
    {
        this.name=name;
    }
    public String getName()
    {
        return this.name;
    }
    public void setMarks(int[] marks)
    {
        this.marks=marks;
    }
    public int[] getMarks()
    {
        return this.marks;
    }
    public void calculateAvg()
    {   float sum=0;
        for(int i=0;i<this.marks.length;i++)
        {
            sum+=this.marks[i];
        }
        average=(float)(sum/(this.marks.length));
        setAverage(average);
    }
    public void findGrade()
    {int flag=0;
        for(int i=0;i<this.marks.length;i++)
        {
            if(this.marks[i]<50)
            {
                flag++;
            }
        }
        if(flag>0)
        {
            grade='F';
        }
        else
        {
            if(this.average>=80 && this.average<=100)
            {
                grade='O';
            }
            else if(this.average>=50 && this.average<=79)
            {
                grade='A';
            }
            else
            {
                grade='F';
            }
        }
        setGrade(grade);
    }
    public void setAverage(float average)
    {
        this.average=average;
    }
    public void setGrade(char grade)
    {
        this.grade=grade;
    }
    public float getAverage()
    {
        return this.average;
    }
    public char getGrade()
    {
        return this.grade;
    }
}
Next:
  1. String Concatenation
  2. Find Average Age
  3. Login

Leave a Comment