Structures And Arrays
Questions:1
C program to declare a structure named student with attributes as roll, name, and per. Take input for the details of 40 students and display them.
Sol:
#include<stdio.h> struct student { int roll; char name[20]; float per; }s[40]; //struct student s[40]; int main() { //struct student s[40]; int i; /* input */ for(i=0;i<40;i++) { printf("Enter roll,name and per "); scanf("%d %s %f",&s[i].roll,s[i].name,&s[i].per); } /* display */ for(i=0;i<40;i++) { printf("roll %d name %s per %f\n",s[i].roll,s[i].name,s[i].per); } //getch(); return(0); }
/* Example Output for 3 students */ Enter roll,name and per 101 Amit 98 Enter roll,name and per 102 Sumit 96 Enter roll,name and per 103 Kapil 88 roll 101 name Amit per 98.000000 roll 102 name Sumit per 96.000000 roll 103 name Kapil per 88.000000
Question:2
C program to declare a structure named employee with attributes as empno, name, and salary. Take input for details of 25 employees and display them.
Sol:
#include<stdio.h> struct employee { int empno; char name[20]; float sal; }s[25]; //struct employee s[25]; int main() { //struct employee s[25]; int i; /* input */ for(i=0;i<25;i++) { printf("Enter empno,name and sal "); scanf("%d %s %f",&s[i].empno,s[i].name,&s[i].sal); } /* display */ for(i=0;i<25;i++) { printf("Empno %d name %s sal %f\n",s[i].empno,s[i].name,s[i].sal); } //getch(); return(0); }
/* Example Output of 3 employee */ Enter empno,name and sal 1001 Amit 9800 Enter empno,name and sal 102 Sumit 9600 Enter empno,name and sal 103 Kapil 8800 empno 101 name Amit sal 9800.000000 empno 102 name Sumit sal 9600.000000 empno 103 name Kapil sal 8800.000000
Question:3
C program to declare a structure named book with attributes as book code, book title, price, number of copies and cost. Book(bcode, btitle,price,noc, cost)
Take input for the details of 10 books, display the details along with cost.
Sol:
#include<stdio.h> struct book { int bcode,noc; char btitle[20]; float price,cost; }b[10]; //struct book b[10]; int main() { //struct book b[10]; int i; clrscr(); /* input */ for(i=0;i<10;i++) { printf("Enter book code, btitle, price and noc "); scanf("%d %s %f %d",&b[i].bcode,b[i].btitle, &b[i].price,&b[i].noc); b[i].cost=b[i].price*b[i].noc; } /* display */ for(i=0;i<10;i++) { printf("bcode %d btitle %s price %.2f noc %d cost %.2f\n", b[i].bcode,b[i].btitle,b[i].price,b[i].noc,b[i].cost); } getch(); return(0); }
Question:4.
C program to declare a structure named product with attributes as product no, product name, rate, quantity and cost. Product(no, name, rate, qty, cost). Take input for the details of 25 products, display the details along with cost of the products.
Sol:
#include<stdio.h> struct product { int pno; char pname[15]; float rate,qty,cost; }p[25]; //struct product p[25]; int main() { //struct product p[25]; int i; clrscr(); /* input */ for(i=0;i<25;i++) { printf("Enter pno,pname,rate and qty "); scanf("%d %s %f %f",&p[i].pno,p[i].pname,&p[i].rate,&p[i].qty); p[i].cost=p[i].rate*p[i].qty; } /* display */ for(i=0;i<25;i++) { printf("pno %d pname %s rate %.2f qty %d cost %.2f\n", p[i].pno,p[i].pname,p[i].rate,p[i].qty,p[i].cost); } getch(); return(0); }
Question:5.
C program to declare a structure names student with attributes as roll number , name, marks of three subjects, total and per. Student(roll, name, m1, m2, m3, total, per) Take input for details of 50 student, display the details along with total and per.
Sol:
#include<stdio.h> struct student { int roll; char name[20]; float m1,m2,m3,total,per; }s[50]; //struct student s[50]; int main() { //struct student s[50]; int i; /* input */ for(i=0;i<50;i++) { printf("Enter roll,name,m1,m2, and m3 "); scanf("%d %s %f %f %f",&s[i].roll,s[i].name,&s[i].m1,&s[i].m2,&s[i].m3); s[i].total=s[i].m1+s[i].m2+s[i].m3; s[i].per=s[i].total/3; } /* display details */ for(i=0;i<50;i++) { printf("Roll %d Name %s Total %.3f Per %.3f\n",s[i].roll,s[i].name,s[i].total,s[i].per); } return(0); }