struct programs

advertisement
Structure based programs
1. Employee details program
#include<stdio.h>
#include<conio.h>
struct emp
{
int empno ;
char name[10] ;
int bpay, allow, ded, npay ;
} e[10] ;
void main()
{
int i, n ;
clrscr() ;
printf("Enter the number of employees : ") ;
scanf("%d", &n) ;
for(i = 0 ; i < n ; i++)
{
printf("\nEnter the employee number : ") ;
scanf("%d", &e[i].empno) ;
printf("\nEnter the name : ") ;
scanf("%s", e[i].name) ;
printf("\nEnter the basic pay, allowances & deductions : ") ;
scanf("%d %d %d", &e[i].bpay, &e[i].allow, &e[i].ded) ;
e[i].npay = e[i].bpay + e[i].allow - e[i].ded ;
}
printf("\nEmp. No. Name \t Bpay \t Allow \t Ded \t Npay \n\n") ;
for(i = 0 ; i < n ; i++)
{
printf("%d \t %s \t %d \t %d \t %d \t %d \n", e[i].empno,
e[i].name, e[i].bpay, e[i].allow, e[i].ded, e[i].npay) ;
}
getch() ;
}
Output:
Enter the number of employees : 2
Enter the employee number : 101
Enter the name : Arun
Enter the basic pay, allowances & deductions : 5000 1000 250
Enter the employee number : 102
Enter the name : Babu
Enter the basic pay, allowances & deductions : 7000 1500 750
Emp.No. Name Bpay Allow Ded Npay
101
Arun 5000 1000 250 5750
102
Babu 7000 1500 750 7750
2. Student details program
#include<stdio.h>
#define SIZE 50
struct student {
char name[30];
int rollno;
int sub[3];
};
void main() {
int i, j, max, count, total, n, a[SIZE], ni;
struct student st[SIZE];
clrscr();
printf("Enter how many students: ");
scanf("%d", &n);
/* for loop to read the names and roll numbers*/
for (i = 0; i < n; i++) {
printf("\nEnter name and roll number for student %d : ", i);
scanf("%s", &st[i].name);
scanf("%d", &st[i].rollno);
}
/* for loop to read ith student's jth subject*/
for (i = 0; i < n; i++) {
for (j = 0; j <= 2; j++) {
printf("\nEnter marks of student %d for subject %d : ", i, j);
scanf("%d", &st[i].sub[j]);
}
}
/* (i) for loop to calculate total marks obtained by each student*/
for (i = 0; i < n; i++) {
total = 0;
for (j = 0; j < 3; j++) {
total = total + st[i].sub[j];
}
printf("\nTotal marks obtained by student %s are %dn", st[i].name,total);
a[i] = total;
}
/* (ii) for loop to list out the student's roll numbers who
have secured the highest marks in each subject */
/* roll number who secured the highest marks */
for (j = 0; j < 3; j++) {
max = 0;
for (i = 0; i < n; i++) {
if (max < st[i].sub[j]) {
max = st[i].sub[j];
ni = i;
}
}
printf("\nStudent %s got maximum marks = %d in Subject : %d",st[ni].name, max, j);
}
max = 0;
for (i = 0; i < n; i++) {
if (max < a[i]) {
max = a[i];
ni = i;
}
}
printf("\n%s obtained the total highest marks.", st[ni].name);
getch();
}
Output:
Enter how many students: 2
Enter name and roll number for student 0 : Pritesh 1
Enter name and roll number for student 1 : Suraj 2
Enter marks of student 0 for subject 0 : 90
Enter marks of student 0 for subject 1 : 89
Enter marks of student 0 for subject 2 : 78
Enter marks of student 1 for subject 0 : 67
Enter marks of student 1 for subject 1 : 88
Enter marks of student 1 for subject 2 : 99
Total marks obtained by student Pritesh are 257
Total marks obtained by student Suraj are 254
Student Pritesh got maximum marks = 90 in Subject : 0
Student Pritesh got maximum marks = 89 in Subject : 1
Student Suraj got maximum marks = 99 in Subject : 2
Pritesh obtained the total highest marks.
3. Write a C program using structure to find student grades in a class. Make necessary assumption.
Ans.
Before writing program we make some assumption as following :
Maximum total marks is 500.
Student_percentage
Grades
>=80
A
>=60
B
>=50
C
>=40
D
<40
F
/*C program to find students grades in a class through structure */
#include<stdio.h>
#include<conio.h>
struct stud
{
char nam[20];
int obtain_mark;
int per;
char grad[5];
};
struct stud s[5];
int i;
int main()
{
for(i=1; i<=5; i++)
{
printf("Enter %d student name : ",i);
scanf("%s",&s[i].nam);
printf("Enter
%d
student
obtained
marks
=
",i);
scanf("%d",&s[i].obtain_mark);
fflush(stdin);
}
for(i=1;
i<=5;
i++)
s[i].per=s[i].obtain_mark/5;
for(i=1;
i<=5;
i++)
{
if(s[i].per>=80)
strcpy(s[i].grad,"A");
else if(s[i].per>=60)
strcpy(s[i].grad,"B");
else if(s[i].per>=50)
strcpy(s[i].grad,"C");
else if(s[i].per>=40)
strcpy(s[i].grad,"D");
else
strcpy(s[i].grad,"F");
}
for(i=1; i<=5; i++)
printf("\n%d
student
%s
has
obtained
grade
%s
",i,s[i].nam,s[i].grad);
getch();
return 0;
}
Output:
Enter
1
student
name :
John
Enter 1 student obtained marks : 250
Enter
2
student
name :
Robert
student
name :
Isabell
Enter 2 student obtained marks : 410
Enter
3
Enter 3 student obtained marks : 386
Enter
4
student
name :
Adem
Enter 4 student obtained marks :197
Enter
5
student
name :Harry
Enter 5 student obtained marks : 490
1
student
Jhon
has
2 student Robert has obtained grade A
3 student Isabell has obtained grade B
4 student Adem has obtained grade F
5 student Harry has obtained grade A
4.Book details program
#include <stdio.h>
#include <string.h>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main( ) {
struct Books Book1;
/* Declare Book1 of type Book */
struct Books Book2;
/* Declare Book2 of type Book */
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
/* print Book1 info */
printf( "Book 1 title : %s\n", Book1.title);
printf( "Book 1 author : %s\n", Book1.author);
printf( "Book 1 subject : %s\n", Book1.subject);
obtained
grade C
printf( "Book 1 book_id : %d\n", Book1.book_id);
/* print Book2 info */
printf( "Book 2 title : %s\n", Book2.title);
printf( "Book 2 author : %s\n", Book2.author);
printf( "Book 2 subject : %s\n", Book2.subject);
printf( "Book 2 book_id : %d\n", Book2.book_id);
return 0;
}
Output:
Book 1 title : C Programming
Book 1 author : Nuha Ali
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Zara Ali
Book 2 subject : Telecom Billing Tutorial
Book 2 book_id : 6495700
Download