1) Write a program to define structure with tag state with fields state name, number of districts and total population. Read and display the data.
#include<stdio.h>
#include<conio.h>
struct state {
char name[50];
int num_dis;
int population;
};
void main()
{
int i;
struct state s[3];
clrscr();
for(i=0;i<3;i++){
printf("Enter name of state %d: ", i+1);
scanf("%s", &s[i].name);
printf("Enter number of districts in %s: ", s[i].name);
scanf("%d", &s[i].num_dis);
printf("Enter total population of %s in Lakh: ",s[i].name);
scanf("%d", &s[i].population);
printf("\n");
}
for (i=0;i<3;i++){
printf("State name: %s\n", s[i].name);
printf("Number of districts: %d\n", s[i].num_dis);
printf("Total population: %d Lakh\n", s[i].population);
}
getch();
}
2) Write a program to create a structure of 5 student’s roll_no and name and display the records. Use array of structure.
#include <stdio.h>
#include <conio.h>
struct student {
int roll_no;
char name[50];
};
void main() {
int i;
struct student s[5];
clrscr();
for (i = 0; i < 5; i++) {
printf("Enter roll number for student %d: ", i+1);
scanf("%d", &s[i].roll_no);
printf("Enter name for student %d: ", i+1);
scanf("%s", s[i].name);
printf("\n");
}
printf("Student Records:\n");
for (i = 0; i < 5; i++) {
printf("Roll number: %d\n", s[i].roll_no);
printf("Name: %s\n\n", s[i].name);
}
getch();
}
3) Write a program to create structure of bank with accno, holder_name and balance and display them for n holders whose balance is less than 5000.
#include<stdio.h>
#include<conio.h>
struct bank
{
int accno,bal;
char name[50];
}b[20];
void main()
{
int i,n;
clrscr();
printf("Enter number of bank account holders:");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("Enter account number for holder %d:",i+1);
scanf("%d", &b[i].accno);
printf("Enter name for holder %d:",i+1);
scanf("%s", b[i].name);
printf("Enter balance for holder %d:",i+1);
scanf("%d", &b[i].bal);
printf("\n");
}
printf("Account holders with balance less than 5000:\n");
for (i=0;i<n;i++)
{
if(b[i].bal<5000)
{
printf("Account number:%d\n",b[i].accno);
printf("Account holder name:%s\n", b[i].name);
printf("Balance:%d\n\n",b[i].bal);
}
}
getch();
}
4) Write a program to create union of student’s roll_no and name and display the records.
#include <stdio.h>
#include <conio.h>
union student {
int roll_no;
char name[50];
};
void main() {
union student s;
clrscr();
// Read in data for student
printf("Enter roll number for student: ");
scanf("%d", &s.roll_no);
printf("Enter name for student: ");
scanf("%s", s.name);
// Display data for student
printf("Student Record:\n");
printf("Roll number: %d\n", s.roll_no);
printf("Name: %s\n", s.name);
getch();
}