Friday, April 4, 2025

[A+] More C Questions with Answers

 

Q1) Write a C program using structure to input employee’s id, name and salary. Then print that entered data in sorted format on the basis of salary (from highest to lowest).

Ans:

#include <stdio.h>

struct employee

{

    int id;

    char name[30];

    float salary;

};

int main()

{

    struct employee e[10], temp;

    int i, j;

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

    {

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

        printf("Enter ID: ");

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

        printf("Enter Name: ");

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

        printf("Enter Salary: ");

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

    }

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

    {

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

        {

            if(e[i].salary < e[j].salary)

            {

                temp = e[i];

                e[i] = e[j];

                e[j] = temp;

            }

        }

    }

    printf("\nEmployee Details Sorted by Salary (Highest to Lowest):\n");

    printf("\nID\tNAME\tSALARY");

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

    {

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

    }

    return 0;

}

PQ) 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: Do yourself looking on Q1

Q2) 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>

struct student

{

    int roll_no;

    char fname[20];

    char lname[20];

};

int main()

{

    struct student s[5], temp;

    int i, j;

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

    {

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

        printf("Enter Roll No: ");

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

        printf("Enter First Name: ");

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

        printf("Enter Last Name: ");

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

    }

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

    {

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

        {

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

            {

                temp = s[i];

                s[i] = s[j];

                s[j] = temp;

            }

        }

    }

    printf("\nStudent Records Sorted by Roll Number (Ascending):\n");

    printf("\nROLL NO\tFIRST NAME\tLAST NAME");

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

    {

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

    }

    return 0;

}

Q3) 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 <string.h>

struct employee

{

    char name[30];

    char address[50];

};

int main()

{

    struct employee e[5], temp;

    int i, j;

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

    {

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

        printf("Enter Name: ");

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

        printf("Enter Address: ");

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

    }

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

    {

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

        {

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

            {

                temp = e[i];

                e[i] = e[j];

                e[j] = temp;

            }

        }

    }

    printf("\nEmployees sorted in descending order by name:\n");

    printf("\nNAME\tADDRESS");

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

    {

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

    }

    return 0;

}

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home