Chapter 4- Programming in C---->Structure and Union

 Structure and Union

 











Example of structure

#include <stdio.h>

int main()

{

    struct student

    {

    int r;

    char n[100];

    float p;

}s;

printf("Enter roll number, name and percentage");

scanf("%d%s%f", &s.r,s.n,&s.p);

printf("roll number-%d name-%s percentage-%f",s.r,s.n,s.p);

return 0;

}

 Example of Union

#include <stdio.h>

int main()

{

    union student

    {

    int r;

    char n[100];

    float p;

}s;

printf("Enter roll number, name and percentage");

scanf("%d%s%f", &s.r,s.n,&s.p);

printf("roll number-%d name-%s percentage-%f",s.r,s.n,s.p);

return 0;

}

 

1. Write a program to input roll number, name and class of 10 students and display the record in appropriate format using structure.

#include <stdio.h>

struct student

{

    int roll;

    char name[100];

    int classNo;

};

 

int main() {

    struct student s[10];

    int i;

    for(i = 0; i < 10; i++) {

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

        printf("Roll Number: ");

        scanf("%d", &s[i].roll);

        printf("Name: ");

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

        printf("Class: ");

        scanf("%d", &s[i].classNo);

    }

    printf("\n--- Student Records ---\n");

    printf("Roll\tName\tClass\n");

    for(i = 0; i < 10; i++) {

        printf("%d\t%s\t%d\n", s[i].roll, s[i].name, s[i].classNo);

    }

    return 0;

}

2. Write a program that reads Name and address of "n" employees and display the record in appropriate format using structure.

#include <stdio.h>

struct employee

{

        char name[100];

        char address[100];

};

 

int main() {

    struct employee s[10];

    int i,n;

    printf("enter number of input");

    scanf("%d",&n);

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

        printf("Enter details of employee %d:", i+1);

        printf("Name: ");

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

        printf("address: ");

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

    }

    printf("\n--- Employee Records ---\n");

    printf("Name \t address\n");

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

        printf("%s\t%s", s[i].name, s[i].address);

    }

    return 0;

}

3. Write a program to input Name, Post and Salary of "n" employees and display the record whose salary is greater than 40,000 in appropriate format using structure.

 

#include <stdio.h>

 

struct employee {

    char name[100];

    char post[100];

    float salary;

};

int main() {

    int n, i;

    printf("Enter number of employees: ");

    scanf("%d", &n);

    struct employee e[n];

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

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

        printf("Name: ");

        scanf("%s", e[i].name);   // single word only

        printf("Post: ");

        scanf("%s", e[i].post);   // single word only

        printf("Salary: ");

        scanf("%f", &e[i].salary);

    }

    printf("\n--- Employees with Salary more than  40000 ---\n");

    printf("Name\t\tPost\t\tSalary\n");

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

        if(e[i].salary > 40000) {

            printf("%-15s %-15s %.2f\n", e[i].name, e[i].post, e[i].salary);

        }

    }

    return 0;

}

4. Write a program to ask Name, class and address of students display the record whose name starts with "N" by using structure.

#include <stdio.h>

#include <string.h>

struct student {

    char name[100];

    int classNo;

    char address[200];

};

int main() {

    int n, i;

        printf("Enter number of students: ");

    scanf("%d", &n);

    getchar();

    struct student s[n];

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

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

        printf("Name: ");

        fgets(s[i].name, sizeof(s[i].name), stdin);

        s[i].name[strcspn(s[i].name, "\n")] = '\0';

        printf("Class: ");

        scanf("%d", &s[i].classNo);

        getchar();

        printf("Address: ");

        fgets(s[i].address, sizeof(s[i].address), stdin);

        s[i].address[strcspn(s[i].address, "\n")] = '\0';

    }

    printf("\n--- Students whose name starts with 'N' ---\n");

    printf("Name\t\tClass\tAddress\n");

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

        if(s[i].name[0] == 'N' || s[i].name[0] == 'n') {

            printf("%-15s %-7d %-30s\n", s[i].name, s[i].classNo, s[i].address);

        }

    }

    return 0;

}

5. Write a program to input Roll number, Name, and Address of students and display the records in ascending order by using structure.

#include <stdio.h>

#include <string.h>

 

struct student

{

    int roll;

    char name[100];

    char address[200];

};

int main()

{

    int n, i, j;

    struct student temp;

 

    printf("Enter number of students: ");

    scanf("%d", &n);

    getchar();

    struct student s[n];

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

       {

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

        printf("Roll Number: ");

        scanf("%d", &s[i].roll);

        getchar();

        printf("Name: ");

        fgets(s[i].name, sizeof(s[i].name), stdin);

        s[i].name[strcspn(s[i].name, "\n")] = '\0';

        printf("Address: ");

        fgets(s[i].address, sizeof(s[i].address), stdin);

        s[i].address[strcspn(s[i].address, "\n")] = '\0';

    }

    for(i = 0; i < n-1; i++)

    {

        for(j = i+1; j < n; j++)

        {

            if(s[i].roll > s[j].roll)

            {

                temp = s[i];

                s[i] = s[j];

                s[j] = temp;

            }

        }

    }

 

    printf("\n--- Student Records in Ascending Order of Roll ---\n");

    printf("Roll\tName\t\tAddress\n");

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

    {

        printf("%-5d %-15s %-30s\n", s[i].roll, s[i].name, s[i].address);

    }

 

    return 0;

}

6. Write a program to input Name and age of 15 students and count the number of students whose age is greater than 18 by using structure.

#include <stdio.h>

struct student

{

    char name[100];

    int age;

};

int main()

{

    struct student s[15];

    int i, count = 0;

    for(i = 0; i < 15; i++)

    {

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

        printf("Name: ");

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

        printf("Age: ");

        scanf("%d", &s[i].age);

        if(s[i].age > 18)

        {

            count++;

        }

    }

    printf("\nNumber of students with age > 18 = %d\n", count);

    return 0;

}

 7. Write a program to input Name, Address and age of "n" employees and display the record in appropriate format using structure.

#include <stdio.h>

#include <string.h>

struct employee

{

    char name[100];

    char address[200];

    int age;

};

 

int main()

{

    int n, i;

    printf("Enter number of employees: ");

    scanf("%d", &n);

    getchar();

    struct employee e[n];

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

    {

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

        printf("Name: ");

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

        printf("Address: ");

         scanf("%s", e[i].address);

        printf("Age: ");

        scanf("%d", &e[i].age);

        getchar();

    }

    printf("\n--- Employee Records ---\n");

    printf( "Name", "Address", "Age");

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

    {

        printf("%s %s %d\n", e[i].name, e[i].address, e[i].age);

    }

    return 0;

}

Popular posts from this blog

Computer