hello and welcome to all-coding.com, today in this article we will teach you a java Program to Find Sum and Average of 10 Students Using Arrays. previously we have done an Easy C Program to Find Sum and Average of 10 Students Using Arrays so must check that as well if you are a student of a C programming language as well.
Java Program to Find Sum and Average of 10 Students Using Arrays
in this java program, we will Compute sum and average of array elements in Java:-
- Import the package java. util.Scanner; in which we will use Scanner class to give input to our program at run time.
- we will make public class sumAndAvgOf10studentsUsingArrays{}, remember one thing the name of the class and the name of your java file should be the same
- The Main method public static void main(String[] args){] in which our program will be written
-
int I, size, sum = 0; float avg; these are the variable we will be using in our program
- we will display a statement System.out.print(“enter the size of the array: “);
- Scanner sc= new Scanner(System. in); Here, we have created an object of Scanner named sc.
- size=sc.nextInt(); // here we have initialized the size variable.
- int marks[] = new int[size]; // declaration of array in java.
- System.out.print(“enter marks of students : “);
-
for(i=0;i<size;i++){marks[i] =sc.nextInt(); } // initialization of array in java using for loop
-
for(i=0;i<size;i++){sum=sum+marks[i];} // Sum and average in Java
- avg=sum/size;
- System.out.print(“average = “+avg);}
Below is the implementation of java Program to Find Sum and Average of 10 Students Using Arrays
//Java program to calculate the average value of array elements //Write a program to initialize an integer array and print the sum and average of the array wipro import java.util.Scanner; public class sumAndAvgOf10studentsUsingArrays{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int i,size,sum = 0; float avg; System.out.print("enter size of array : "); size = sc.nextInt(); int marks[] = new int[size]; System.out.print("enter marks of students : "); for(i = 0 ; i < size ; i++){ marks[i] = sc.nextInt(); } for(i = 0 ; i < size ; i++){ sum = sum + marks[i]; } avg = sum/size; System.out.print("average = "+avg); } }
Output :