1. Write a C program to show the size of union and structure.
#include<stdio.h>
struct student
{
int roll_no;
char name[40];
}s;
union students
{
char name[20];
int roll_no;
}s1;
void main ()
{
printf("Size of structure is %d\n",sizeof (s));
printf("Size of union is %d\n",sizeof (s1));
}
Output:
2. WAP that reads the information of a person with personal id,
name, address, age using structure and display the record.
#include<stdio.h>
struct person
{
int age,pid;
char name[30],address[30];
}p ;
void main ()
{
printf("Enter the name, pid, address and age of a person:\n");
scanf("%s%d%s%d",p.name,&p.pid,p.address,&p.age);
printf("Name:%s\t Pid:%d\t Address:%s\t Age:%d\t" ,p.name ,p.pid, p.address, p.age);
}
Output:
3. WAP in C program that uses structure to represent details of
‘5’ books (title, author, publication and price) and print them
out. (2081 NEB board)
#include<stdio.h>
struct book
{
float price;
char title[30],author[30],publication[30];
} b[5];
void main ()
{
int i;
for(i=0;i<5;i++)
{
printf("Enter Title, Author, Publication and Price %d:",i+1);
scanf("%s%s%s%f",b[i].title,b[i].author,b[i].publication,&b[i].price);
}
printf("Details of all books:\n");
for(i=0;i<5;i++)
{
printf("Title:%s\t Author:%s\t Publication:%s\t Price:%.2f\
n" ,b[i].title,b[i].author,b[i].publication,b[i].price);
}
}
Output:
4. Write a C program that reads the account number, name
and address of ten customers from users and displays the
account number, name and address of these customers using
Array and structure. (2082 NEB board)
#include<stdio.h>
struct customer
{
int acc_no;
char name[30],address[30];
} c[10];
void main ()
{
int i;
for(i=0;i<10;i++)
{
printf("Enter name, address and account number of customer %d:",i+1);
scanf("%s%s%d",c[i].name,c[i].address,&c[i].acc_no);
}
printf("Details of ten customers:\n");
for(i=0;i<10;i++)
{
printf("Name:%s\t Address:%s\t Account number:%d\
n" ,c[i].name,c[i].address,c[i].acc_no);
}
}
Output:
5. Write a C program using structure to input staff id, name
and the salary of ‘n’ staffs. Display staff id, name and salary of
those staff whose salary range from 25 thousand to 40
thousand. (2080 NEB board)
#include <stdio.h>
struct staff
{
int id;
float salary;
char name[30];
};
void main ()
{
int i,n;
printf("Enter the number of staff:");
scanf("%d",&n);
struct staff s[n];
for(i=0;i<n;i++)
{
printf("Enter staff id, name and salary %d:",i+1);
scanf("%d%s%f",&s[i].id,s[i].name,&s[i].salary);
}
printf("Details of employee with salary between 25000 to 40000:\n");
for(i=0;i<n;i++)
{
if(s[i].salary>=25000&&s[i].salary<=40000)
{
printf("Name:%s\t Id:%d\t salary:%.2f\
n" ,s[i].name,s[i].id,s[i].salary);
}
}
}
Output:
6. Write a C program using a structure to store book title,
author, and price for 5 books. Find and display the details of
the most and least expensive books.
#include<stdio.h>
struct storebook
{
char title[30],author[30];
float price;
}s[5];
void main ()
{
int i,max,min;
for(i=0;i<5;i++)
{
printf("Enter the title, author and price of store book %d:",i+1);
scanf("%s%s%f",s[i].title,s[i].author,&s[i].price);
}
max=s[i].price;
for(i=0;i<5;i++)
{
if(s[i].price>max)
{
max=s[i].price;
}
}
min=s[i].price;
for(i=0;i<5;i++)
{
if(s[i].price<min)
{
min=s[i].price;
}
}
printf("Most expensive book of the store book:%d\n",max);
printf("Title:%d\t Author:%d\t Price:%f\n",s[i].title,s[i].author,s[i].price);
printf("Least expenxive book of the store book:%d\n",min);
printf("Title:%d\t Author:%d\t Price:%f\n",s[i].title,s[i].author,s[i].price);
}
Output:
7. Write a program in C to input the ID, name and address of
'n' employees using a structure, and display the records of
those employees whose address is 'Kathmandu'.
#include <string.h>
#include <stdio.h>
struct employee
{
int id;
char name[30], address[30];
};
void main ()
{
int i, n;
printf("Enter the number of employees: ");
scanf("%d", &n);
struct employee e[n];
for(i = 0; i < n; i++)
{
printf("Enter employee %d's name, ID, and address: ", i + 1);
scanf("%s %d %s", e[i].name, &e[i].id, e[i].address);
}
printf("\nDetails of employees whose address is Kathmandu:\n");
for(i = 0; i < n; i++)
{
if(strcasecmp(e[i].address, "Kathmandu") == 0)
{
printf("Name: %s\t ID: %d\t Address: %s\n", e[i].name, e[i].id, e[i].address);
}
}
}
Output:
8. Write a program in C to input the roll number, fname,
lname, and total marks of 5 students using a structure, and
display the records in ascending order based on total marks.
#include <string.h>
#include <stdio.h>
struct students {
int roll_no;
char fname[30], lname[30];
float tmarks;
};
void main () {
int i, j;
struct students temp, s[5];
for(i = 0; i < 5; i++) {
printf("Enter the student's fname, lname, roll number and marks %d: ", i + 1);
scanf("%s %s %d %f", s[i].fname, s[i].lname, &s[i].roll_no, &s[i].tmarks);
}
printf("Rearrange the records of students in ascending order based on marks:\n");
for(i = 0; i < 4; i++) {
for(j = i + 1; j < 5; j++) {
if(s[i].tmarks > s[j].tmarks) {
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
// Output sorted student details
for(i = 0; i < 5; i++) {
printf("Fname: %s\t Lname: %s\t Roll number: %d\t Marks: %.2f\n", s[i].fname,
s[i].lname, s[i].roll_no, s[i].tmarks);
}
}
Output:
9. Write a program to read the roll number, name, and gender
of multiple students using a structure variable, and then
rearrange (sort) the records in alphabetical order based on the
students' names.
#include<string.h>
#include<stdio.h>
struct students
{
int roll_no;
char name[30],gender[30];
};
void main ()
{
int i,n,j;
printf("Enter the number of students:");
scanf("%d",&n);
struct students temp,s[n];
for(i=0;i<n;i++)
{
printf("Enter the students name,roll number and gender %d:",i+1);
scanf("%s%d%s",s[i].name,&s[i].roll_no,s[i].gender);
}
printf("Rearrange the records of students in alphabetical order based on names:\n");
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(s[i].name,s[j].name)>0) {
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
}
for(i=0;i<n;i++) {
printf("Name:%s\t Id:%d\t Address:%s\n" ,s[i].name,s[i].roll_no,s[i].gender);
}
}
Output:
10. WAP to store 5 student’s record like name, age and gender
using union variable.
#include<stdio.h>
union students
{
char name[30],gender[30];
int age;
}s[5];
void main ()
{
int i;
for(i=0;i<5;i++)
{
printf("Enter the students name, age and gender %d:",i+1);
scanf("%s%d%s",s[i].name,&s[i].age,s[i].gender);
}
printf("Details of 5 students:\n");
for(i=0;i<5;i++)
{
printf("Name:%s\t Age:%d\t Gender:%s\n",s[i].name,s[i].age,s[i].gender);
}
}
Output: