C program for Displaying 'N' Student details in Tabular form (Structure)

#include <stdio.h>

void line()
{
    int i;
    for(i=0;i<50;i++)
        printf("-");
}

struct Student {
    int rollNo;
    char name[50];
    char branch[20];
    float marks;
};

int main() {
    int n, i;

    printf("Enter number of students: ");
    scanf("%d", &n);

    struct Student s[n]; // Array of structures

    // Input student data
    for(i = 0; i < n; i++) {
        printf("\nEnter details of student %d:\n", i + 1);
        printf("Roll No: ");
        scanf("%d", &s[i].rollNo);
        printf("Name: ");
        scanf(" %[^\n]", s[i].name); // reads string with spaces
        printf("Branch: ");
        scanf("%s", s[i].branch);
        printf("Marks: ");
        scanf("%f", &s[i].marks);
    }

    // Display in tabular form
    line();
    printf("\n %-8s %-20s %-10s %-6s \n", "Roll No", "Name", "Branch", "Marks");
    line();
    for(i = 0; i < n; i++) {
        printf("\n %-8d %-20s %-10s %-6.2f \n",
               s[i].rollNo, s[i].name, s[i].branch, s[i].marks);
    }
    line();

    return 0;
}

Comments

Popular posts from this blog

Single Program Using All Automatic String Functions

C program for subtraction of two matrices (2D Arrays)

C program for Multiplication of Two matrices