Write a java program to store marks of five subjects in an array and
then calculate the average marks.
Answers
The given problem is solved using language - Java.
import java.util.Scanner;
public class Java{
public static void main(String s[]){
int a[]=new int[5];
Scanner sc=new Scanner(System.in);
double av=0;
System.out.println("Enter the marks of 5 subjects..");
for(int i=0;i<a.length;i++){
System.out.print(">> ");
a[i]=sc.nextInt();
av+=a[i];
}
av/=5.0;
System.out.println("Average of the marks obtained: "+av);
sc.close();
}
}
- Accept the marks of five subjects from the user.
- Add the marks and divide their sum by 5. Store the result in variable, say 'av'.
- Display 'av'.
See attachment for output.
Answer:
Program:-
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
String name;
int sum=0;
double avg=0;
int a[]=new int[5];
System.out.println("Enter the name of the student:");
name=in.nextLine();
System.out.println("Enter the marks of the student");
for(int i=0;i<a.length;i++)
{
a[i]=in.nextInt();
sum=sum+a[i];
}
avg=sum/(double)5;
System.out.println("The average of the student:"+avg);
}
}