💻 Unit 4 – Part C (Descriptive Q&A)

Programming in C

⬅ Back to Unit 4

Ad Space

Part C: Structures and Unions

85. Explain in detail the control structures in C (decision-making and looping statements) with examples.

Control structures alter the normal sequential flow of execution in a program. They are categorized into decision-making and looping statements.

1. Decision-Making (Conditional) Statements:

These statements execute a block of code only if a certain condition is true.

2. Looping (Iterative) Statements:

These statements execute a block of code repeatedly as long as a condition is true.

86. Create a student information system, i) Declare a structure Student... ii) Calculate the GPA and CGPA...

This program defines a Student structure, calculates percentage (as a proxy for GPA, since a true GPA/CGPA calculation system is complex and specific), and demonstrates its usage.


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

// i) Declare the structure
struct Student {
    char name[100];
    int age;
    long roll_number;
    int marks[5]; // Marks for 5 subjects
    float percentage;
    float gpa; // Using percentage as a proxy
};

// Function to calculate GPA/Percentage
void calculateGPA(struct Student *s) {
    int total_marks = 0;
    int num_subjects = 5;
    for (int i = 0; i < num_subjects; i++) {
        total_marks += s->marks[i];
    }
    
    s->percentage = (float)total_marks / num_subjects;
    
    // ii) Calculate GPA (on a 10-point scale, as percentage/10)
    // This is a simplification. Real GPA/CGPA involves credits.
    s->gpa = s->percentage / 10.0;
}

int main() {
    struct Student s1;

    // --- Get Student Details ---
    strcpy(s1.name, "Arun Kumar");
    s1.age = 19;
    s1.roll_number = 12345;

    printf("Enter marks for 5 subjects for %s:\n", s1.name);
    for (int i = 0; i < 5; i++) {
        printf("Subject %d: ", i + 1);
        scanf("%d", &s1.marks[i]);
    }

    // --- Calculate GPA/Percentage ---
    calculateGPA(&s1);

    // --- Display Student Details ---
    printf("\n--- Student Information ---\n");
    printf("Name: %s\n", s1.name);
    printf("Age: %d\n", s1.age);
    printf("Roll Number: %ld\n", s1.roll_number);
    printf("Percentage: %.2f%%\n", s1.percentage);
    printf("GPA (out of 10): %.2f\n", s1.gpa);
    // CGPA is an average of GPAs over multiple semesters.
    // We can only show GPA for this single set of marks.
    printf("CGPA: (Requires marks from previous semesters)\n");
    
    return 0;
}

87. Define a structure called student... Write a c program to read the details for 30 students and calculate the percentage then display...

This program defines a student structure and uses an array of structures to store records for 3 students (using 3 instead of 30 for easier testing).


#include <stdio.h>

#define NUM_STUDENTS 3 // Using 3 for testing. Change to 30 for full program.
#define NUM_SUBJECTS 5

struct student {
    char name[100];
    long reg_no;
    int marks[NUM_SUBJECTS];
    float percentage;
};

int main() {
    struct student s[NUM_STUDENTS];
    int i, j;

    // --- Read Details for 30 Students ---
    for (i = 0; i < NUM_STUDENTS; i++) {
        printf("\n--- Enter Details for Student %d ---\n", i + 1);
        
        printf("Enter Name: ");
        scanf(" %[^\n]", s[i].name); // Read string with spaces
        
        printf("Enter Register No.: ");
        scanf("%ld", &s[i].reg_no);

        printf("Enter Marks for 5 Subjects:\n");
        int total_marks = 0;
        for (j = 0; j < NUM_SUBJECTS; j++) {
            printf("Subject %d: ", j + 1);
            scanf("%d", &s[i].marks[j]);
            total_marks += s[i].marks[j];
        }
        
        // --- Calculate Percentage ---
        s[i].percentage = (float)total_marks / NUM_SUBJECTS;
    }

    // --- Display All Student Details ---
    printf("\n\n*** Student Details ***\n");
    printf("--------------------------------------------\n");
    printf("Reg. No.\tName\t\tPercentage\n");
    printf("--------------------------------------------\n");

    for (i = 0; i < NUM_STUDENTS; i++) {
        printf("%ld\t\t%s\t\t%.2f%%\n", s[i].reg_no, s[i].name, s[i].percentage);
    }
    printf("--------------------------------------------\n");

    return 0;
}

Ad Space