Write a Java program to calculate the sum of marks stored in an array

  • Estimate Completion Time
    not found 30 minutes
  • Difficulty Level:

Solution

public class ClassName {
public static void main(String[] args) {
int[] marks = {85, 60, 75, 54, 43, 66, 87, 28, 89, 90};
		int sum = 0;
		for (int Marks : marks) 
		{
		sum =sum+ Marks;
		}
		System.out.println("The sum of marks is  " + sum);
}
}
Output
The sum of marks is  677

Explanation

  • Initialize and declare an array of marks. Initialize the variable “sum” of data type “int”.
  • The “for-each” loop is used to iterate the array. It accesses each element in the array and does addition using the “+” operator and stores in a variable “sum”.
  • Print the result using the println() statement.
QA Ads Banner

Proleed Academy

Proleed serves / offers professionally designed IT training courses
globally.

Copyright © 2023 - Proleed Academy | All Rights Reserved.