Write a C# program to accept and display ‘n’ student’s details such as Roll No, Name, and marks in three subjects, using a class. Display the percentage of each student.

Answer:

using System;

namespace StudentDetails
{
    // Class to store student details
    class Student
    {
        public int RollNo;
        public string Name;
        public float Marks1, Marks2, Marks3;

        // Method to calculate percentage
        public float CalculatePercentage()
        {
            float totalMarks = Marks1 + Marks2 + Marks3;
            return (totalMarks / 300) * 100;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the number of students:");
            int n = int.Parse(Console.ReadLine()); // Accept number of students

            // Array to store student details
            Student[] students = new Student[n];

            // Accepting details of each student
            for (int i = 0; i < n; i++)
            {
                students[i] = new Student();
                Console.WriteLine("\nEnter details for student" + (i + 1) + ":");

                Console.Write("Roll No: ");
                students[i].RollNo = int.Parse(Console.ReadLine());

                Console.Write("Name: ");
                students[i].Name = Console.ReadLine();

                Console.Write("Marks in Subject 1: ");
                students[i].Marks1 = float.Parse(Console.ReadLine());

                Console.Write("Marks in Subject 2: ");
                students[i].Marks2 = float.Parse(Console.ReadLine());

                Console.Write("Marks in Subject 3: ");
                students[i].Marks3 = float.Parse(Console.ReadLine());
            }

            // Displaying the details of each student
            Console.WriteLine("\nStudent Details and Percentages:");

            for (int i = 0; i < n; i++)
            {
                Console.WriteLine("\nStudent of " + (i+1) + ":" );
                Console.WriteLine("Roll No:" + students[i].RollNo);
                Console.WriteLine("Name:" + students[i].Name);
                Console.WriteLine("Marks in Subject 1:" + students[i].Marks1);
                Console.WriteLine("Marks in Subject 2:" + students[i].Marks2);
                Console.WriteLine("Marks in Subject 3:" + students[i].Marks3);
                Console.WriteLine("Percentage:" + students[i].CalculatePercentage());
                
            }
            Console.ReadLine();
        }
    }
}
Scroll to Top