Skip to content

luc078.c

Problem Statement

Create a structure 'student' (Roll, Name, Dept, Course, Year). Write functions to print names by join year and print data by roll number.

Metadata

Property Detail
Author Amit Dutta amitdutta4255@gmail.com
Date 08 Feb 2026
License MIT License (See the LICENSE file for details)
Difficulty Intermediate (index: 4 / 10)

Concepts

Beta Feature

This concept detection system is still in beta and may occasionally show incorrect or incomplete results.

  • Array
  • Pointers
  • Iteration
  • Sorting (possible)
  • Recursion

Actions

Raw View on GitHub

You can print or save this file by opening Raw and using your browser.

Source Code

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct student
{
    int roll;
    char name[50];
    char dept[20];
    char course[20];
    int year;
};

void print_by_year(struct student *s, int n, int year);
void print_by_roll(struct student *s, int n, int roll);

int main()
{
    struct student data[450] = {
        {101, "Amit", "CS", "B.Sc", 2024},
        {102, "Rahul", "Physics", "B.Sc", 2024},
        {103, "Sneha", "CS", "M.Sc", 2023},
        {104, "Priya", "Maths", "B.Sc", 2025},
        {105, "Rohan", "CS", "B.Sc", 2024}
    };
    int n = 5; // Using 5 sample records
    int year, roll;

    printf("Enter year to list students: ");
    scanf("%d", &year);
    print_by_year(data, n, year);

    printf("\nEnter roll number to find student: ");
    scanf("%d", &roll);
    print_by_roll(data, n, roll);

    return 0;
}

void print_by_year(struct student *s, int n, int year)
{
    int i, found = 0;
    printf("Students joining in %d:\n", year);
    for (i = 0; i < n; i++)
    {
        if (s[i].year == year)
        {
            printf("- %s\n", s[i].name);
            found = 1;
        }
    }
    if (!found) printf("No students found for this year.\n");
}

void print_by_roll(struct student *s, int n, int roll)
{
    int i;
    for (i = 0; i < n; i++)
    {
        if (s[i].roll == roll)
        {
            printf("\n--- Student Details ---\n");
            printf("Roll: %d\nName: %s\nDept: %s\nCourse: %s\nYear: %d\n",
                   s[i].roll, s[i].name, s[i].dept, s[i].course, s[i].year);
            return;
        }
    }
    printf("Student with Roll %d not found.\n", roll);
}

Explanation

Explain with AI

Copy the prompt below and paste it into any AI assistant.

    You are explaining a C programming code to a beginner.

    STRICT RULES:

    - Only use the given code. Do NOT assume anything not present.

    - Do NOT add extra examples.

    - Keep explanation clear and short.

    - If something is unclear, say "Not clear from code".

    - Follow the exact format below. Do NOT change headings.

    FORMAT:

    [START]

    ## What it does

    (Explain the overall purpose in 1-2 sentences)

    ## Step-by-step

    (Explain how the code works in steps, simple language)

    ## Key Concepts

    (List concepts like loop, condition, function, etc.)

    ## Notes

    (Mention any limitations, errors, or assumptions)

    [END]

    CODE (luc078.c):

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    struct student
    {
        int roll;
        char name[50];
        char dept[20];
        char course[20];
        int year;
    };

    void print_by_year(struct student *s, int n, int year);
    void print_by_roll(struct student *s, int n, int roll);

    int main()
    {
        struct student data[450] = {
            {101, "Amit", "CS", "B.Sc", 2024},
            {102, "Rahul", "Physics", "B.Sc", 2024},
            {103, "Sneha", "CS", "M.Sc", 2023},
            {104, "Priya", "Maths", "B.Sc", 2025},
            {105, "Rohan", "CS", "B.Sc", 2024}
        };
        int n = 5; // Using 5 sample records
        int year, roll;

        printf("Enter year to list students: ");
        scanf("%d", &year);
        print_by_year(data, n, year);

        printf("\nEnter roll number to find student: ");
        scanf("%d", &roll);
        print_by_roll(data, n, roll);

        return 0;
    }

    void print_by_year(struct student *s, int n, int year)
    {
        int i, found = 0;
        printf("Students joining in %d:\n", year);
        for (i = 0; i < n; i++)
        {
            if (s[i].year == year)
            {
                printf("- %s\n", s[i].name);
                found = 1;
            }
        }
        if (!found) printf("No students found for this year.\n");
    }

    void print_by_roll(struct student *s, int n, int roll)
    {
        int i;
        for (i = 0; i < n; i++)
        {
            if (s[i].roll == roll)
            {
                printf("\n--- Student Details ---\n");
                printf("Roll: %d\nName: %s\nDept: %s\nCourse: %s\nYear: %d\n",
                       s[i].roll, s[i].name, s[i].dept, s[i].course, s[i].year);
                return;
            }
        }
        printf("Student with Roll %d not found.\n", roll);
    }