Friday, April 4, 2025

[A+] More C Questions with Answers

 

1) WAP to input name and address of 5 employees and sort them in descending alphabetic order according to their name using structure.

Ans:

#include <stdio.h>

#include <conio.h>

#include <string.h>

struct Employee

{

    char name[50];

    char address[100];

};

void main()

{

    struct Employee emp[5], temp;

    int i, j;   

    clrscr();

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

    {

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

        printf("Enter name”);

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

        printf("Enter address” );

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

    }

    // Sorting in descending order by name)

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

        for (j = i + 1; j < 5; j++) {

            if (strcmp(emp[i].name, emp[j].name) < 0) {

                temp = emp[i];

                emp[i] = emp[j];

                emp[j] = temp;

            }

        }

    }

    printf("%s\t%s\n", "NAME", "ADDRESS");

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

    {

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

    }

    getch();

}

 

2) WAP to input name and address of 5 employees and sort them in ascending alphabetic order according to their name using structure.

Ans:

#include <stdio.h>

#include <conio.h>

#include <string.h>

struct Employee

{

    char name[50];

    char address[100];

};

void main()

{

    struct Employee emp[5], temp;

    int i, j;   

    clrscr();

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

    {

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

        printf("Enter name”);

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

        printf("Enter address” );

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

    }

    // Sorting in ascending order by name

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

        for (j = i + 1; j < 5; j++) {

            if (strcmp(emp[i].name, emp[j].name) > 0) {

                temp = emp[i];

                emp[i] = emp[j];

                emp[j] = temp;

            }

        }

    }

    printf("%s\t%s\n", "NAME", "ADDRESS");

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

    {

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

    }

    getch();

}


3) Write a program that reads roll_no, fname, lname, of 5 students and print the same record in ascending order on the basis of roll_no using structure.

Ans:

#include <stdio.h>

#include <conio.h>

 

struct Student {

    int roll_no;

    char fname[50];

    char lname[50];

};

void main()

{

    struct Student stu[5], temp;

    int i, j;   

    clrscr();

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

    {

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

        printf("Enter roll number”);

        scanf("%d", &stu[i].roll_no);

        printf("Enter first name”);

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

        printf("Enter last name”);

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

    }

    // Sorting in ascending order by roll_no

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

        for (j = i + 1; j < 5; j++) {

            if (stu[i].roll_no > stu[j].roll_no) {

                temp = stu[i];

                stu[i] = stu[j];

                stu[j] = temp;

            }

        }

    }

    printf("\n%s\t%s\t%s\n", "ROLL NO", "FIRST NAME", "LAST NAME");

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

        printf("%d\t%s\t%s\n", stu[i].roll_no, stu[i].fname, stu[i].lname);

    }

    getch();

}


4) Write a C program to store name, mark of 20 students and to sort the data according with mark in descending order and display them.

Ans:

#include <stdio.h>

#include <conio.h>

struct Student

{

    char name[50];

    int mark;

};

void main()

{

    struct Student stu[20], temp;

    int i, j;

    clrscr();

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

    {

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

        printf("Enter name”);

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

        printf("Enter mark”);

        scanf("%d", &stu[i].mark);

    }

    // Sorting in descending order by marks

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

        for (j = i + 1; j < 20; j++) {

            if (stu[i].mark < stu[j].mark) {

                temp = stu[i];

                stu[i] = stu[j];

                stu[j] = temp;

            }

        }

    }

    printf("\n%-20s%-10s\n", "NAME", "MARKS");

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

    {

        printf("%-20s%-10d\n", stu[i].name, stu[i].mark);

    }

    getch();

}


0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home