Java Programs


Students’ result Calculation

  • Do the following for the user-entered number of students:

- Find the average marks for a student of his marks in 3 subjects.
- Print whether he passed or failed. A student will fail if his average is less than 50.

Program

import java.util.Scanner;
  
public class StudentFor {

            private static Scanner sc;
            public static void main(String[] args) {
                        int[] sub1,sub2,sub3;
                        String[] name;
                        sc=new Scanner(System.in);
                        sub1=new int[10];
                        sub2=new int[10];
                        sub3=new int[10];
                        name=new String[10];
                        float[] avg=new float[20];
                        System.out.println("Enter the no. of students:");
                        int num=sc.nextInt();
                        for(int i=1;i<=num;i++) {
                                    System.out.println("Enter the Name of the student"+i+":");
                                    name[i]=sc.next();
                                    System.out.println("Enter the mark of "+name[i]+" in subject1:");
                                    sub1[i]=sc.nextInt();
                                    System.out.println("Enter the mark of "+name[i]+" in subject2:");
                                    sub2[i]=sc.nextInt();
                                    System.out.println("Enter the mark of "+name[i]+" in subject3:");
                                    sub3[i]=sc.nextInt();
                                    avg[i]=(sub1[i]+sub2[i]+sub3[i])/3;
                        }
                        for(int i=1;i<=num;i++) {
                                    if(avg[i]<50) {
                                                System.out.println(name[i]+" failed in the exam");
                                    }
                                    else {
                                                System.out.println(name[i]+" passed in the exam");
                                    }
                        }
            }

}

Output

Enter the no. of students:
2
Enter the Name of the student1:
Raju
Enter the mark of Raju in subject1:
45
Enter the mark of Raju in subject2:
50
Enter the mark of Raju in subject3:
60
Enter the Name of the student2:
Aisha
Enter the mark of Aisha in subject1:
34
Enter the mark of Aisha in subject2:
40
Enter the mark of Aisha in subject3:
45
Raju passed in the exam
Aisha failed in the exam


--------------------------------------------------------------------------------------------

Year is valid or not

  • Given a number, determine whether it is a valid year and if so, whether it is a leap year.

 Program

import java.util.Scanner;

public class ValidYear {
           
            private static Scanner sc;
            public static void main(String[] args) {
                        int year;
                        sc=new Scanner(System.in);
                        System.out.print("Enter a number:");
                        year=sc.nextInt();
                        if(year>0) {
                                    System.out.print(year+" is a Valid year");
                                    if((year%400==0)||(year%4==0&&year%100!=0)) {
                                                System.out.println(" and is a leap year too.");
                                    }
                                    else {
                                                System.out.println(", but it is not a leap year.");
                                    }
                        }
                        else {
                                    System.out.println(year+" is not a Valid year.");
                        }
            }

}

Output

Enter a number:2014
2014 is a Valid year, but it is not a leap year.

-----------------------------------------------------------------------------------------------

No comments:

Post a Comment