import java.util.Scanner;public class CgpaToPercentageConverter {// Conversion factor (commonly used formula: Percentage = CGPA * 9.5)private static final double CONVERSION_FACTOR = 9.5;public static void main(String[] args) {// Create a Scanner object to read input from the userScanner scanner = new Scanner(System.in);// Prompt the user to enter their CGPASystem.out.print("Enter your CGPA: ");double cgpa = scanner.nextDouble();// Validate the input (CGPA should be between 0 and 10)if (cgpa < 0 || cgpa > 10) {System.out.println("Invalid CGPA! CGPA should be between 0 and 10.");} else {// Convert CGPA to percentagedouble percentage = cgpa * CONVERSION_FACTOR;// Display the resultSystem.out.printf("Your percentage is: %.2f%%\n", percentage);}// Close the scanner to avoid resource leakscanner.close();}}import java.util.Scanner; public class CgpaToPercentageConverter { // Conversion factor (commonly used formula: Percentage = CGPA * 9.5) private static final double CONVERSION_FACTOR = 9.5; public static void main(String[] args) { // Create a Scanner object to read input from the user Scanner scanner = new Scanner(System.in); // Prompt the user to enter their CGPA System.out.print("Enter your CGPA: "); double cgpa = scanner.nextDouble(); // Validate the input (CGPA should be between 0 and 10) if (cgpa < 0 || cgpa > 10) { System.out.println("Invalid CGPA! CGPA should be between 0 and 10."); } else { // Convert CGPA to percentage double percentage = cgpa * CONVERSION_FACTOR; // Display the result System.out.printf("Your percentage is: %.2f%%\n", percentage); } // Close the scanner to avoid resource leak scanner.close(); } }import java.util.Scanner; public class CgpaToPercentageConverter { // Conversion factor (commonly used formula: Percentage = CGPA * 9.5) private static final double CONVERSION_FACTOR = 9.5; public static void main(String[] args) { // Create a Scanner object to read input from the user Scanner scanner = new Scanner(System.in); // Prompt the user to enter their CGPA System.out.print("Enter your CGPA: "); double cgpa = scanner.nextDouble(); // Validate the input (CGPA should be between 0 and 10) if (cgpa < 0 || cgpa > 10) { System.out.println("Invalid CGPA! CGPA should be between 0 and 10."); } else { // Convert CGPA to percentage double percentage = cgpa * CONVERSION_FACTOR; // Display the result System.out.printf("Your percentage is: %.2f%%\n", percentage); } // Close the scanner to avoid resource leak scanner.close(); } }
Enter fullscreen mode Exit fullscreen mode
Explanation of the Code:
Importing the Scanner Class:
We import the Scanner class from the java.util package to read input from the user.
Defining the Conversion Factor:
We define a constant CONVERSION_FACTOR with a value of 9.5. This is a commonly used factor to convert CGPA to percentage.
Main Method:
The main method is the entry point of the program.
Creating a Scanner Object:
We create a Scanner object named scanner to read input from the user.
Prompting the User for Input:
We prompt the user to enter their CGPA using System.out.print.
Reading the CGPA Input:
We use scanner.nextDouble() to read the CGPA value entered by the user.
Input Validation:
We check if the entered CGPA is within the valid range (0 to 10). If not, we display an error message.
*Calculating the Percentage:
*
If the CGPA is valid, we calculate the percentage by multiplying the CGPA by the conversion factor (9.5).
Displaying the Result:
We use System.out.printf to display the calculated percentage with two decimal places.
Closing the Scanner:
Finally, we close the Scanner object to free up resources.
How to Run the Program:
Copy the code into a file named CgpaToPercentageConverter.java.
Compile the program using the command:
javac CgpaToPercentageConverter.javajavac CgpaToPercentageConverter.javajavac CgpaToPercentageConverter.java
Enter fullscreen mode Exit fullscreen mode
Run the program using the command:
java CgpaToPercentageConverterjava CgpaToPercentageConverterjava CgpaToPercentageConverter
Enter fullscreen mode Exit fullscreen mode
Enter your CGPA when prompted, and the program will display the corresponding percentage.
Example Output:
Enter your CGPA: 8.5Your percentage is: 80.75%Enter your CGPA: 8.5 Your percentage is: 80.75%Enter your CGPA: 8.5 Your percentage is: 80.75%
Enter fullscreen mode Exit fullscreen mode
At the end, you will get the CGPA to Percentage converter like https://cgpaintopercentages.com/
原文链接:CGPA to Percentage Converter Using Java (Basic But Accurate)
暂无评论内容