In: Computer Science
Write the basic JAVA code for the beginners and follow all the instructions listed
1. A year with 366 days is called a leap year. A year is a leap year if it is divisible by 4 (for e.g., 1980), except that it is not a leap year if it is also divisible by 100 (for e.g., 1900); however, it is a leap year if it is further divisible by 400 (for e.g., 2000).
Write a program that prompts the user to enter a year and displays whether that year is a leap year. The program should repeatedly prompt the user for a year until the user enters a sentinel value of -99 to quit the program. Note that you will need to use the mod function (%) to determine divisibility by a number. You CANNOT use the class GregorianCalendar and any of its methods.
2. Write a java program to print student’s total marks, percentage and grade. User input is given to enter number of subjects and then prompts user to enter marks. Calculate Total marks, Student Percentage, Student Grade.
Note:
1. Maximum marks for a subject is 100.
2. percentage = (totalMarks/(count*100)) * 100;
>=90 is Grade A
>=80 and <=89.99 is Grade B
>=70 and <=79.99 is Grade C
>=60 and <=69.99 is Grade D
< 60 is Fail
Tips: use scanner for inputs, use for loop (to iterate and store marks), use switch statement (to print grades).
Sample Output:
Enter Number of Subject(s): 3
Enter Marks of 3 Subject(s):
70
80
90
Total Marks : 240
Student Percentage: 80
Student Grade : B
Program 1:-
Screenshot of program code:-
Screenshot of output:-
Program code to copy:-
//Program to check whether a given year is leap year or
not
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
Scanner stdin = new
Scanner(System.in); // For standard input
int year;
do {
//Prompt &
read a year from user
System.out.print("Enter a
year <enter sentinel value -99 to quit the program>:
");
year =
stdin.nextInt();
if(year !=-99) {
//Checking if a year is
divisible by 4
if(year %
4 == 0) {
//Checking if a year is divisible by 100
if(year % 100 == 0) {
//Checking if a year is divisible by 400
if(year % 400 == 0)
System.out.println(year + " is a Leap Year");
else
System.out.println(year + " is not a Leap Year");
}
else
System.out.println(year + "
is a Leap Year");
}
else
System.out.println(year + " is not a Leap
Year");
}
}while(year!=-99);
}
}
Program 2:-
Screenshot of program code:-
Screenshot of output:-
Program code to copy:-
import java.util.Scanner;
public class StudentMarksGrade {
public static void main(String[] args) {
Scanner in = new
Scanner(System.in);
//Prompt & read Number of
Subject(s) from user
System.out.print("Enter Number of
Subject(s): ");
int count = in.nextInt();
double totalMarks = 0;
//Prompt the user to enter marks of subjects
System.out.println("Enter Marks of " + count + " Subject(s):
");
for(int i=1; i<=count; i++) {
//Read the marks of subjecy from user
double marks = in.nextDouble();
//Calculate total marks
totalMarks += marks;
}
//Calculate percentage
double percentage = (totalMarks/(count*100)) * 100;
//Display total marks & percentage
System.out.println("Total Marks: " + totalMarks);
System.out.println("Student Percentage: " + percentage);
//Determine grade
char grade;
if(percentage>=90)
grade = 'A';
else
if(percentage>=80)
grade = 'B';
else
if(percentage>=70)
grade = 'C';
else
if(percentage>=60)
grade = 'D';
else
grade = 'F';
//Display grade
switch(grade) {
case 'A': System.out.println("Student Grade : " +
grade);
break;
case 'B': System.out.println("Student Grade : " +
grade);
break;
case 'C': System.out.println("Student Grade : " +
grade);
break;
case 'D': System.out.println("Student Grade : " +
grade);
break;
case 'F': System.out.println("Student Grade :
Fail");
}
}
}