C PROGRAM(structure) EXAMPLES

 1. Write a c program to enter id,name,and marks scored in math, science, social of 5 students. Also display in proper format along with percentages using structure.


#include <stdio.h>

#include<conio.h>

struct Student 

{

    int id;

    char name[50];

    int mathMarks;

    int scienceMarks;

    int socialMarks;

};

void  main() 

{

    struct Student s[5];

int i;

int totalMarks:

float percentage;

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

{        

     printf("ID: ");

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

        

        printf("Name: ");

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


        printf("Marks scored in Maths: ");

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

        

        printf("Marks scored in Science: ");

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

        

        printf("Marks scored in Social: ");

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

    }

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

 {

        totalMarks = s[i].mathMarks + s[i].scienceMarks + s[i].socialMarks;

        percentage = (totalMarks / 3);


        printf("Student ID: %d\nName: %s\nMaths Marks: %d\nScience Marks: %d\n"Social Marks: %d\n", students[i].id,s[i].name,s[i].mathMarks, s[i].socialMarks);

        printf("Percentage: %f\n\n", percentage);

    }


getch();

}


2.  Write a C program to input staffid, name and salary of 5 staffs. display staffid, name and salary of those staff whose salary range from 25 thousands to 30 thousands using structure.


#include <stdio.h>

#include<conio.h>

struct Staff {

    int staffId;

    char name[50];

   int salary;

};


void main() {

   

    struct Staff s[5];

int i;

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

{       

      printf("Staff ID: ");

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

        

        printf("Name: ");

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


        printf("Salary: ");

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

    }

   

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

{

        if (s[i].salary >= 25000 && s[i].salary <= 30000) {

            printf("Staff ID: %d\nName: %s\nSalary: %dn\n", s[i].staffId, s[i].name,s[i].salary);

              }

    }


getch();

}


3. Write a C program to enter id, name, address and marks of 5 students.. display them in proper format using structure.


# include<stdio.h>

#include<conio.h>

struct student

{

int id;

char name[20];

char address[20];

int marks;

};

void main()

{

struct student s[5];

int i;

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

{

printf("Enter ID:");

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

printf("Enter Name:");

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

printf("Enter Address:");

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

printf("Enter Marks:");

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

}

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

{

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

}

getch();

}


4. write a Program that takes  name and address of different students and sort them on the basis of name.

#include <stdio.h>

#include <string.h>

#include<conio.h>

struct Student {

    char name[50];

    char address[100];

};

void main() {

    int n;

int i;

struct Student temp;

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

    scanf("%d", &n);


    struct Student s[100]; // Assuming a maximum of 100 students

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

{

        printf("Enter name for student %d: ", i + 1);

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

        printf("Enter address for student %d: ", i + 1);

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

    }


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

{

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

 {

            if (strcmp(s[j].name, s[j + 1].name) > 0) 

{

               temp = s[j];

                s[j] = s[j + 1];

                s[j + 1] = temp;            

                

            }

        }

    }


    printf("\nSorted list of students:\n");

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

{

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

    }

   getch();

}

5. Write a program to enter id,name, salary and post of 3 employee and display in proper format.

#include<stdio.h>

#include<conio.h>

struct employee

{

int id;

char name[20];

int salary;

char post[20];

};

void main()

{

struct employee e[3];

int i;

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

{

printf("Enter ID of employee:");

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

printf("Enter Name of employee:")

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

printf("Enter salary of employee:");

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

printf("Enter post of employee:");

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

}

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

{

printf("\nID=%d\nName=%s\nSalary=%d\nPost=%s",e[i].id,e[i].name,e[i].salary,e[i].post);

}

getch();

}


6. Write a C program to read 5 employees’ id, name, salary and 

address. Display the record in proper format according to 

highest paid salary.

#include <stdio.h>

#include<conio.h>

// Define the structure for an employee

struct Employee {

 int id;

 char name[50];

 int salary;

 char address[100];

};

void main() {

 struct Employee e[5];

 int i, j;

 struct Employee temp;

 clrscr();

 // Input employee records

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

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

printf("ID: ");

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

printf("Name: ");

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

printf("Salary: ");

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

printf("Address: ");

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

printf("\n");

 }

 // Sort employees by salary in descending order

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

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

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

// Swap employees[i] and employees[j]

temp = e[i];

e[i] = e[j];

e[j] = temp;

 }

}

 }

 // Display sorted employee records

 printf("Employee Records (Sorted by Highest Salary):\n");

 printf("ID\tName\tSalary\tAddress\n");

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

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

 }

 getch();

}

7. Write a C program to input id, name and marks obtained in 3 

subjects by 5 students. Display the record of students with total 

marks obtained along with percentages.


#include <stdio.h>

#include<conio.h>

struct Student

{

 int id;

 char name[50];

 int mathMarks;

 int scienceMarks;

 int socialMarks;

};

void main()

{

 struct Student s[3];

int i;

int totalMarks;

float percentage;

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

{

 printf("ID: ");

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

printf("Name: ");

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

printf("Marks scored in Maths: ");

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

printf("Marks scored in Science: ");

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

printf("Marks scored in Social: ");

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

 }

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

{

totalMarks = s[i].mathMarks + s[i].scienceMarks + s[i].socialMarks;

percentage = (totalMarks / 3);

printf("Student ID: %d\nName: %s\nMaths Marks: %d\nScience Marks: %d\nSocial 

Marks: %d\n", s[i].id,s[i].name,s[i].mathMarks, s[i].socialMarks);

printf("Total Marks:%d\n\n", totalMarks);

printf("Percentage: %f\n\n", percentage);

 }

getch();

}




Comments

Popular posts from this blog

Software Process Model(SPM)