The below program shows how to use array and store student’s details and fetch details.
Here we are using for each loop which is used to iterate throughout entire array element.
- Example
import java.util.Scanner; class FindGrade { //get roll number and marks of student and calculate percentage and grade public static void main(String args[]) { int sub[],i,total=0; float per=0.0f; Scanner sc; String rNo; sub=new int[5]; sc=new Scanner(System.in); System.out.println("Enter Roll Number"); rNo=sc.next(); for(i=0;i<5;i++) { System.out.println("Enter Marks Of Subject "+(i+1)); sub[i]=sc.nextInt(); total=total+sub[i]; } per=total*100/500; System.out.println("\n***** Details Of Student *****\n"); System.out.println("\nRoll Number "+rNo); System.out.println("Total Marks Gained Is "+total); System.out.println("Percentage Gained Is "+per); if(per>90) System.out.println("Your Grade Is A+"); else if(per>80 && per<90) System.out.println("Your Grade Is A"); else if(per>70 && per<80) System.out.println("Your Grade Is B+"); else if(per>60 && per<70) System.out.println("Your Grade Is B"); else if(per>50 && per<60) System.out.println("Your Grade Is C+"); else if(per>40 && per<50) System.out.println("Your Grade Is C"); else System.out.println("Your Grade Is F"); } }
- OutPut