Program To Generate Fibonacci Series
Program To Generate Fibonacci Series
Output:
Enter the limit: 50
The fibonacci series upto the limit 50 is: 0 1 1 2 3 5 8 13 21 34
The number of elements in the fibonacci series upto the limit 50 is 10
getch();
return 0;
}
Output:
Enter the two numbers: 52 12
Gcd of 52 and 12 is 4
obj.fun();
getch();
return 0;
}
Output:
Enter the limit: 8
Enter 8 numbers: -9 0 2 7 -99 66 43 -101
Maximum number is: 66
Minimum number is: -101
Output:
Enter the limit : 30
Prime numbers upto the limit 30 are : 1 2 3 5 7 11 13 17 19 23 29
The number of elements in the seris up to 30 is 11
p.dis();
getch();
return 0;
}
Output:
Enter the limit: 500
Perfect numbers are: 6 28 496
The number of elements in the series up to 500 is: 3
void pp::display()
{
cout<<"\nPrime numbers up to the limit "<<n<<" are: ";
for(int i=0;i<x;i++)
cout<<prime[i]<<setw(4);
cout<<"\n\nThe number of elements in the prime series up to "<<n<<" is
"<<x;
cout<<"\n\nPerfect divisors up to the limit "<<n<<" are: ";
for(i=0;i<c;i++)
cout<<per[i]<<setw(4);
cout<<"\n\nThe number of elements in the perfect divisors series up to
"<<n<<" is "<<c;
}
int main()
{
clrscr();
pp p1;
cout<<"Enter the limit : ";
p1.read();
p1.perpri();
p1.display();
getch();
return 0;
}
Output:
Enter the limit: 40
Prime numbers up to the limit 40 are: 1 2 3 5 7 11 13 17 19 23 29
31 37
The number of elements in the prime series up to 40 is 13
Perfect divisors up to the limit 40 are: 6 28
The number of elements in the perfect divisors series up to 40 is 2
#include<iostream.h>
#include<conio.h>
int a[25];
void read(int);
void display(int);
void srt(int);
int main()
{
clrscr();
int n;
cout<<"Enter the limit value : ";
cin>>n;
read(n);
srt(n);
display(n);
getch();
return 0;
}
void read(int n)
{
cout<<"Enter "<<n<<" numbers to sort: ";
for(int i=0;i<n;i++)
cin>>a[i];
}
void srt(int n)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
{
if(a[i] < a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
void display(int n)
{
cout<<"Sorted array in ascending order is: ";
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
}
Output:
Enter the limit value: 5
Enter 5 numbers to sort: 2 1 6 -21 0
Sorted array in ascending order is: -21 0 1 2 6
#include<iostream.h>
#include<conio.h>
class sort
{
private:
int a[10],i,j,n;
public:
void read();
void display();
void srt();
};
void sort::read()
{
cin>>n;
cout<<"Enter "<<n<<" numbers to sort: ";
for(i=0;i<n;i++)
cin>>a[i];
}
void sort::srt()
{
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
if(a[i] < a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
void sort::display()
{
cout<<"Sorted array in ascending order is: ";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
}
int main()
{
clrscr();
cout<<"Enter the limit value : ";
sort st;
st.read();
st.srt();
st.display();
getch();
return 0;
}
Output:
Enter the limit value: 7
Enter 7 numbers to sort: 3 2 -66 -444 9 100 22
Sorted array in ascending order is: -444 -66 2 3 9 22 100
void display(int n)
{
cout<<"Names after sorting:"<<endl;
for(int i=0;i<n;i++)
cout<<endl<<name[i];
}
Output:
Enter the number of names to be sorted:5
Enter name 1: manjesh
Enter name 2: nagaraju
Enter name 3: soma
Enter name 4: dilip
Enter name 5: manju
Names after sorting:
Dilip
Manjesh
Manju
Nagaraju
Soma
int i;
for(i=0;i<n;i++)
cout<<endl<<name[i];
}
int main()
{
clrscr();
names st;
cout<<"Enter the number of names to be sorted:";
st.read();
st.sort();
cout<<endl<<"Sorted names:"<<endl;
st.display();
getch();
return 0;
}
Output:
Enter the number of names to be sorted:6
Enter name
Enter name
Enter name
Enter name
Enter name
Enter name
1: manjunath
2: prashanth
3: manjesh
4: dilip
5: soma
6: nagaraj
Sorted names:
Dilip
Manjesh
Manjunath
Nagaraj
Prashanth
Soma
Output:
Enter the coefficients of the a, b, c: 0 2 4
The given equation is linear.
Root is: -2
Enter the coefficients of the a, b, c: 2 4 2
The roots are real and equal
Root 1 is: -1
Root 2 is: -1
Enter the coefficients of the a, b, c: 2 4 -3
The roots are real and distinct
Root 1 is: 0.581139
Root 2 is: -2.581139
Enter the coefficients of the a,b,c: - 2 5 -6
The roots are complex
Root 1 is: -4.203895e-45+i9.55142e-19
Root 2 is: -4.203895e-45-i9.55142e-19
20. Program to find the value of sine and cosine series using class
#include<iostream.h>
#include<conio.h>
#include<math.h>
class sincos
{
private:
int i,p;
float deg,x,y,m;
public:
void read();
float sincs(int);
void display()
{
cout<<endl<<"sin("<<deg<<")="<<sincs(1);
cout<<endl<<endl<<"cos("<<deg<<")="<<sincs(0);
}
};
void sincos::read()
{
cout<<"Enter the degree : ",
cin>>deg;
cout<<endl<<"Enter the accuracy : ";
cin>>m;
x=3.14*deg/180;
}
float sincos::sincs(int k)
{
float b=0,sum=0;
long int fact=1;
for(int i=k;;i+=2)
{
sum+=pow(x,i)*pow(-1,i/2)/fact;
fact*=(i+1)*(i+2);
if(fabs(sum-b)<m)
break;
b=sum;
}
return(sum);
}
int main()
{
clrscr();
sincos s;
s.read();
s.display();
getch();
return 0;
}
Output:
Enter the degree: 87
Enter the accuracy: 0.0001
sin(87)=0.998589
cos(87)=0.053104
cout<<endl;
for(int j=0;j<c1;j++)
cout<<setw(4)<<a[i][j];
}
}
void sub(int m1[20][20],int m2[20][20],int r1,int c1)
{
int s[20][20];
for(int i=0;i<r1;i++)
for(int j=0;j<c1;j++)
s[i][j]=m1[i][j]-m2[i][j];
for(i=0;i<r1;i++)
{
cout<<endl;
for(int j=0;j<c1;j++)
cout<<setw(4)<<s[i][j];
}
}
Output:
Enter order of matrix 1: 3 6
Enter order of matrix 2: 2 3
Operation not possible
Enter order of matrix 1: 2 5
Enter order of matrix 2: 2 5
Enter elements of first matrix:
12478
65890
Enter elements of second matrix:
34587
21456
Matrix after addition
4 6 9 15 15
8 6 12 14 6
Matrix after subtraction
-2 -2 -1 -1 1
4 4 4 4 -6
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<stdlib.h>
class matrix
{
private:
int r,c,m[10][10];
public:
matrix(){}
~matrix(){}
void read()
{
cin>>r>>c;
}
void mat();
int con(matrix,matrix);
void add(matrix,matrix);
void sub(matrix,matrix);
void dis()
{
for(int i=0;i<r;i++)
{
cout<<endl;
for(int j=0;j<c;j++)
cout<<setw(4)<<m[i][j];
}
}
};
int matrix::con(matrix x,matrix y)
{
return((x.r==y.r&&x.c==y.c)?1:0);
}
void matrix::mat()
{
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
cin>>m[i][j];
}
void matrix::add(matrix x, matrix y)
{
r=x.r;
c=x.c;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
m[i][j]=x.m[i][j]+y.m[i][j];
}
void matrix::sub(matrix x, matrix y)
{
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
m[i][j]=x.m[i][j]-y.m[i][j];
}
int main()
{
clrscr();
matrix m1,m2,a;
cout<<"Enter order of matrix 1 : ";
m1.read();
cout<<"Enter order of matrix 2 : ";
m2.read();
if(a.con(m1,m2))
{
cout<<"Enter elements of first matrix:"<<endl;
m1.mat();
cout<<"Enter elements of second matrix:"<<endl;
m2.mat();
cout<<endl<<"Matrix after addition"<<endl;
a.add(m1,m2);
a.dis();
cout<<endl<<"Matrix after subtraction"<<endl;
a.sub(m1,m2);
a.dis();
}
else
cout<<"operation not possiable";
getch();
return 0;
}
Output:
Enter order of matrix 1: 2 3
Enter order of matrix 2: 3 2
Operation not possiable
Enter order of matrix 1: 3 4
Enter order of matrix 2: 3 4
Enter first matrix
1425
6254
3978
Enter second matrix
0247
9145
9486
Matrix after addition
1 6 6 12
15 3 9 9
12 13 15 14
Matrix after subtraction
1 2 -2 -2
-3 1 1 -1
-6 5 -1 2
{
cout<<endl;
for(int j=0;j<c2;j++)
cout<<setw(4)<<add[i][j];
}
}
Output:
Enter the order of first matrix: 2 3
Enter the order of second matrix: 3 3
Enter first matrix
125
625
Enter second matrix
147
625
123
Matrix after multiplication :
18 18 32
23 38 67
Enter the order of first matrix: 3 5
Enter the order of second matrix: 6 5
Multiplication not possible
for(j=0;j<y.c;j++)
cout<<setw(4)<<m[i][j];
}
}
int main()
{
clrscr();
matrix m1,m2,a;
cout<<"Enter order of matrix 1 : ";
m1.read();
cout<<"Enter order of matrix 2 : ";
m2.read();
if(a.con(m1,m2))
{
cout<<"Enter elements of first matrix:"<<endl;
m1.mat();
cout<<"Enter elements of second matrix:"<<endl;
m2.mat();
cout<<endl<<"Matrix after multiplication"<<endl;
a.mul(m1,m2);
}
else
cout<<"Multiplication not possiable";
getch();
return 0;
}
Output:
Enter order of matrix 1: 3 3
Enter order of matrix 2: 3 2
Enter elements of first matrix:
245
624
351
Enter elements of second matrix:
124
325
Matrix after multiplication
28 41
22 38
25 26
Enter order of matrix 1: 2 5
Enter order of matrix 2: 2 5
Multiplication not possiable
Output:
Enter order of the matrix: 3 5
Enter matrix elements
2 4 -99 6 8
6 1 0 9 -12
10 6 4 -11 9
Matrix after sorting row elements
-99 2 4 6 8
-12 0 1 6 9
-11 4 6 9 10
cout<<endl;
for(int j=0;j<c;j++)
cout<<setw(4)<<m1[i][j];
}
}
int main()
{
clrscr();
mat m;
cout<<"Enter order of the matrix : ";
m.order();
cout<<endl<<"Enter matrix elements "<<endl;
m.read();
cout<<endl<<"Matrix after sorting row elements"<<endl;
m.sort();
m.dis();
getch();
return 0;
}
Output:
Enter order of the matrix : 4 3
Enter matrix elements
-99
0
4
17
4 6
8 -2
3 6
4 -52
4 6
0 8
4 6
4 17
void psdiag(int a)
{
int sum=0,j;
for(int i=0;i<a;i++)
sum+=m1[i][i];
cout<<endl<<endl<<"The sum of principal diagonal elements is = "<<sum;
sum=0;
for(i=0,j=a-1;i<a;i++,j--)
sum+=m1[i][j];
cout<<endl<<endl<<"The sum of secondary diagonal elements is =
"<<sum;
}
Output:
Enter order of matrix: 3 4
Enter matrix elements:
245
625
145
698
Transpose of a matrix
2 2 5
4 5 6
5 1 9
6 4 8
Not possible to find the sum of principle and secondary diagonal.
Enter order of matrix: 3 3
Enter matrix elements:
123
456
789
Transpose of a matrix
1 4 7
2 5 8
3 6 9
The sum of principal diagonal elements is = 15
The sum of secondary diagonal elements is = 15
for(i=0,j=m-1;i<m;i++,j--)
sum+=m1[i][j];
cout<<endl<<endl<<"The sum of secondary diagonal elements is =
"<<sum;
}
else
cout<<endl<<endl<<"Not possible to find the sum of principle and
secondary diagonal.";
}
int main()
{
trans t;
clrscr();
cout<<"Enter order of matrix : " ;
t.readord();
cout<<endl<<"Enter matrix elements : "<<endl;
t.readmat();
cout<<endl<<"Transpose of a matrix ";
t.tra();
t.psdiag();
getch();
return 0;
}
Output:
Enter order of matrix: 3 4
Enter matrix elements:
1253
1254
9258
Transpose of a matrix
1 1 9
2 2 2
5 5 5
3 4 8
Not possible to find the sum of principle and secondary diagonal.
Enter order of matrix: 4 4
Enter matrix elements:
2648
6254
6984
3165
Transpose of a matrix
2
6
4
8
6
2
5
4
6
9
8
4
3
1
6
5
#include<iostream.h>
#include<conio.h>
int m1[10][10];
int sym(int);
int skw(int);
int main()
{
int m,n;
clrscr();
cout<<"Enter order of matrix : " ;
cin>>m>>n;
if(m==n)
{
cout<<"Enter matrix elements : "<<endl;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>m1[i][j];
if(sym(m))
cout<<"Matrix is symmetric"<<endl;
else
if(skw(m))
cout<<"Matrix is skew symmetric"<<endl;
else
cout<<"Matrix is neither symmetric nor skew symmetric"<<endl;
}
else
cout<<"Not a square matrix";
getch();
return 0;
}
int sym(int c)
{
for(int i=0;i<c;i++)
for(int j=0;j<c;j++)
if(m1[i][j]!=m1[j][i])
return 0;
return 1;
}
int skw(int c)
{
for(int i=0;i<c;i++)
for(int j=0;j<c;j++)
if(m1[i][j]!=-m1[j][i])
return 0;
return 1;
}
Output:
Enter order of matrix: 2 4
Not a square matrix
Enter order of matrix: 4 4
Enter matrix elements:
1234
2567
3608
4789
Matrix is symmetric
Enter order of matrix: 3 3
Enter matrix elements:
0 2 -3
-2 0 4
3 -4 0
Matrix is skew symmetric
Enter order of matrix : 4 4
Enter matrix elements :
1234
6254
6548
3549
Matrix is neither symmetric nor skew symmetric
30.
clrscr();
sksy s;
cout<<"Enter order of matrix : " ;
if(s.read())
{
cout<<"Enter matrix elements : "<<endl;
s.readmat();
if(s.sym())
cout<<"Matrix is symmetric"<<endl;
else
if(s.skw())
cout<<"Matrix is skew symmetric"<<endl;
else
cout<<"Matrix is neither symmetric nor skew symmetric"<<endl;
}
else
cout<<"Not a square matrix";
getch();
return 0;
}
Output:
Enter order of matrix: 2 6
Not a square matrix
Enter order of matrix: 3 3
Enter matrix elements:
123
254
346
Matrix is symmetric
Enter order of matrix: 4 4
Enter matrix elements:
0 -2 3 4
2 0 -5 6
-3 5 0 7
-4 -6 -7 0
Matrix is skew symmetric
Enter order of matrix: 4 4
Enter matrix elements:
2458
6247
9248
6847
Matrix is neither symmetric nor skew symmetric
complex complex::mul(complex a)
{
complex b;
b.r=(r*a.r)-(im*a.im);
b.im=(r*a.r)+(im*a.im);
return (b);
}
complex complex::div(complex a)
{
complex b;
b.r=((r*a.r)+(im*a.im))/((a.r*a.r)+(a.im*a.im));
b.im=((r*a.r)-(im*a.im))/((a.r*a.r)+(a.im*a.im));
return (b);
}
int main()
{
complex c1,c2,c3;
clrscr();
cout<<"Enter the real and imaginary part first complex number : ";
c1.read();
cout<<endl<<"Enter the real and imaginary part second complex number : ";
c2.read();
c3=c1.add(c2);
cout<<endl<<"Addition of two complex number is :";
c3.dis();
c3=c1.sub(c2);
cout<<endl<<endl<<"Subtration of two complex number is :";
c3.dis();
c3=c1.mul(c2);
cout<<endl<<endl<<"Multiplication of two complex number is :";
c3.dis();
c3=c1.div(c2);
cout<<endl<<endl<<"Division of two complex number is :";
c3.dis();
getch();
return 0;
}
Output:
Enter the real and imaginary part first complex number: 3 4
Enter the real and imaginary part second complex number: 2 9
Addition of two complex number is: 5+i13
Subtration of two complex number is: 1-i5
Multiplication of two complex number is:-30+i42
Division of two complex number is: 0.494118-i0.352941
complex complex::operator*(complex a)
{
complex b;
b.r=(r*a.r)-(im*a.im);
b.im=(r*a.r)+(im*a.im);
return (b);
}
complex complex::operator/(complex a)
{
complex b;
b.r=((r*a.r)+(im*a.im))/((a.r*a.r)+(a.im*a.im));
b.im=((r*a.r)-(im*a.im))/((a.r*a.r)+(a.im*a.im));
return (b);
}
int main()
{
complex c1,c2,c3;
clrscr();
cout<<"Enter the real and imaginary part first complex number : ";
c1.read();
cout<<endl<<"Enter the real and imaginary part second complex number : ";
c2.read();
c3=c1+c2;
cout<<endl<<"Addition of two complex number is :";
c3.dis();
c3=c1-c2;
cout<<endl<<endl<<"Subtration of two complex number is :";
c3.dis();
c3=c1*c2;
cout<<endl<<endl<<"Multiplication of two complex number is :";
c3.dis();
c3=c1/c2;
cout<<endl<<endl<<"Division of two complex number is :";
c3.dis();
getch();
return 0;
}
Output:
Enter the real and imaginary part first complex number: 5 -7
Enter the real and imaginary part second complex number: 2 6
Addition of two complex number is: 7-i1
Subtration of two complex number is: 3-i13
Multiplication of two complex number is: 52-i32
Division of two complex number is:-0.8+i1.3
t.hr=(p/3600);
return(t);
}
int main()
{
time t1,t2,t3;
clrscr();
cout<<"Enter the first time in hr:min:sec = ";
t1.read();
cout<<endl<<"Enter the second time in hr:min:sec = ";
t2.read();
t3=t1.add(t2);
cout<<endl<<"Addition of two times in hr:min:sec = ";
t3.dis();
t3=t1.sub(t2);
cout<<endl<<"Difference of two times in hr:min:sec = ";
t3.dis();
getch();
return 0;
}
Output:
Enter the first time in hr: min: sec = 5 23 41
Enter the second time in hr: min: sec = 2 12 28
Addition of two times in hr: min: sec = 7:36:9
Difference of two times in hr: min: sec = 3:11:13
34.
t.se=p%60;
t.mi=(p%3600)/60;
t.hr=(p/3600);
return(t);
}
int main()
{
time t1,t2,t3;
clrscr();
cout<<"Enter the first time in hr:min:sec = ";
t1.read();
cout<<endl<<"Enter the second time in hr:min:sec = ";
t2.read();
t3=t1+t2;
cout<<endl<<"Addition of two times in hr:min:sec = ";
t3.dis();
t3=t1-t2;
cout<<endl<<"Difference of two times in hr:min:sec = ";
t3.dis();
getch();
return 0;
}
Output:
Enter the first time in hr: min: sec = 9 48 52
Enter the second time in hr: min: sec = 6 21 36
Addition of two times in hr: min: sec = 16:10:28
Difference of two times in hr: min: sec = 3:27:16
void student::read_rec()
{
read();
gotoxy(35,10);cin>>name;
name[0]=toupper(name[0]);
gotoxy(35,11);cin>>rol_no;
gotoxy(35,12);cin>>sub[0];
gotoxy(35,13);cin>>sub[1];
gotoxy(35,14);cin>>sub[2];
tot=sub[0]+sub[1]+sub[2];
gotoxy(35,15);cout<<tot;
}
void line(int k)
{
for(int i=0;i<k;i++)
cout<<"-";
}
int menu()
{
int k;
clrscr();
gotoxy(20,2);line(35);
gotoxy(28,4);cout<<" STUDENT RECORD ";
gotoxy(20,6);line(35);
gotoxy(22,8);cout<<"1 Create record";
gotoxy(22,9);cout<<"2 Display all record";
gotoxy(22,10);cout<<"3 Sort by name";
gotoxy(22,11);cout<<"4 Sort by total marks";
gotoxy(22,12);cout<<"5 Add record";
gotoxy(22,13);cout<<"6 Edit record";
gotoxy(22,14);cout<<"7 Search record by roll number";
gotoxy(22,15);cout<<"8 Search record by name";
gotoxy(22,16);cout<<"9 Delete record ";
gotoxy(21,17);cout<<"10 Exit";
gotoxy(20,19);line(35);
gotoxy(22,21);cout<<"Enter your choice : ";
cin>>k;
return (k);
}
void read()
{
gotoxy(20,8);line(35);
gotoxy(22,10);cout<<"Name
: ";
gotoxy(22,11);cout<<"Roll number : ";
gotoxy(22,12);cout<<"Subject 1 : ";
gotoxy(22,13);cout<<"Subject 2 : ";
gotoxy(22,14);cout<<"Subject 3 : ";
gotoxy(22,15);cout<<"Total
:";
gotoxy(20,17);line(35);
}
void dis_rec()
{
gotoxy(2,2);line(78);
gotoxy(4,4);cout<<"Rollno.";
gotoxy(16,4);cout<<"Name";
gotoxy(34,4);cout<<"Sub1";
gotoxy(44,4);cout<<"Sub2";
gotoxy(54,4);cout<<"Sub3";
gotoxy(64,4);cout<<"Total";
gotoxy(2,6);line(78);
}
void student::dis_data(int i)
{
gotoxy(7,8+i);cout<<rol_no;
gotoxy(17,8+i);cout<<name;
gotoxy(35,8+i);cout<<sub[0];
gotoxy(45,8+i);cout<<sub[1];
gotoxy(55,8+i);cout<<sub[2];
gotoxy(65,8+i);cout<<tot;
}
int main()
{
clrscr();
student st[10];
int n=0;
for(;;)
{
switch(menu())
{
case 1:clrscr();
gotoxy(22,12);cout<<"Enter the number of student : ";
cin>>n;
for(int i=0;i<n;i++)
{
clrscr();
st[k].dis_data(k);
gotoxy(2,9+k);line(78);
gotoxy(22,11+k);cout<<"Press any key to cont.... ";
getch();
break;
case 4:clrscr();
if(n==0)
{
gotoxy(22,12);cout<<"No record found"<<endl;
gotoxy(22,14);cout<<"Press any key to continue... ";
getch();
break;
}
else
dis_rec();
for(k=0;k<n;k++)
for(j=k+1;j<n;j++)
{
student s;
if(st[k]<st[j])
{
s=st[k];
st[k]=st[j];
st[j]=s;
}
}
for(k=0;k<n;k++)
st[k].dis_data(k);
gotoxy(2,9+k);line(78);
gotoxy(22,11+k);cout<<"Press any key to cont.... ";
getch();
break;
case 5: clrscr();
for(i=n;;i++)
{
clrscr();
char s;
n++;
gotoxy(22,6);cout<<"Enter the record of student "<<i+1;
st[i].read_rec();
gotoxy(22,19);cout<<"Do you want to add one more
record(y/n):";
cin>>s;
if(s=='n')
break;
gotoxy(22,20);cout<<"Press any key to cont.... ";
getch();
}
break;
case 6:clrscr();
char name1[15],r;
if(n==0)
{
gotoxy(22,12);cout<<"No record found"<<endl;
gotoxy(22,14);cout<<"Press any key to continue... ";
getch();
break;
}
else
gotoxy(22,6);cout<<"Enter the name of the student:";
cin>>name1;
name1[0]=toupper(name1[0]);
for(i=0;i<n;i++)
if(st[i]==name1)
{
clrscr();
dis_rec();
st[i].dis_data(0);
gotoxy(2,10);line(78);
gotoxy(22,12);cout<<"Do you want to edit this record?(y/n):";
cin>>r;
if(r=='y')
{
clrscr();
gotoxy(22,6);cout<<"Enter the record of student
"<<name1;
st[i].read_rec();
gotoxy(22,19);cout<<"Press any key to cont.... ";
getch();
break;
}
if(r=='n')
break;
}
if(i==n)
{
clrscr();
gotoxy(22,12);cout<<"The record is not found";
gotoxy(22,14);cout<<"Press any key to continue...";
getch();
break;
}
break;
case 7:clrscr();
int m;
if(n==0)
{
gotoxy(22,12);cout<<"No record found"<<endl;
gotoxy(22,14);cout<<"Press any key to continue... ";
getch();
break;
}
else
gotoxy(22,12);cout<<"Enter the rollno of the student:";
cin>>m;
for(i=0;i<n;i++)
if(st[i]==m)
{
clrscr();
dis_rec();
k=0;
st[i].dis_data(k);
gotoxy(2,10);line(78);
gotoxy(22,14);cout<<"Press any key to cont.... ";
getch();
break;
}
if(i==n)
{
clrscr();
gotoxy(22,12);cout<<"The record is not found";
gotoxy(22,14);cout<<"Press any key to continue...";
getch();
break;
}
break;
case 8:clrscr();
if(n==0)
{
gotoxy(22,12);cout<<"No record found"<<endl;
gotoxy(22,14);cout<<"Press any key to continue... ";
getch();
break;
}
else
gotoxy(22,12);cout<<"Enter the name of the student:";
cin>>name1;
name1[0]=toupper(name1[0]);
for(i=0;i<n;i++)
if(st[i]==name1)
{
clrscr();
dis_rec();
k=0;
st[i].dis_data(k);
gotoxy(2,10);line(78);
gotoxy(22,14);cout<<"Press any key to cont.... ";
getch();
break;
}
if(i==n)
{
clrscr();
gotoxy(22,12);cout<<"The record is not found";
gotoxy(22,14);cout<<"Press any key to continue...";
getch();
break;
}
break;
case 9:clrscr();
if(n==0)
{
gotoxy(22,12);cout<<"No record found"<<endl;
gotoxy(22,14);cout<<"Press any key to continue... ";
getch();
break;
}
else
gotoxy(22,12);cout<<"Enter the name of the student:";
cin>>name1;
name1[0]=toupper(name1[0]);
for(i=0;i<n;i++)
if(st[i]==name1)
{
clrscr();
dis_rec();
k=0;
st[i].dis_data(k);
gotoxy(2,10);line(78);
gotoxy(22,15);cout<<"Do you want to delete this record?
(y/n):";
cin>>r;
if(r=='y')
{
clrscr();
for(k=i;k<n;k++)
// if((k+1)<n)
st[k]=st[k+1];
n--; i--;
gotoxy(22,10);cout<<"The record is deleted "<<name1;
gotoxy(22,14);cout<<"Press any key to cont.... ";
getch();
break;
}
if(r=='n')
break;
}
if(i==n)
{
clrscr();
gotoxy(22,12);cout<<"The record is not found";
gotoxy(22,14);cout<<"Press any key to continue...";
getch();
break;
}
break;
case 10: exit(0);
default:
gotoxy(22,38); cout<<"Enter correct choice between 1 to 10";
gotoxy(22,40); cout<<"Press any key to continue";
break;
}
}
return (0);
}
Output:
--------------------------------------------------STUDENT RECORD
--------------------------------------------------1 Create record
2 Display all record
3 Sort by name
4 Sort by total marks
5 Add record
6 Edit record
7 Search record by roll number
8 Search record by name
9 Delete record
10 Exit
---------------------------------------Enter your choice: 1
Enter the number of student: 3
Enter the record of student 1
--------------------------------------------------Name
: manjesh
Roll number: 1
Subject 1 :19
Subject 2 :12
Subject 3 :15
Total
:46
--------------------------------------------------Press any key to cont....
Enter the record of student 2
--------------------------------------------------Name
: soma
Roll number: 2
Subject 1 :21
Subject 2 :10
Subject 3 :13
Total
: 44
--------------------------------------------------Press any key to cont....
6 Edit record
7 Search record by roll number
8 Search record by name
9 Delete record
10 Exit
--------------------------------------------------Enter your choice : 3
-----------------------------------------------------------------------------Rollno. Name
Sub1
Sub2
Sub3
Total
-----------------------------------------------------------------------------3
Dilip
10
10
10
30
1
Manjesh
19
12
15
46
2
Soma
21
10
13
44
-----------------------------------------------------------------------------Press any key to cont....
--------------------------------------------------STUDENT RECORD
--------------------------------------------------1 Create record
2 Display all record
3 Sort by name
4 Sort by total marks
5 Add record
6 Edit record
7 Search record by roll number
8 Search record by name
9 Delete record
10 Exit
--------------------------------------------------Enter your choice: 4
-----------------------------------------------------------------------------Rollno. Name
Sub1
Sub2
Sub3
Total
-----------------------------------------------------------------------------1
Manjesh
19
12
15
46
2
Soma
21
10
13
44
3
Dilip
10
10
10
30
-----------------------------------------------------------------------------Press any key to cont....
--------------------------------------------------STUDENT RECORD
--------------------------------------------------1 Create record
2 Display all record
3 Sort by name
4 Sort by total marks
5 Add record
6 Edit record
7 Search record by roll number
8 Search record by name
9 Delete record
10 Exit
---------------------------------------Enter your choice : 5
Enter the record of student 4
--------------------------------------------------Name
: nagaraju
Roll number: 4
Subject 1 :12
Subject 2 :12
Subject 3 :12
Total
: 36
--------------------------------------------------Do you want to add one more record(y/n): y
Enter the record of student 5
--------------------------------------------------Name
: manju
Roll number: 5
Subject 1 :11
Subject 2 :11
Subject 3 :11
Total
: 33
--------------------------------------------------Do you want to add one more record(y/n): n
-----------------------------------------------------------------------------Rollno. Name
Sub1
Sub2
Sub3
Total
-----------------------------------------------------------------------------1
Manjesh
19
12
15
46
2
Soma
21
10
13
44
3
Dilip
10
10
10
30
4
Nagaraj
12
12
12
36
5
Manju
11
11
11
33
------------------------------------------------------------------------------
4
Nagaraj
12
12
12
36
5
Manju
11
11
11
33
-----------------------------------------------------------------------------Press any key to cont....
--------------------------------------------------STUDENT RECORD
--------------------------------------------------1 Create record
2 Display all record
3 Sort by name
4 Sort by total marks
5 Add record
6 Edit record
7 Search record by roll number
8 Search record by name
9 Delete record
10 Exit
--------------------------------------------------Enter your choice: 7
Enter the rollno of the student: 4
-----------------------------------------------------------------------------Rollno. Name
Sub1
Sub2
Sub3
Total
-----------------------------------------------------------------------------4
Nagaraj
12
12
12
36
-----------------------------------------------------------------------------Press any key to cont....
Enter the rollno of the student:9
The record is not found
Press any key to continue...
--------------------------------------------------STUDENT RECORD
--------------------------------------------------1 Create record
2 Display all record
3 Sort by name
4 Sort by total marks
5 Add record
6 Edit record
Rollno. Name
Sub1
Sub2
Sub3
Total
-----------------------------------------------------------------------------1
Manjeshkumar 20
20
20
60
2
Soma
21
10
13
44
4
Nagaraj
12
12
12
36
5
Manju
11
11
11
33
-----------------------------------------------------------------------------Press any key to cont....
Enter the name of the student:soma
-----------------------------------------------------------------------------Rollno. Name
Sub1
Sub2
Sub3
Total
-----------------------------------------------------------------------------2
Soma
21
10
13
44
-----------------------------------------------------------------------------Do you want to delete this record?(y/n):n
-----------------------------------------------------------------------------Rollno. Name
Sub1
Sub2
Sub3
Total
-----------------------------------------------------------------------------1
Manjeshkumar 20
20
20
60
2
Soma
21
10
13
44
4
Nagaraj
12
12
12
36
5
Manju
11
11
11
33
-----------------------------------------------------------------------------Press any key to cont....
--------------------------------------------------STUDENT RECORD
--------------------------------------------------1 Create record
2 Display all record
3 Sort by name
4 Sort by total marks
5 Add record
6 Edit record
7 Search record by roll number
8 Search record by name
9 Delete record
10 Exit
----------------------------------------------------Enter your choice : 10
36. Program to perform handling of student record using inheritance
#include<iostream.h>
#include <stdlib.h>
#include<conio.h>
#include <ctype.h>
#include <string.h>
void read();
void dis_rec();
class record
{
protected:
int rol_no,tot,sub[4];
char name[20];
public:
record(){}
~record(){}
void read_rec();
void dis_data(int);
};
class student:public record
{
public:
student(){}
~student(){}
int operator==(char c[]);
int operator==(int);
int operator>(student);
int operator<(student);
};
int student::operator==(char c[])
{
return(strcmp(name,c)==0?1:0);
}
int student::operator==(int t)
{
return((rol_no==t)?1:0);
}
int student::operator>(student k)
{
return((strcmp(name,k.name)>0)?1:0);
}
int student::operator<(student k)
{
return((tot<k.tot)?1:0);
}
void record::read_rec()
{
read();
gotoxy(35,10);cin>>name;
name[0]=toupper(name[0]);
gotoxy(35,11);cin>>rol_no;
gotoxy(35,12);cin>>sub[0];
gotoxy(35,13);cin>>sub[1];
gotoxy(35,14);cin>>sub[2];
tot=sub[0]+sub[1]+sub[2];
gotoxy(35,15);cout<<tot;
}
void line(int k)
{
for(int i=0;i<k;i++)
cout<<"-";
}
int menu()
{
int k;
clrscr();
gotoxy(20,2);line(35);
gotoxy(28,4);cout<<" STUDENT RECORD ";
gotoxy(20,6);line(35);
gotoxy(22,8);cout<<"1 Create record";
gotoxy(22,9);cout<<"2 Display all record";
gotoxy(22,10);cout<<"3 Sort by name";
gotoxy(22,11);cout<<"4 Sort by total marks";
gotoxy(22,12);cout<<"5 Add record";
gotoxy(22,13);cout<<"6 Edit record";
gotoxy(22,14);cout<<"7 Search record by roll number";
gotoxy(22,15);cout<<"8 Search record by name";
gotoxy(22,16);cout<<"9 Delete record ";
gotoxy(21,17);cout<<"10 Exit";
gotoxy(20,19);line(35);
gotoxy(22,21);cout<<"Enter your choice : ";
cin>>k;
return (k);
}
void read()
{
gotoxy(20,8);line(35);
gotoxy(22,10);cout<<"Name
: ";
gotoxy(22,11);cout<<"Roll number : ";
gotoxy(22,12);cout<<"Subject 1 : ";
gotoxy(22,13);cout<<"Subject 2 : ";
gotoxy(22,14);cout<<"Subject 3 : ";
gotoxy(22,15);cout<<"Total
:";
gotoxy(20,17);line(35);
}
void dis_rec()
{
gotoxy(2,2);line(78);
gotoxy(4,4);cout<<"Rollno.";
gotoxy(16,4);cout<<"Name";
gotoxy(34,4);cout<<"Sub1";
gotoxy(44,4);cout<<"Sub2";
gotoxy(54,4);cout<<"Sub3";
gotoxy(64,4);cout<<"Total";
gotoxy(2,6);line(78);
}
void record::dis_data(int i)
{
gotoxy(7,8+i);cout<<rol_no;
gotoxy(17,8+i);cout<<name;
gotoxy(35,8+i);cout<<sub[0];
gotoxy(45,8+i);cout<<sub[1];
gotoxy(55,8+i);cout<<sub[2];
gotoxy(65,8+i);cout<<tot;
}
int main()
{
clrscr();
student st[10];
int n=0;
for(;;)
{
switch(menu())
{
case 1:clrscr();
st[k]=st[j];
st[j]=s;
}
}
for(k=0;k<n;k++)
st[k].dis_data(k);
gotoxy(2,9+k);line(78);
gotoxy(22,11+k);cout<<"Press any key to cont.... ";
getch();
break;
case 4:clrscr();
if(n==0)
{
gotoxy(22,12);cout<<"No record found"<<endl;
gotoxy(22,14);cout<<"Press any key to continue... ";
getch();
break;
}
else
dis_rec();
for(k=0;k<n;k++)
for(j=k+1;j<n;j++)
{
student s;
if(st[k]<st[j])
{
s=st[k];
st[k]=st[j];
st[j]=s;
}
}
for(k=0;k<n;k++)
st[k].dis_data(k);
gotoxy(2,9+k);line(78);
gotoxy(22,11+k);cout<<"Press any key to cont.... ";
getch();
break;
case 5: clrscr();
for(i=n;;i++)
{
clrscr();
char s;
n++;
}
if(i==n)
{
clrscr();
gotoxy(22,12);cout<<"The record is not found";
gotoxy(22,14);cout<<"Press any key to continue...";
getch();
break;
}
break;
case 7:clrscr();
int m;
if(n==0)
{
gotoxy(22,12);cout<<"No record found"<<endl;
gotoxy(22,14);cout<<"Press any key to continue... ";
getch();
break;
}
else
gotoxy(22,12);cout<<"Enter the rollno of the student:";
cin>>m;
for(i=0;i<n;i++)
if(st[i]==m)
{
clrscr();
dis_rec();
k=0;
st[i].dis_data(k);
gotoxy(2,10);line(78);
gotoxy(22,14);cout<<"Press any key to cont.... ";
getch();
break;
}
if(i==n)
{
clrscr();
gotoxy(22,12);cout<<"The record is not found";
gotoxy(22,14);cout<<"Press any key to continue...";
getch();
break;
}
break;
case 8:clrscr();
if(n==0)
{
gotoxy(22,12);cout<<"No record found"<<endl;
gotoxy(22,14);cout<<"Press any key to continue... ";
getch();
break;
}
else
gotoxy(22,12);cout<<"Enter the name of the student:";
cin>>name1;
name1[0]=toupper(name1[0]);
for(i=0;i<n;i++)
if(st[i]==name1)
{
clrscr();
dis_rec();
k=0;
st[i].dis_data(k);
gotoxy(2,10);line(78);
gotoxy(22,14);cout<<"Press any key to cont.... ";
getch();
break;
}
if(i==n)
{
clrscr();
gotoxy(22,12);cout<<"The record is not found";
gotoxy(22,14);cout<<"Press any key to continue...";
getch();
break;
}
break;
case 9:clrscr();
if(n==0)
{
gotoxy(22,12);cout<<"No record found"<<endl;
gotoxy(22,14);cout<<"Press any key to continue... ";
getch();
break;
}
else
gotoxy(22,12);cout<<"Enter the name of the student:";
cin>>name1;
name1[0]=toupper(name1[0]);
for(i=0;i<n;i++)
if(st[i]==name1)
{
clrscr();
dis_rec();
k=0;
st[i].dis_data(k);
gotoxy(2,10);line(78);
gotoxy(22,15);cout<<"Do you want to delete this record?
(y/n):";
cin>>r;
if(r=='y')
{
clrscr();
for(k=i;k<n;k++)
// if((k+1)<n)
st[k]=st[k+1];
n--; i--;
gotoxy(22,10);cout<<"The record is deleted "<<name1;
gotoxy(22,14);cout<<"Press any key to cont.... ";
getch();
break;
}
if(r=='n')
break;
}
if(i==n)
{
clrscr();
gotoxy(22,12);cout<<"The record is not found";
gotoxy(22,14);cout<<"Press any key to continue...";
getch();
break;
}
break;
case 10: exit(0);
default:
gotoxy(22,38); cout<<"Enter correct choice between 1 to 10";
gotoxy(22,40); cout<<"Press any key to continue";
break;
}
}
return (0);
}
Output:
--------------------------------------------------STUDENT RECORD
--------------------------------------------------1 Create record
2 Display all record
3 Sort by name
4 Sort by total marks
5 Add record
6 Edit record
7 Search record by roll number
8 Search record by name
9 Delete record
10 Exit
---------------------------------------Enter your choice: 1
Enter the number of student: 3
Enter the record of student 1
--------------------------------------------------Name
: manjesh
Roll number: 1
Subject 1 :19
Subject 2 :12
Subject 3 :15
Total
:46
--------------------------------------------------Press any key to cont....
Enter the record of student 2
--------------------------------------------------Name
: soma
Roll number: 2
Subject 1 :21
Subject 2 :10
Subject 3 :13
Total
: 44
--------------------------------------------------Press any key to cont....
--------------------------------------------------Name
: dilip
Roll number: 3
Subject 1 :10
Subject 2 :10
Subject 3 :10
Total
: 30
--------------------------------------------------Press any key to cont....
--------------------------------------------------STUDENT RECORD
--------------------------------------------------1 Create record
2 Display all record
3 Sort by name
4 Sort by total marks
5 Add record
6 Edit record
7 Search record by roll number
8 Search record by name
9 Delete record
10 Exit
---------------------------------------Enter your choice: 2
-----------------------------------------------------------------------------Rollno. Name
Sub1
Sub2
Sub3
Total
-----------------------------------------------------------------------------1
Manjesh
19
12
15
46
2
Soma
21
10
13
44
3
Dilip
10
10
10
30
-----------------------------------------------------------------------------Press any key to cont....
--------------------------------------------------STUDENT RECORD
--------------------------------------------------1 Create record
2 Display all record
3 Sort by name
4 Sort by total marks
5 Add record
6 Edit record
---------------------------------------------------
STUDENT RECORD
--------------------------------------------------1 Create record
2 Display all record
3 Sort by name
4 Sort by total marks
5 Add record
6 Edit record
7 Search record by roll number
8 Search record by name
9 Delete record
10 Exit
---------------------------------------Enter your choice : 5
Enter the record of student 4
--------------------------------------------------Name
: nagaraju
Roll number: 4
Subject 1 :12
Subject 2 :12
Subject 3 :12
Total
: 36
--------------------------------------------------Do you want to add one more record(y/n): y
Enter the record of student 5
--------------------------------------------------Name
: manju
Roll number: 5
Subject 1 :11
Subject 2 :11
Subject 3 :11
Total
: 33
--------------------------------------------------Do you want to add one more record(y/n): n
-----------------------------------------------------------------------------Rollno. Name
Sub1
Sub2
Sub3
Total
-----------------------------------------------------------------------------1
Manjesh
19
12
15
46
2
Soma
21
10
13
44
3
Dilip
10
10
10
30
4
Nagaraj
12
12
12
36
5
Manju
11
11
11
33
-----------------------------------------------------------------------------Press any key to cont....
--------------------------------------------------STUDENT RECORD
--------------------------------------------------1 Create record
2 Display all record
3 Sort by name
4 Sort by total marks
5 Add record
6 Edit record
7 Search record by roll number
8 Search record by name
9 Delete record
10 Exit
--------------------------------------------------Enter your choice : 6
Enter the name of the student: manjesh
-----------------------------------------------------------------------------Rollno. Name
Sub1
Sub2
Sub3
Total
-----------------------------------------------------------------------------1
Manjesh
19
12
15
46
-----------------------------------------------------------------------------Do you want to edit this record?(y/n):y
Enter the record of student Manjesh
--------------------------------------------------Name
: manjeshkumar
Roll number: 1
Subject 1 :20
Subject 2 :20
Subject 3 :20
Total
: 60
--------------------------------------------------Press any key to cont....
-----------------------------------------------------------------------------Rollno. Name
Sub1
Sub2
Sub3
Total
-----------------------------------------------------------------------------1
Manjeshkumar 20
20
20
60
2
Soma
21
10
13
44
3
Dilip
10
10
10
30
4
Nagaraj
12
12
12
36
5
Manju
11
11
11
33
-----------------------------------------------------------------------------Press any key to cont....
--------------------------------------------------STUDENT RECORD
--------------------------------------------------1 Create record
2 Display all record
3 Sort by name
4 Sort by total marks
5 Add record
6 Edit record
7 Search record by roll number
8 Search record by name
9 Delete record
10 Exit
--------------------------------------------------Enter your choice: 7
Enter the rollno of the student: 4
-----------------------------------------------------------------------------Rollno. Name
Sub1
Sub2
Sub3
Total
-----------------------------------------------------------------------------4
Nagaraj
12
12
12
36
-----------------------------------------------------------------------------Press any key to cont....
Enter the rollno of the student:9
The record is not found
Press any key to continue...
--------------------------------------------------STUDENT RECORD
--------------------------------------------------1 Create record
2 Display all record
3 Sort by name
4 Sort by total marks
5 Add record
6 Edit record
7 Search record by roll number
-----------------------------------------------------------------------------1
Manjeshkumar 20
20
20
60
2
Soma
21
10
13
44
4
Nagaraj
12
12
12
36
5
Manju
11
11
11
33
-----------------------------------------------------------------------------Press any key to cont....
Enter the name of the student:soma
-----------------------------------------------------------------------------Rollno. Name
Sub1
Sub2
Sub3
Total
-----------------------------------------------------------------------------2
Soma
21
10
13
44
-----------------------------------------------------------------------------Do you want to delete this record?(y/n):n
-----------------------------------------------------------------------------Rollno. Name
Sub1
Sub2
Sub3
Total
-----------------------------------------------------------------------------1
Manjeshkumar 20
20
20
60
2
Soma
21
10
13
44
4
Nagaraj
12
12
12
36
5
Manju
11
11
11
33
-----------------------------------------------------------------------------Press any key to cont....
--------------------------------------------------STUDENT RECORD
--------------------------------------------------1 Create record
2 Display all record
3 Sort by name
4 Sort by total marks
5 Add record
6 Edit record
7 Search record by roll number
8 Search record by name
9 Delete record
10 Exit
----------------------------------------------------Enter your choice : 10