Write a program to input marks in 3 subjects and find the total and average. Print the
mes“Write a program to input marks in 3 subjects and find the total and average. Print the message ‘Good’ if the average is above 80 else print the message ‘Work Hard’.”sage ‘Good’ if the average is above 80 else print the message ‘Work Hard’.
Answers
Answer:
import java.util.Scanner;
public class Answer
{
public static void main (String []args)
{
int marks,total=0,avg;
Scanner sc=new Scanner(System.in); //Initialsing the scanner class
System.out.println();
System.out.println("Enter the marks in three subjects: ");
for(int i=1;i<=3;i++) //For loop used to enter marks in 3 subjects
{
marks=sc.nextInt();
total=total+marks;
}
avg=total/3;
System.out.println("Total marks obtained = " + total);
System.out.println("Average marks obtained = " + avg);
if(avg>=80)
System.out.println("\'Good\'");
else
System.out.println("\'Work hard\'");
/*
* I have used this character in the print statements \'.
* This character is known as an escape character.
* These are non-graphic characters used as commands to direct the cursor while printing.
* In this case \' prints the single quotes character.
*/
}
}