190k views
7 votes
Write a java program for the following

A student will not be allowed to sit in exam if his/her attendance is less than 75% .
Take the following input from user
Number of classes held
Number of classes attended
And print
Percentage of class attended
Is student allowed to sit in exam or not .

User Oxymoron
by
5.7k points

1 Answer

11 votes

Answer:

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int classesHeld, classesAttended;

double percentage;

System.out.print("Enter number of classes held: ");

classesHeld = input.nextInt();

System.out.print("Enter number of classes attended: ");

classesAttended = input.nextInt();

percentage = (double) classesAttended / classesHeld;

if(percentage < 0.75)

System.out.println("You are not allowed to sit in exam");

else

System.out.println("You are allowed to sit in exam");

}

}

Step-by-step explanation:

Import the Scanner to be able to get input from the user

Create a Scanner object to get input

Declare the variables

Ask the user to enter classesHeld and classesAttended

Calculate the percentage, divide the classesAttended by classesHeld. You also need to cast the one of the variable to double. Otherwise, the result would be an int

Check the percentage. If it is smaller than 0.75 (75% equals 0.75), print that the student is not allowed. Otherwise, print that the student is allowed

User Perry Horwich
by
5.9k points