Programs on structures
Write a program to input employee number name and salary from the user and store these values in a structure and at last print the values in a proper format
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct employee
{
int empno;
char name[25];
int sly;
}emp;
void main()
{
cout<<”enter the
employee number”;
cin>>emp.empno;
cout<<”enter the
employee name”;
gets(emp.name);
cout<<”enter salary”;
cin>>emp.sly;
cout<<”\nempioyee
details”;
cout<<”employee
number=”<<emp.empno;
cout<<”\nemployee
name=”<<emp.name;
cout<<”\nemployee
salary=”<<<emp.sly;
getch();
}
output
enter employee number10101
enter employee name rakhi
gupta
enter employee salary56000
employee details
employee number=10101
employee name=rakhi gupta
employee salary56000
Write a program to input roll no., name, and percentage of 5 students and store it in a structure name student.A last display all the details in program format.
#include<iostream.h>
#include<conio.h>
#include<studio.h>
struct student
{
int rollno;
char name[25];
float per;
}stu[5];
void main()
{
int i;
for(i=0;i<5;i++)
{
cout<<”Enter the roll
no”;
cin>>stu[i].rollno;
cout<<”Enter the name”;
gets(stu[i].name);
cout<<”Enter the
percentage”;
cin>>stu[i].per;
}
clrscr();
cout<<”Student
details”;
for(i=0;i<5;i++)
cout<<”\nStudent roll
no.=”<<stu[i].roll no;
cout<<”\nStudent
name.=”<<stu[i].name;
cout<<”\nStudent
percentage.=”<<stu[i].per;
}
getch();
}
Output
Student roll no.1
Student name.sandeep
Student percentage.45
Student roll no.2
Student name.anushka
Student percentage.89
Student roll no.3
Student name.dev
Student percentage.56
Student roll no.4
Student name.naveen
Student percentage.90
Student roll no5.
Student name.ashu
Student percentage.67
Write a program to input product no., product name and unit price of 10 products within a structure name product and print the detail of product with maximum unit price.
#include<iostream.h>
#include<conio.h>
#include<studio.h>
struct product
{
int no;
char name[25];
int price;
}
pro[10];
void main;
{
float max=0;
int i,key;
for(i=0;i<10;i++)
{
cout<<”Enter product
number”;
cin>>pro[i].pno;
cout<<”Enter product
name”;
gets(pro[i].pname);
cout<<”Enter product
price”;
cin>>pro[i].price;
}
if(pro[i].price>max)
{
max=pro[i].price;
key=i;
}
}
clrscr();
cout<<”The detail of
products with maximum price”;
cout<<”/nproduct
no.”pro[key].pno;
cout<<”/nname”pro[key].pname;
cout<<”/nprice”pro[key].price;
getch()
}
Output
The datails of product with
maximum price
product no.8
namesunfeast
price560
Write a program to input name roll no and date of birth of three students from the user and display these information on the screen. The name should be further divided into first name and last name and stored in a structure. Date of birth is also divided into the date, month and year and stored in another structure to design the program.
#include<iostream.h>
#include<conio.h>
#include<studio.h>
struct name details
{
char fname[25];
char lname[25];
};
struct date of birth
{
int dd;
int mm;
int yy;
};
struct student
{
name details n;
int rno;
date of birth dob;
}s[3];
void main()
{
int i;
for(i=0;i<3;i++)
{
clrscr();
cout<<”Enter name of
student”;
cin>>s[i].n.fname>>s[i].n.lname;
cout<<”Enter roll no”;
cin>>s[i].rno;
cout<<”date of birth”;
cin>>s[i].dob.dd>>s[i].dob.mm>>s[i].dob.yy;
}
clrscr();
cout<<”student’s
details”;
for(i=0;i<3;i++)
{
cout<<”/nstudent
name=”<<s[i].n./name<<””<<s[i].n.lname;
cout<<”/nstudent roll
number=”<<s[i].rno;
cout<<”/nstudent date
of birth”; cout<<s[i].dob.dd<<”/”<<s[i].dob.mm<<”/”<<s[i].dob.yy;
}
getch();
}
output
student name=dolly
student roll number=2
student date of
birth23/9/2000
student name=riya
student roll number=5
student date of birth3/9/2001
student name=betty
student roll number=7
student date of birth7/3/2000
Write a program to input name and marks of three subjects from user and print name with average marks.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct marks
{
int sub1;
int sub2;
int sub3;
}
struct student
char name[50];
marks mk;
}s;
void main()
{
float avg;
cout<<”enter the name”;
gets(s.name);
cout<<”enter the
marks”;
cout<<”sub1”;
cin>>s.mk.sub1;
cout<<”sub2”;
cin>>s.mk.sub2;
cout<<”sub3”;
cin>>s.mk.sub3;
avg=(s.mk.sub1+s.mk.sub2+s.mk.sub3)/3;
clrscr();
cout<<”name=”<<s.name;
cout<<”average
marks=”<<avg;
getch();
}
name=riya
average marks=78
2 Comments
That way errors can easily be corrected.
WE will see to that functions and try to reduce complexity.