Solutions for Class 10 ICSE Logix Kips Computer Applications with BlueJ Java | IT Developer <?php echo $page_title; ?>
IT Developer

Input in Java

Chapter 6

Input in Java

Class 10 - Logix Kips ICSE Computer Applications with BlueJ


Share with a Friend

Java Program: Student Details & Percentage Calculation


Write a program in Java, using the Scanner methods, to read and display the following details:

Name - as a String data type
Roll Number - as an integer data type
Marks in 5 subjects - as a float data type

Compute and display the percentage of marks.

import java.util.Scanner;

 

public class StudentPercentage {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

 

        // Input student details

        System.out.print("Enter Student Name: ");

        String name = sc.nextLine();

 

        System.out.print("Enter Roll Number: ");

        int rollNo = sc.nextInt();

 

        float totalMarks = 0;

 

        System.out.println("Enter marks in 5 subjects:");

        for (int i = 1; i <= 5; i++) {

            System.out.print("Subject " + i + ": ");

            float marks = sc.nextFloat();

            totalMarks += marks;

        }

 

        // Calculate percentage

        float percentage = (totalMarks / 500) * 100;

 

        // Display output

        System.out.println("\n--- Student Details ---");

        System.out.println("Name: " + name);

        System.out.println("Roll Number: " + rollNo);

        System.out.println("Percentage: " + percentage + "%");

 

        sc.close();

    }

}

Output

 
SAMPLE INPUT : 
Enter Student Name: Rahul Sharma
Enter Roll Number: 25
Enter marks in 5 subjects:
Subject 1: 78
Subject 2: 85
Subject 3: 90
Subject 4: 88
Subject 5: 79

SAMPLE OUTPUT : 
--- Student Details ---
Name: Rahul Sharma
Roll Number: 25
Percentage: 84.0%

Explanation

  • The Scanner class is used to read input values.
  • nextLine() is used for reading the name (String).
  • nextInt() is used for reading the roll number.
  • nextFloat() is used for reading marks.
  • A for loop is used to read marks of 5 subjects.
  • Percentage is calculated using the formula:

Percentage = (Total Marks) / 500 × 100