Answers for "A2"

0

A2

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in); // creating an object of scanner class to take input from the user
        int maximumMarks = 0, minimumMarks = 0, sum = 0; // declaring three integer variables and initializing then to 0
        int[] marks = new int[10]; // declaring an array of integers of size 10
        for (int i = 0; i < marks.length; i++) { // loop to take input from the user
            System.out.print("Enter the marks of student " + (i + 1) + ": "); // prompt the user to enter the marks of (i + 1) th student
            marks[i] = scan.nextInt(); // taking input from the user and copying it at i th position in array marks
        }
        for (int i = 0; i < marks.length; i++) { // loop to go through the array elements and find the maximum, minimum and sum of all elements
            sum = sum + marks[i]; // value at i th position in array marks is added to sum
            if (i == 0) { // when i is equal to 0 that means when the loop is executed for the first time
                maximumMarks = marks[i]; // value at i th location in array marks is copied in variable maximumMarks
                minimumMarks = marks[i]; // value at i th location in array marks is copied in variable minimumMarks
            } else { // for the rest of times when loop is executed
                if (marks[i] > maximumMarks) { // if the value at i th position in array marks is greater than value in variable maximumMarks
                    maximumMarks = marks[i]; // then that value is copied in variable maximumMarks
                } else if (marks[i] < minimumMarks) { // else if the value at i th position in array marks is less than value in variable maximumMarks
                    minimumMarks = marks[i]; // then that value is copied in variable minimumMarks
                }
            }
        }
        float average = (float) sum / 10; // average is calculated
        System.out.println("\nMaximum marks is: " + maximumMarks); // maximum marks is printed
        System.out.println("Minimum marks is: " + minimumMarks); // minimum marks is printed
        System.out.println("Average marks is: " + average); // average is printed
    }
}
Posted by: Guest on October-17-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language