C Program to Store Information of Students Using Structure

Method 1:-
#include<stdio.h>
#include<conio.h>
struct student {
    char name[20];
    int roll;
    float marks;
} s[5];

int main() {
    int i,n;

  printf("Enter how many records you want to store:-");
  scanf("%d",&n);
    // storing information
    for (i = 0; i < n; ++i) {
        s[i].roll = i + 1;
        printf("\nEnter roll no: ");
        scanf("%d",&s[i].roll);
        printf("Enter first name: ");
        scanf("%s", s[i].name);
        printf("Enter marks: ");
        scanf("%f", &s[i].marks);
    }
    printf("Displaying Information:\n\n");

    // displaying information
    for (i = 0; i < n; ++i) {
        printf("\nRoll number: %d\n", i + 1);
        printf("First name: ");
        puts(s[i].name);
        printf("Marks: %.1f", s[i].marks);
        printf("\n");
    }
    return 0;
}
output:-
Enter how many records you want to store:-2

Enter roll no: 1
Enter first name: ram
Enter marks: 100

Enter roll no: 2
Enter first name: rahul
Enter marks: 90
Displaying Information:


Roll number: 1
First name: ram
Marks: 100.0

Roll number: 2
First name: rahul
Marks: 90.0

Method 2:-

#include <stdio.h>

struct student
{
    int rollno;
    char name[80];
    int marks;
};

void accept(struct student[], int);
void display(struct student[], int);
void search(struct student[], int, int);
int findMax(struct student[], int);
void toppers(struct student[], int);

int main()
{
    struct student data[20];
    int n, choice, rollno;

    printf("Number of records you want to enter? : ");
    scanf("%d", &n);
    accept(data, n);
    do
    {

        printf("\nResult Menu :\n");
        printf("Press 1 to display all records.\n");
        printf("Press 2 to search a record.\n");
        printf("Press 3 to display toppers names.\n");
        printf("Press 0 to exit\n");
        printf("\nEnter choice(0-3) : ");
        scanf("%d", &choice);
        switch (choice)
        {
            case 1:
                display(data, n);
                break;
            case 2:
                printf("Enter roll number to search : ");
                scanf("%d", &rollno);
                search(data, n, rollno);
                break;
            case 3:
                toppers(data, n);
        }
    }
    while (choice != 0);

    return 0;
}

void accept(struct student list[80], int s)
{
    int i;
    for (i = 0; i < s; i++)
    {
        printf("\nEnter data for Record #%d", i + 1);

        printf("\nEnter rollno : ");
        scanf("%d", &list[i].rollno);
        fflush(stdin);
        printf("Enter name : ");
        gets(list[i].name);

        printf("Enter marks : ");
        scanf("%d", &list[i].marks);
    } 
}

void display(struct student list[80], int s)
{
    int i;

    printf("\n\nRollno\tName\tMarks\n");
    for (i = 0; i < s; i++)
    {
        printf("%d\t%s\t%d\n", list[i].rollno, list[i].name, list[i].marks);
    } 
}

void search(struct student list[80], int s, int number)
{
    int i;

    for (i = 0; i < s; i++)
    {
        if (list[i].rollno == number)
        {
            printf("Rollno : %d\nName : %s\nMarks : %d\n", list[i].rollno,
                list[i].name, list[i].marks);
            return ;
        } 
    }
    printf("Record not Found\n");
}

int findMax(struct student list[], int s)
{
    int i, max;

    max = list[0].marks;
    for (i = 1; i < s; i++)
    {
        if (list[i].marks > max)
        {
            max = list[i].marks;
        } 
    }
    return max;
}

void toppers(struct student list[], int s)
{
    int i;
    for (i = 0; i < s; i++)
    {
        if (list[i].marks == findMax(list, s))
        {
            printf("%s\n", list[i].name);
        } 
    }
}

output:-

Number of records you want to enter? : 2


Enter data for Record #1

Enter rollno : 1

Enter name : rahul

Enter marks : 100


Enter data for Record #2

Enter rollno : 2

Enter name : ram

Enter marks : 90


Result Menu :

Press 1 to display all records.

Press 2 to search a record.

Press 3 to display toppers names.

Press 0 to exit


Enter choice(0-3) : 1



Rollno  Name    Marks

1       rahul   100

2       ram     90


Result Menu :

Press 1 to display all records.

Press 2 to search a record.

Press 3 to display toppers names.

Press 0 to exit


Enter choice(0-3) : 2

Enter roll number to search : 1

Rollno : 1

Name : rahul

Marks : 100


Result Menu :

Press 1 to display all records.

Press 2 to search a record.

Press 3 to display toppers names.

Press 0 to exit


Enter choice(0-3) : 3

rahul


Result Menu :

Press 1 to display all records.

Press 2 to search a record.

Press 3 to display toppers names.

Press 0 to exit


Enter choice(0-3) : 0

Post a Comment

If you have any doubts, Please let me know
Thanks!

Previous Post Next Post