Practical – List – 4

 
Practical – List – 4






1. WAP to demonstrate initialisation of structure.

Program:

#include <stdio.h>

#include <conio.h>

 

struct Person

{

    char name[50];

    int age;

    float height;

};

 

void main()

{

    clrscr();

    struct Person person1 = {"John", 25, 6.0};

 

    printf("Details of person1:\n");

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

    printf("Age: %d\n", person1.age);

    printf("Height: %.2f\n", person1.height);

 

    getch();

}

 

Output:

Details of person1:

Name: John 

Age: 25    

Height: 6.00

 


Please follow me for more practicals....



2. WAP to take a input from user and print the following details of book using structure Book name, Author name, Publisher name, pages.

Program:

#include <stdio.h>

#include <conio.h>

 

struct Book {

    char name[100];

    char author[100];

    char publisher[100];

    int pages;

};

 

void main() {

    clrscr();

    struct Book book;

 

    printf("Book Name: ");

    scanf("%99[^\n]", book.name); getchar();

 

    printf("Author's name: ");

    scanf("%99[^\n]", book.author); getchar();

 

    printf("Publisher's name: ");

    scanf("%99[^\n]", book.publisher); getchar();

 

    printf("Number of pages: ");

    scanf("%d", &book.pages);

 

    printf("\nDetails of the book:\n");

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

    printf("Author: %s\n", book.author);

    printf("Publisher: %s\n", book.publisher);

    printf("Number of pages: %d\n", book.pages);

 

    getch();

}

 

Output:

Book Name: Atomic Habbit

Author's name: Jems Clear

Publisher's name: Random House

Number of pages: 320

 

Details of the book:

Name: Atomic Habbit

Author: Jems Clear

Publisher: Random House

Number of pages: 320

 

3. WAP to declare & initialiser array of structure.

Program:

#include <stdio.h>

#include <conio.h>

struct Book

{

    char name[100];

    char author[100];

    char publisher[100];

    int pages;

};

 

void main()

{

    clrscr();

    struct Book books[3] = {

        {"Atomic Habits", "James Clear", "Random House", 200},

        {"You Can Win", "Shiv Khera", "Macmillan", 300},

        {"Secret", "Rhonda Byrne", "Simon & Schuster", 250}    

    };

 

    printf("Details of books:\n");

    for (int i = 0; i < 3; i++)

    {

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

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

        printf("Author: %s\n", books[i].author);

        printf("Publisher: %s\n", books[i].publisher);

        printf("Number of pages: %d\n", books[i].pages);

        printf("\n");

    }

 

    getch();

}

 

 

Output:

Details of books:

Book 1:

Name: Atomic Habits   

Author: James Clear   

Publisher: Random House

Number of pages: 200  

 

Book 2:

Name: You Can Win     

Author: Shiv Khera    

Publisher: Macmillan  

Number of pages: 300  

 

Book 3:

Name: Secret

Author: Rhonda Byrne

Publisher: Simon & Schuster

Number of pages: 250

 


Please follow me for more practicals....



4. WAP to read and print the value of ID, name, percentage of 5 students using array of structure.

Program:

#include <stdio.h>

#include <conio.h>

 

struct Student

{

    int id;

    char name[50];

    float percentage;

};

 

void main()

{

    clrscr();

    struct Student students[5];

 

    printf("Enter details of 5 students:\n");

    for (int i = 0; i < 5; i++)

    {

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

        printf("ID: ");

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

 

        printf("Name: ");

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

 

        printf("Percentage: ");

        scanf("%f", &students[i].percentage);

    }

 

    printf("\nDetails of students:\n");

 

    for (int i = 0; i < 5; i++)

    {

        printf("%d\t|", students[i].id);

    }

    printf("\n");

    for (int i = 0; i < 5; i++)

    {

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

    }

    printf("\n");

    for (int i = 0; i < 5; i++)

    {

        printf("%.2f\t|", students[i].percentage);

    }

 

    printf("\n");

 

    getch();

}

 

 

Output:

Enter details of 5 students:

 

Student: 1

ID: 1

Name: Mehdi

Percentage: 96

 

Student: 2

ID: 2

Name: Rahul

Percentage: 88

 

Student: 3

ID: 3

Name: Arjun

Percentage: 76

 

Student: 4

ID: 4

Name: Neha

Percentage: 66

 

Student: 5

ID: 5

Name: Anil

Percentage: 82

 

Details of students:

1             |2           |3           |4           |5           |

Mehdi  |Rahul |Arjun |Neha  |Anil     |

96.00     |88.00   |76.00 |66.00 |82.00 |

 

5. WAP to demonstrate the use of pointer to structure.

Program:

#include <stdio.h>

#include <conio.h>

 

struct Student

{

    int id;

    char name[50];

    float percentage;

};

 

int main()

{

    clrscr();

    struct Student student1 = {1, "Mehdiali", 98.6};

 

    struct Student *ptr;

 

    ptr = &student1;

 

    printf("Accessed using pointer:\n");

    printf("ID: %d\n", ptr->id);

    printf("Name: %s\n", ptr->name);

    printf("Percentage: %.2f\n", ptr->percentage);

 

    getch();

}

 

 

Output:

Accessed using pointer:

ID: 1

Name: Mehdiali

Percentage: 98.60

 


Please follow me for more practicals....



6. WAP to demonstrate the use of union.

Program:

#include <stdio.h>

#include <conio.h>

#include <string.h>

 

union Data

{

    int integer;

    float floating_point;

    char string[20];

};

 

int main()

{

    union Data data;

 

    data.integer = 10;

    printf("Integer: %d\n", data.integer);

 

    data.floating_point = 3.14;

    printf("Floating Point: %.2f\n", data.floating_point);

 

    strcpy(data.string, "Hello");

    printf("String: %s\n", data.string);

    getch();

}

 

 

Output:

Integer: 10

Floating Point: 3.14

String: Hello


 

Please follow me for more practicals....




7. WAP to search on element from array of structure.

Program:

#include <stdio.h>

#include <string.h>

 

struct Student

{

    int id;

    char name[50];

    float percentage;

};

 

int searchStudentByID(struct Student students[], int size, int searchID)

{

    for (int i = 0; i < size; i++)

    {

        if (students[i].id == searchID)

        {

            return i;

        }

    }

    return -1;

}

 

int main()

{

    clrscr();

    struct Student students[5] = {

        {1, "Mehdi", 98.6},

        {2, "Arjun", 88.7},

        {3, "Anil", 76.8},

        {4, "Neha", 75.6},

        {5, "Rahul", 79.6}};

 

    int searchID;

    printf("Enter the ID to search: ");

    scanf("%d", &searchID);

 

    int index = searchStudentByID(students, 5, searchID);

 

    if (index != -1)

    {

        printf("\nDetails of the student:\n");

        printf("ID: %d\n", students[index].id);

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

        printf("Percentage: %.2f\n", students[index].percentage);

    }

    else

    {

        printf("Student with ID %d not found.\n", searchID);

    }

 

    return 0;

}

 

 

Output:

Enter the ID to search: 1

 

Details of the student:

ID: 1

Name: Mehdi

Percentage: 98.60

 

 

Please follow me for more practicals....



8. WAP to short element of array of structure.

Program:

#include <stdio.h>

#include <string.h>

 

struct Student

{

    int id;

    char name[50];

    float percentage;

};

 

void sortStudentsByPercentage(struct Student students[], int size)

{

    struct Student temp;

    for (int i = 0; i < size - 1; i++)

    {

        for (int j = 0; j < size - i - 1; j++)

        {

            if (students[j].percentage > students[j + 1].percentage)

            {

                temp = students[j];

                students[j] = students[j + 1];

                students[j + 1] = temp;

            }

        }

    }

}

 

int main()

{

    clrscr();

    struct Student students[5] = {

        {101, "Arjun", 85.5},

        {102, "Anil", 75.2},

        {103, "Mehdi", 90.0},

        {104, "Neha", 82.7},

        {105, "Rahul", 88.3}};

 

    printf("Details before sorting:\n");

    for (int i = 0; i < 5; i++)

    {

        printf("%d\t %s\t %.2f\n", students[i].id, students[i].name, students[i].percentage);

    }

 

    sortStudentsByPercentage(students, 5);

 

    printf("\nDetails after sorting:\n");

    for (int i = 0; i < 5; i++)

    {

        printf("%d\t %s\t %.2f\n", students[i].id, students[i].name, students[i].percentage);

    }

 

    return 0;

}

 

 

Output:

Details before sorting:

101      Arjun   85.50

102      Anil    75.20

103      Mehdi   90.00

104      Neha    82.70

105      Rahul   88.30

 

Details after sorting:

102      Anil    75.20

104      Neha    82.70

101      Arjun   85.50

105      Rahul   88.30

103      Mehdi   90.00

 

 

Please follow me for more practicals....



9. WAP to declare a following structure member name, age, height, weight, read all the members of structure of 10 person & find list of persons with all related data weight > 50 and print the same with suitable height > 40.

Program:

#include <stdio.h>

#include <conio.h>

 

struct Person

{

    char name[50];

    int age;

    float height;

    float weight;

};

 

int main()

{

    clrscr();

    struct Person persons[10];

 

    printf("Enter details of 10 persons:\n");

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

    {

        printf("\nName: ");

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

        printf("Age: ");

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

        printf("Height: ");

        scanf("%f", &persons[i].height);

        printf("Weight: ");

        scanf("%f", &persons[i].weight);

    }

 

    printf("\nList of persons with weight > 50 and height > 40:\n");

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

    printf("Name\tAge\tHeight\tWeight\n");

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

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

    {

        if (persons[i].weight > 50 && persons[i].height > 40)

        {

            printf("%s\t%d\t%.2f\t%.2f\n", persons[i].name, persons[i].age, persons[i].height, persons[i].weight);

        }

    }

 

    getch();

}

 

 

Output:

Enter details of 10 persons:

 

Name: Arjun

Age: 30

Height: 180

Weight: 70

 

Name: Priya

Age: 25

Height: 160

Weight: 55

 

Name: Amit

Age: 35

Height: 170

Weight: 65

 

Name: Shreya

Age: 28

Height: 150

Weight: 45

 

Name: Mehdiali

Age: 16

Height: 185

Weight: 45

 

Name: Raj

Age: 40

Height: 155

Weight: 60

 

Name: Kavita

Age: 45

Height: 175

Weight: 80

 

Name: Vikram

Age: 22

Height: 165

Weight: 52

 

Name: Anjali

Age: 27

Height: 190

Weight: 90

 

Name: Neha

Age: 38

Height: 170

Weight: 70

 

 

 

 

List of persons with weight > 50 and height > 40:

------------------------------------------------

Name    Age     Height  Weight

------------------------------------------------

Arjun    30      180.00  70.00

Mehdiali 16      185.00  45.00

Anjali   27      190.00  90.00

 


Please follow me for more practicals....


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.