Student Example With Specific Fees


Explanation

An array of structure is used to store multiple student records. We can apply conditions on structure members to display only specific records according to the given requirement.

In this program, student details such as name, roll number, and fees are stored using an array of structures. The program checks the fees of each student and displays only those students whose fees are greater than 500.

How It Works?

  • Step 1: Create a structure named student with members name, rollno, and fees.
  • Step 2: Create an array of structure s[5] to store multiple student records.
  • Step 3: Accept details of three students using a for loop.
  • Step 4: Use another for loop to check each student's fees.
  • Step 5: If the student's fees are greater than 500, display that student's details.

Example Program


#include <stdio.h>
#include <conio.h>

struct student
{

    char name[20];

    int rollno, fees;

};


main()
{

    struct student s[5];

    int k;

    clrscr();


    for(k = 0; k <= 2; k++)

    {

        printf("Enter the student Name : ");

        scanf("%s", s[k].name);


        printf("\nEnter the Roll Number : ");

        scanf("%d", &s[k].rollno);


        printf("\nEnter the fees : ");

        scanf("%d", &s[k].fees);

    }


    printf("\nName \tRoll No. \tFees");


    for(k = 0; k <= 2; k++)

    {

        if(s[k].fees > 500)

        {

            printf("\n%s", s[k].name);

            printf("\t%d", s[k].rollno);

            printf("\t%d", s[k].fees);

        }

    }


    getch();

}

Output

Enter the student Name : Amit
Enter the Roll Number : 1
Enter the fees : 300

Enter the student Name : Rahul
Enter the Roll Number : 2
Enter the fees : 800

Enter the student Name : Neha
Enter the Roll Number : 3
Enter the fees : 1000

Name    Roll No.   Fees
Rahul    2       800
Neha     3       1000

Practice Exercises

Task 1: Write a C program using an array of structure to store details of 5 employees and display only those employees whose salary is greater than 30000.

Goal: Learn how to use conditions with structure members and display specific records from an array of structures.

💡 Hint: Create a structure named employee with members name, id, and salary. Accept multiple employee records using an array of structures. Use an if condition to check whether salary > 30000 before displaying employee details.
💡 Show Solution

#include <stdio.h>
#include <conio.h>

struct employee
{

    char name[20];

    int id, salary;

};


main()
{

    struct employee e[5];

    int k;

    clrscr();


    for(k = 0; k <= 2; k++)

    {

        printf("Enter Employee Name : ");

        scanf("%s", e[k].name);


        printf("\nEnter Employee ID : ");

        scanf("%d", &e[k].id);


        printf("\nEnter Salary : ");

        scanf("%d", &e[k].salary);

    }


    printf("\nName \tID \tSalary");


    for(k = 0; k <= 2; k++)

    {

        if(e[k].salary > 30000)

        {

            printf("\n%s", e[k].name);

            printf("\t%d", e[k].id);

            printf("\t%d", e[k].salary);

        }

    }


    getch();

}

Output
Enter Employee Name : Amit
Enter Employee ID : 101
Enter Salary : 25000

Enter Employee Name : Rahul
Enter Employee ID : 102
Enter Salary : 45000

Enter Employee Name : Neha
Enter Employee ID : 103
Enter Salary : 60000

Name    ID    Salary
Rahul    102    45000
Neha     103    60000
Explanation: The program stores employee details using an array of structures. Each employee's salary is checked using an if condition. Only employees having salary greater than 30000 are displayed.