0% found this document useful (0 votes)
25 views11 pages

Structures Union Programs

Uploaded by

missiongaire132
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views11 pages

Structures Union Programs

Uploaded by

missiongaire132
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1) Write a program in C to store [5] students' details

(id, name, roll_number) and print them in proper format.


#include <stdio.h>
// Define a structure to hold student details
struct Student {
int id;
char name[50];
int roll_number;
};

int main() {
struct Student students[5]; // Array to hold details of 5 students
int i;

// Input student details


for (i = 0; i < 5; i++) {
printf("Enter details for student %d (ID, Name, Roll Number): ", i + 1);
scanf("%d %s %d", &students[i].id, students[i].name, &students[i].roll_number);
printf("\n");
}

// Print student details


printf("Student Details (ID, Name, Roll Number):\n");
for (i = 0; i < 5; i++) {
printf("%d, %s, %d\n", students[i].id, students[i].name, students[i].roll_number);
}

return 0;
}
2. Write a program in C to store [n] students’
details (id, name,roll_number) and print them.
#include <stdio.h>
struct Student {
int id;
char name[50];
int roll_number;
};

int main() {
int n;

// Input number of students


printf("Enter the number of students: ");
scanf("%d", &n);

struct Student students[n]; // Array to hold student details

// Input student details


for (int i = 0; i < n; i++) {
printf("Enter details for student %d (ID, Name, Roll Number): ", i + 1);
scanf("%d %s %d", &students[i].id, students[i].name, &students[i].roll_number);
}

// Print student details


printf("\nStudent Details:\n");
printf("ID\tName\t\tRoll Number\n");
printf("-----------------------------------\n");

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


printf("%d\t%s\t\t%d\n", students[i].id, students[i].name, students[i].roll_number);
}

return 0;
}
3. Write a program in C to store students’
details(id, name,roll_number) until the user wants
to continue and print them.
#include <stdio.h>

struct Stu {
int id;
char name[50];
int roll;
};

int main() {
struct Stu s[100]; // Array to hold student details
int cnt = 0; // Count of students
char ch;

do {
// Input student details in a single line
printf("Enter ID, Name, Roll Number: ");
scanf("%d %s %d", &s[cnt].id, s[cnt].name, &s[cnt].roll);
cnt++;

// Ask if the user wants to continue


printf("Add another student? (y/n): ");
scanf(" %c", &ch);
} while (ch == 'y' || ch == 'Y');

// Print student details


printf("\nID\tName\tRoll\n");
printf("---------------------\n");
for (int i = 0; i < cnt; i++) {
printf("%d\t%s\t%d\n", s[i].id, s[i].name, s[i].roll);
}

return 0;
}
4. Write a program in C to store [5] students’ details(id,
name, roll_number, computer marks) and print only those
data whose computer marks is above 50.
#include <stdio.h>

struct Stu {
int id;
char name[50];
int roll;
int comp_marks;
};

int main() {
struct Stu s[5]; // Array to hold 5 student details

// Input student details


for (int i = 0; i < 5; i++) {
printf("Enter ID, Name, Roll Number, Computer Marks: ");
scanf("%d %s %d %d", &s[i].id, s[i].name, &s[i].roll, &s[i].comp_marks);
}

// Print students with computer marks above 50


printf("\nStudents with Computer Marks > 50:\n");
printf("ID\tName\tRoll\tComp Marks\n");
printf("-------------------------------------\n");
for (int i = 0; i < 5; i++) {
if (s[i].comp_marks > 50) {
printf("%d\t%s\t%d\t%d\n", s[i].id, s[i].name, s[i].roll, s[i].comp_marks);
}
}

return 0;
}
5. Write a programming C to store [n] students’ details(id,
name, roll_number, computer marks) and display student
details in descending order of computer marks.

#include <stdio.h>
#include <string.h>

struct Stu {
int id;
char name[50];
int roll;
int comp_marks;
};

void sort(struct Stu s[], int n) {


struct Stu temp;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (s[j].comp_marks < s[j + 1].comp_marks) {
temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
}
}
}
int main() {
int n;
printf("Enter number of students: ");
scanf("%d", &n);

struct Stu s[n]; // Array to hold student details


// Input student details
for (int i = 0; i < n; i++) {
printf("Enter ID, Name, Roll Number, Computer Marks:
");
scanf("%d %s %d %d", &s[i].id, s[i].name, &s[i].roll,
&s[i].comp_marks);
}

// Sort students by computer marks in descending order


sort(s, n);

// Print sorted student details


printf("\nStudent Details (Sorted by Computer Marks):\n");
printf("ID\tName\tRoll\tComp Marks\n");
printf("-------------------------------------\n");
for (int i = 0; i < n; i++) {
printf("%d\t%s\t%d\t%d\n", s[i].id, s[i].name, s[i].roll,
s[i].comp_marks);
}

return 0;
}
6. Write a program in C to store [5] students’
details(name,roll_number, computer marks) and print only those
data whose computer marks is above 50 and name starts with ‘a’ or
‘m’.
#include <stdio.h>
#include <string.h>

struct Stu {
char name[50];
int roll;
int comp_marks;
};

int main() {
struct Stu s[5]; // Array to hold 5 student details

// Input student details


for (int i = 0; i < 5; i++) {
printf("Enter Name, Roll Number, Computer Marks: ");
scanf("%s %d %d", s[i].name, &s[i].roll, &s[i].comp_marks);
}

// Print students with specific criteria


printf("\nStudents with Comp Marks > 50 and Name starts with 'a' or 'm':\n");
printf("Name\tRoll\tComp Marks\n");
printf("-------------------------------------\n");
for (int i = 0; i < 5; i++) {
if (s[i].comp_marks > 50 && (s[i].name[0] == 'a' || s[i].name[0] == 'A' ||
s[i].name[0] == 'm' || s[i].name[0] == 'M')) {
printf("%s\t%d\t%d\n", s[i].name, s[i].roll, s[i].comp_marks);
}
}

return 0;
}
7. Write a program in C to store [n] students’ details(id,name,
roll_number, computer marks) and arrange them in ascending
order of marks and display those data whose marks is above 40 and
name starts with ‘a’ or ‘m’.
#include <stdio.h>
#include <string.h>

struct Student {
int id;
char name[50];
int roll_number;
int computer_marks;
};

void sortStudents(struct Student students[], int n) {


struct Student temp;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (students[j].computer_marks > students[j +
1].computer_marks) {
// Swap students[j] and students[j + 1]
temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
}
int main() {
int n; // Input number of students
printf("Enter the number of students: ");
scanf("%d", &n);
struct Student students[n]; // Array to hold student details

// Input student details


for (int i = 0; i < n; i++) {
printf("Enter details for student %d (ID, Name, Roll Number, Computer
Marks): ", i + 1);
scanf("%d %s %d %d", &students[i].id, students[i].name,
&students[i].roll_number, &students[i].computer_marks);
}

// Sort students by computer marks


sortStudents(students, n);

// Display students with marks above 40 and name starting with 'a' or 'm'
printf("\nStudents with marks above 40 and name starting with 'a' or 'm':\n");
printf("ID\tName\t\tRoll Number\tComputer Marks\n");
printf("---------------------------------------------------\n");

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


if (students[i].computer_marks > 40 &&
(students[i].name[0] == 'a' || students[i].name[0] == 'A' ||
students[i].name[0] == 'm' || students[i].name[0] == 'M')) {
printf("%d\t%s\t\t%d\t\t%d\n",
students[i].id, students[i].name,
students[i].roll_number, students[i].computer_marks);
}
}

return 0;
}
8.Develop a program in C using structure to ask the information of any 12 students with
roll_number,name and marks scored in sub1, sub2, and sub3. Also, display them in proper
format along with the calculation of total and percentage . [Note: the full marks of each
subject is 100].
#include <stdio.h>

// Define a structure to hold student details


struct Student {
int roll;
char name[50];
int marks1;
int marks2;
int marks3;
};

int main() {
struct Student students[12]; // Array to hold details of 12 students
int i;

// Input student details


for (i = 0; i < 12; i++) {
printf("Enter details for student %d (Roll, Name, Marks1, Marks2, Marks3): ", i + 1);
scanf("%d %s %d %d %d", &students[i].roll, students[i].name,
&students[i].marks1, &students[i].marks2, &students[i].marks3);
printf("\n");
}

// Print student details with total and percentage


printf("\nStudent Details:\n");
printf("Roll\tName\t\tMarks1\tMarks2\tMarks3\tTotal\tPercentage\n");
printf("---------------------------------------------------------------\n");

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


int total = students[i].marks1 + students[i].marks2 + students[i].marks3;
float percentage = (total / 3.0); // Calculate percentage (out of 100)

printf("%d\t%s\t\t%d\t%d\t%d\t%d\t%.2f%%\n",
students[i].roll, students[i].name,
students[i].marks1, students[i].marks2,
students[i].marks3, total, percentage);
}

return 0;
}

You might also like