#include <stdio.h>


// Define a structure tag population with fields men and women

struct population {

    int men;

    int women;

};


// Create a structure within a structure using state and population

struct state {

    char name[50]; // Assuming the state name won't exceed 50 characters

    struct population pop;

};


int main() {

    struct state s;


    // Read data

    printf("Enter state name: ");

    scanf("%s", s.name);

    

    printf("Enter number of men: ");

    scanf("%d", &s.pop.men);

    

    printf("Enter number of women: ");

    scanf("%d", &s.pop.women);


    // Display data

    printf("\nState: %s\n", s.name);

    printf("Population:\n");

    printf("Men: %d\n", s.pop.men);

    printf("Women: %d\n", s.pop.women);


    return 0;

}


#include <stdio.h>


struct Book {

    char title[100];

    char author[100];

    int pages;

    float cost;

};


int main() {

    struct Book book, *ptr;


    // Assigning the address of the structure variable to the pointer

    ptr = &book;


    // Getting details of the book

    printf("Enter the title of the book: ");

    scanf("%99[^\n]%*c", ptr->title); // Read until newline character

    

    printf("Enter the name of the author: ");

    scanf("%99[^\n]%*c", ptr->author); // Read until newline character

    

    printf("Enter the number of pages: ");

    scanf("%d", &ptr->pages);

    

    printf("Enter the cost of the book: ");

    scanf("%f", &ptr->cost);


    // Displaying the content using the pointer to the structure

    printf("\nBook Details:\n");

    printf("Title: %s\n", ptr->title);

    printf("Author: %s\n", ptr->author);

    printf("Pages: %d\n", ptr->pages);

    printf("Cost: $%.2f\n", ptr->cost);


    return 0;

}


#include <stdio.h>


#define MAX_SUBJECTS 5

#define MAX_NAME_LENGTH 100

#define MAX_REG_LENGTH 20


// Define structure for student

struct Student {

    char name[MAX_NAME_LENGTH];

    char regNumber[MAX_REG_LENGTH];

    int marks[MAX_SUBJECTS];

    int total;

    float average;

    char grade;

};


// Function to calculate total marks

int calculateTotal(int marks[], int numSubjects) {

    int total = 0;

    for (int i = 0; i < numSubjects; i++) {

        total += marks[i];

    }

    return total;

}


// Function to calculate average marks

float calculateAverage(int total, int numSubjects) {

    return (float)total / numSubjects;

}


// Function to calculate grade

char calculateGrade(float average) {

    if (average >= 90) {

        return 'A';

    } else if (average >= 80) {

        return 'B';

    } else if (average >= 70) {

        return 'C';

    } else if (average >= 60) {

        return 'D';

    } else {

        return 'F';

    }

}


int main() {

    int n;

    printf("Enter the number of students: ");

    scanf("%d", &n);


    struct Student students[n];


    // Input data for each student

    for (int i = 0; i < n; i++) {

        printf("\nEnter details for student %d:\n", i + 1);

        printf("Name: ");

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

        printf("Register Number: ");

        scanf("%s", students[i].regNumber);

        printf("Enter marks for %d subjects:\n", MAX_SUBJECTS);

        for (int j = 0; j < MAX_SUBJECTS; j++) {

            printf("Mark for Subject %d: ", j + 1);

            scanf("%d", &students[i].marks[j]);

        }


        // Calculate total marks and average

        students[i].total = calculateTotal(students[i].marks, MAX_SUBJECTS);

        students[i].average = calculateAverage(students[i].total, MAX_SUBJECTS);


        // Calculate grade

        students[i].grade = calculateGrade(students[i].average);

    }


    // Print mark sheets

    printf("\n\nMark Sheets:\n");

    for (int i = 0; i < n; i++) {

        printf("\nStudent %d\n", i + 1);

        printf("Name: %s\n", students[i].name);

        printf("Register Number: %s\n", students[i].regNumber);

        printf("Marks:\n");

        for (int j = 0; j < MAX_SUBJECTS; j++) {

            printf("Subject %d: %d\n", j + 1, students[i].marks[j]);

        }

        printf("Total: %d\n", students[i].total);

        printf("Average: %.2f\n", students[i].average);

        printf("Grade: %c\n", students[i].grade);

    }


    return 0;

}

Comments

Popular posts from this blog