PracSolutiondocx 1
PracSolutiondocx 1
PracSolutiondocx 1
#include <iostream.h>
int main()
{
int i,j;
char c;
int n=5;
c='A';
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
if(c=='Z')
break;
cout<<c;
c++;
}
cout<<endl;
}
return 0;
Assignment 2
/* Write a C++ program to accept length and width of a rectangle. Calculate and
display perimeter as well as area of a rectangle by using Inline function.*/
#include<iostream.h>
#include<conio.h>
class rectangle
{
float l,w;
public:
void getdata()
{
cout<<"\nEnter length of rectangle : ";
cin>>l;
cout<<"\nEnter width of rectangle : ";
cin>>w;
}
inline void Peri()
{
cout<<"\nPerimeter of rectangle = "<<2*(l+w);
}
inline void Area()
{
cout<<"\nArea of rectangle = "<<l*w;
}
};
void main()
{
clrscr();
rectangle obj;
obj.getdata();
obj.Peri();
obj.Area();
getch();
}
/* Write a C++ program to define power function to calculate x^y.(Use default
value as 2 for y).*/
#include <iostream.h>
#include<conio.h>
int cal(int x, int y=2)
{
int i, result=1;
cout<<"Enter x"<<endl;
cin>>x;
for(i = 0; i<y; i++){
result = result * x;
}
cout << x << "^" << y << " = " << result;
return result;
}
void main()
{
int a;
clrscr();
cal(a);
getch();
}
/* Write a C++ program to calculate area and circumference of a circle.(Use
default argument, scope resolution operator and manipulator.)*/
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
float p=3.14;
float area(float r=2.8)
{
cout<<setprecision(2)<<"Area of circile with derfault argument=
"<<::p*r*r<<endl;
return 0;
}
float circ(float r)
{
cout<<setprecision(2)<<"Circumference="<<::p*r*2<<endl;
return 0;
}
void main()
{
clrscr();
area();
circ(8);
getch();
Assignment 3
/* Write a C++ program to calculate the average height of all the students of a
class. The number of students and their heights are entered by user.(Use array of
objects) */
# include <iostream.h>
#include<conio.h>
class Student {
public:
int roll_no, height;
void get_height() {
cout << "Enter roll number: ";
cin >> roll_no;
cout << "Enter height: ";
cin >> height;
cout << endl;
}
};
void main() {
Student s[10];
clrscr();
int n, total = 0;
float average;
average = total / n;
getch();
}
class Test2
{
int b;
public: void getdata()
{
cout<<"Enter Second Number"<<endl;
cin>>b;
}
friend void big(Test1, Test2);
friend void min(Test1, Test2);
};
void big(Test1 t1, Test2 t2)
{
if(t1.a>t2.b)
cout<<" Maximum number "<<t1.a<<endl;
else if(t2.b>t1.a)
cout<<"Maximum number"<<t2.b<<endl;
/* else if(t1.a<t2.b)
cout<<"a is small";
else if(t2.b<t1.a)
cout<<"b is small"; */
else
cout<<"a and b are equal"<<endl;
}
void min(Test1 t1, Test2 t2)
{
if(t1.a<t2.b)
cout<<"Minimum number"<<t1.a<<endl;
else if(t2.b<t1.a)
cout<<"Minimum Number "<<t2.b<<endl;
else
cout<<"Both are equal" <<endl;
}
void main()
{
Test1 t1;
Test2 t2;
clrscr();
t1.getdata();
t2.getdata();
big(t1,t2);
min(t1,t2);
getch();
}
/* Write a C++ program to create a class Distance with data members feet and
inches. Write member functions for the following.
a. To accept distance.
b. To display distance
c. To add two distance objects.
(Use object as a function argument and function returning objects)*/
#include<iostream.h>
#include<conio.h>
class Distance
{
private:
int feet;
int inches;
public:
void set_distance()
{
cout<<"Enter feet: ";
cin>>feet;
cout<<"Enter inches: ";
cin>>inches;
}
void get_distance()
{
cout<<"Distance is feet= "<<feet<<", inches=
"<<inches<<endl;
}
Distance add(Distance d1, Distance d2)
{
Distance d3;
d3.feet = d1.feet + d2.feet;
d3.inches = d1.inches + d2.inches;
d3.feet = d3.feet + (d3.inches / 12);
d3.inches = d3.inches % 12;
return(d3);
}
};
void main()
{
Distance d1, d2, d3;
clrscr();
d1.set_distance();
d2.set_distance();
d3=d1.add(d1, d2);
d3.get_distance();
getch();
}
Assignment 4
/* Write a c++ program to create a class. MyDate with three data member as
dd,mm,yyyy. Create and initialize the object by using parameterized constructor
and display date in dd-mon-yyyy.(input: 19-12-2014 Output: 19 dec-2014)(Use
the concept of dynamic initialization of object)*/
#include<iostream.h>
#include<conio.h>
class date
{
int dd,mm,yy;
public: date(int d,int m,int y)
{
dd=d;
mm=m;
yy=y;
}
void display()
{
cout<<"\ngiven date is\t";
cout<<dd<<"-"<<mm<<"-"<<yy;
cout<<"\nAfter formating date is\t";
switch(mm)
{
case 1:
cout<<"\n"<<dd<<"-Jan-"<<yy;
break;
case 2:
cout<<"\n"<<dd<<"-Feb-"<<yy;
break;
case 3:
cout<<"\n"<<dd<<"-Mar-"<<yy;
break;
case 4:
cout<<"\n"<<dd<<"-Apr-"<<yy;
break;
case 5:
cout<<"\n"<<dd<<"-May-"<<yy;
break;
case 6:
cout<<"\n"<<dd<<"-Jun-"<<yy;
break;
case 7:
cout<<"\n"<<dd<<"-Jul-"<<yy;
break;
case 8:
cout<<"\n"<<dd<<"-Aug-"<<yy;
break;
case 9:
cout<<"\n"<<dd<<"-Sep-"<<yy;
break;
case 10:
cout<<"\n"<<dd<<"-Oct-"<<yy;
break;
case 11:
cout<<"\n"<<dd<<"-Nov-"<<yy;
break;
case 12:
cout<<"\n"<<dd<<"-Dec-"<<yy;
break;
default:
cout<<"\nInvalid month";
}
}
};
void main()
{
clrscr();int m,dt,y;
cout<<"\n Enter date : ";
cin>>dt;
cout<<"\n Enter month : ";
cin>>m;
cout<<"\n Enter year : ";
cin>>y;
date d(dt,m,y);
d.display();
getch();
}
/* Write a C++ program to create a class ‘MyMatrix’ which contains two
dimensional integer array of size mXn. Write a member function to display sum
of all elements of entered matrix.(Use Dynamic constructor for allocating
memory and destructor to free memory of an object)*/
#include<conio.h>
#include<iostream.h>
main()
{
int r,c,sum=0;
clrscr();
cout<<"\n Enter dimension ";
cin>>r;
cin>>c;
int **a;
a=new int*[r];//[s];
for(int i=0;i<r;i++)
{ a[i]=new int[c];
}
for(i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout<<"\n enter data : ";
cin>>a[i][j];
}
}
for(i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout<<"\t"<<a[i][j];
}
cout<<endl;
}
for(i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
sum=sum+a[i][j];
}
}
cout<<"\n sum of all elements of matrix : "<<sum;
getch();
}
Assignment 5
/*Define a class Product that contains data member as Prod_no, Prod_Name,
Prod_Price. Derive a class Discount(discount_in_Percentage) from class
Product. A Customer buys ͚n͛ products. Accept quantity for each product ,
calculate Total Discount and accordingly generate Bill. Display the bill using
appropriate Manipulators*/
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class Product
{ public:
int no;
char pname[20];
float price;
public :
void accept()
{ cout<<"\n Enter product id : ";
cin>>no;
cout<<"\n Enter product name : ";
cin>>pname;
cout<<"\n Enter product price : ";
cin>>price;
}
};
class Discount : public Product
{
public :
int d;
void accept_D()
{
cout<<"\n Enter product discount : ";
cin>>d;
}
void display()
{
cout<<setiosflags(ios::left)<<setw(15)<<pname<<setw(10)<<price<<setiosflag
s(ios::right)<<s
etw(10)<<d<<endl;
}
};
void main()
{
Discount ob[10];
int i,n,total=0,discnt=0;
char cname[20];clrscr();
cout<<"\n Enter name of customer : ";
cin>>cname;
cout<<"\n Enter no product to be puraches : ";
cin>>n;
for(i=0;i<n;i++)
{
ob[i].accept();
ob[i].accept_D();
}
cout<<"\n **********YOUR BILL**************\n";
cout<<"\n Name of customer : "<<cname<<endl;
cout<<setiosflags(ios::left)<<setw(15)<<"Product name
"<<setw(10)<<"Price"<<setiosflags(ios::right)<<setw(10)<<"Discount"<<endl;
for(i=0;i<n;i++)
{
ob[i].display();
total=total+ob[i].price;
discnt=discnt+((ob[i].price * ob[i].d)/100);
}
cout<<"\n=====================================\n";
cout<<"Total : ";
cout<<setw(25)<<setiosflags(ios::right)<< total-discnt;
getch();
}
/* Create a base class Student(Roll_No, Name, Class) which derives two classes
Internal_Marks(IntM1, IntM2, IntM3, IntM4, IntM5) and
External_Marks(ExtM1 ExtM2, ExtM3, ExtM4, ExtM5). Class Result(T1, T2,
T3, T4, T5) inherits both Internal_Marks and External_Marks classes. (Use
Virtual Base Class) Write a C++ menu driven program to perform the following
functions: To Accept and display student details Calculate Subject wise total
marks obtained. Check whether student has passed in Internal and External
Exam of each subject. Also check whether he has passed in respective subject or
not and display result accordingly. */
#include<iostream.h>
#include<conio.h>
#include<string.h>
class student
{
protected:
int rno;
char name[10],cls[10];
public:
void accept()
{
cout<<"\n Enter roll no if sudent : ";
cin>>rno;
cout<<"\n Enter the name of student :";
cin>>name;
cout<<"\n Enter the class of student :";
cin>>cls;
}
void display()
{
cout<<"\n Roll no. :"<<rno<<"\n Name :"<<name<<"\n class : "<<cls<<endl;
}
};
class Internal_Mark:public virtual student
{
protected:
int i_mark[6];
public:
int internal_mark()
{ int i;
cout<<"\n Enter the 6 subjects marks :";
for(i=0;i<6;i++)
{ cout<<"subject "<<i+1<<" : ";
cin>>i_mark[i];
}
for(i=0;i<6;i++)
{ if(i_mark[i]>20)
return 1;
}
return 0;
}
void i_display()
{
cout<<"\n Internal marks is : \n ";
for(int i=0;i<6;i++)
{
cout<<"subject "<<i+1<<"\t"<<i_mark[i]<<endl;
}
}
};
class External_Mark:public virtual student
{
protected: int e_mark[6];
public:
int external_mark()
{ int i;
cout<<"\n Enter the 6 subjects External marks :";
for(i=0;i<6;i++)
{ cout<<"\n subject "<<i+1<<" : ";
cin>>e_mark[i];
}
for(i=0;i<6;i++)
{ if(e_mark[i]>80 || e_mark[i]<32)
return 1;
}
return 0;
}
void e_display()
{
cout<<"\n eXternal marks are : \n ";
for(int i=0;i<6;i++)
{
cout<<"subject "<<i+1<<"\t"<<e_mark[i]<<endl;
}
}
};
class result: public Internal_Mark,public External_Mark
{
int total[6];
char grade[10];
float per;
public:
void cal_res()
{ for(int i=0;i<6;i++)
{
total[i]=i_mark[i]+e_mark[i];
}
}
void dis_res()
{
display();
i_display();
e_display();
cout<<"Total marks : \n ";
for(int i=0;i<6;i++)
{cout<<"subject "<<i+1<<"\t"<<total[i]<<endl;}
}
};
int main()
{
clrscr();
int n,ch,a,b;
result obj;
do
{
cout<<"\n 1.student info \n 2.Internal mark \n 3.Extranal mark \n 4. Result \n 0.
exit";
cout<<"\n Enter your choice : ";
cin>>ch;
switch(ch)
{ case 1: obj.accept();
break;
case 2: a=obj.internal_mark();
break;
case 3: b=obj.external_mark();
break;
case 4: if(a==0 && b==0)
{obj.cal_res();
obj.dis_res();}
else cout<<"Fail";
break;
case 0: break;
default: cout<<"\n Invalid choice : ";
}
}while(ch!=0);
getch();
}
Assignment 6
/* Write a C++ program to create a class Integer. Write necessary member
functions to overload the operator unary pre and post decrement -͚ -͛ for an integer
number.*/
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
class integer
{
int no;
public:
integer(){}
integer(int num)
{
no=num;
}
integer operator--()
{
--no;
return *this;
}
integer operator--(int) //post increment
{
integer temp=*this;
no--;
return temp;
}
void display()
{
cout<<"
No = "<<no;
}
};
void main()
{
integer i(10),i1(10),i3;
clrscr();
cout<<"
After postdecrement : ";
i3=i--;
i3.display();
cout<<"
After predecrement : ";
i3=--i1;
i3.display();
getch();
}
/* Write a C++ program to calculate area of cone, sphere and circle by using
function overloading.*/
#include<iostream.h>
#include<conio.h>
class calculate
{
float r_cn,r_s,side;
int r_c;
public:
void area(float r, float s)
{
r_c=r;
side=s;
cout<<"
Area of cone = "<<3.14*r_c*side;
}
void area(float r)
{
r_s=r;
cout<<"
Area of sphere = "<<4*3.14*r_s*r_s;
}
void area(int r)
{
r_c=r;
cout<<"
Area of circle = "<<3.14*r_c*r_c;
}
};
void main()
{
float r_cone,side;
int r_circle;
float sp;
clrscr();
calculate c;
cout<<"
Enter radius of cone ";
cin>>r_cone;
cout<<"
Enter side of cone ";
cin>>side;
cout<<"
Enter radius of sphere ";
cin>>sp;
cout<<"
Enter radius of circle ";
cin>>r_circle;
c.area(r_cone,side);
c.area(sp);
c.area(r_circle);
getch();
}
#include<conio.h>
#include<iostream.h>
#include<string.h>
class Mystring
{
char *str;
int len;
public:
Mystring()
{}
Mystring(char s[])
{
len=strlen(s);
str=new char[len+1];
strcpy(str,s);
}
void display()
{
cout<<endl<<"string : "<<str;
}
void operator>(Mystring ob)
{
int len1,len2;
len1=strlen(str);
len2=strlen(ob.str);
if(len1>len2)
cout<<endl<<str<<" is max length ";
else
cout<<endl<<ob.str<<" is max length ";
}
void operator!=(Mystring ob)
{
if(strcmp(str,ob.str)==0)
cout<<endl<<"strings are same";
else
cout<<endl<<"strings are not same";
}
Mystring operator+(Mystring ob)
{
Mystring a;
int len1,len2;
len1=strlen(str);
len2=strlen(ob.str);
a.str=new char[len1+len2+1];
strcpy(a.str,str);
strcat(a.str,ob.str);
return a;
}
};
void main()
{
char s1[10],s2[20];
clrscr();
cout<<" Enter string : ";
cin>>s1;
cout<<"Enter 2nd string : ";
cin>>s2;
Mystring m1(s1),m2(s2),m3;
m1.display();
m2.display();
m1>m2;
m1!=m2;
m3=m1+m2;
m3.display();
getch();
}
/* Create a base class Shape. Derive three different classes Circle, Rectangle
and
Triangle from Shape class. Write a C++ program to calculate area of Circle,
Rectangle and Triangle.(Use pure virtual function)*/
#include<iostream.h>
#include<conio.h>
class shape
{
public:
virtual void accept()=0;
virtual void area()=0;
};
class circle:public shape
{
public:
int r;
void accept()
{
cout<<"
Enter radius for circle : ";
cin>>r;
}
void area()
{
cout<<"
Area of circle="<<3.14*r*r;
}
};
class rectangle:public shape
{
public:
int l,b;
void accept()
{
cout<<"
Enter length and breadth for rectangle : ";
cin>>l;
cin>>b;
}
void area()
{
cout<<"
Area of rectangle="<<l*b;
}
};
class triangle:public shape
{
public:
int b,h;
void accept()
{
cout<<"
Enter base and height for triangle : ";
cin>>b;
cin>>h;
}
void area()
{
cout<<"
Area of triangle="<<0.5*b*h;
}
};
void main()
{
triangle t;
circle c;
rectangle r;
shape *ptr;
clrscr();
ptr=&t;
ptr->accept();
ptr->area();
ptr=&c;
ptr->accept();
ptr->area();
ptr=&r;
ptr->accept();
ptr->area();
getch();
}
Assignment 7
/* Write a C++ program to create a class Employee which contains data
members as Emp_Id, Emp_Name, Basic_Salary, HRA, DA, Gross_Salary.
Write member functions to accept Employee information. Calculate and display
Gross salary of an employee. (DA=12% of Basic salary and HRA = 30% of
Basic salary) (Use appropriate manipulators to display employee information in
given format :- Emp_Id and Emp_Name should be left justified and
Basic_Salary, HRA, DA, Gross salary Right justified with a precision of two
digits) */
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class emp
{
int eid;
float bs,hra,da,gs;
char name[10];
public:
void accept()
{
cout<<"enter id";
cin>>eid;
cout<<"enter name";
cin>>name;
cout<<"enter bs";
cin>>bs;
}
void display()
{
cout<<"
eid="<<eid<<"
name="<<name<<"
bssic salary = "<<bs;
cout<<"
HRA = "<<(bs*0.30);
cout<<"
DA = "<<(bs*0.12);
cout<<"
Gross Salary = "<<(bs-((bs*0.12)+(bs*0.30)));
}
};
void main()
{
emp ob;
clrscr();
ob.accept();
ob.display();
getch();
}
Assignment 8
/* Write a C++ program to read the contents of a text file. Count and display
number of characters, words and lines from a file. Find the number of
occurrences of a given word present in a file.*/
#include <iostream.h>
#include<fstream.h>
#include<stdlib.h>
#include<string.h>
main()
{
char character,file[20],word[20];
ifstream in;
int charcnt,wcnt,lcnt; //clrscr();
cout<<"\n Enter file name : ";
cin>>file;
in.open(file);
if(in.fail())
{
cout<<"\n file does not exists :"<<endl;
exit(1);
}
charcnt = 0;
lcnt = 0;
wcnt = 0;
char ch;
while(!in.eof())
{
ch=in.get();
if(ch == '\n')
{
lcnt++; wcnt++;
}
else if(ch==' ' ||ch=='\t' )
{wcnt++; charcnt++;
}
else charcnt++;
}
cout << "Characters: " << charcnt << endl;
cout << "Lines: " << lcnt << endl;
cout << "Words: " << wcnt << endl;
in.close();
in.open(file);
Assignment 9
/* Write a C++ program to swap two integer values and two float values by
using function template.*/
#include<conio.h>
#include<iostream.h>
template<class T>
void swap(T &n1,T &n2)
{
T temp;
temp=n1;
n1=n2;
n2=temp;
}
void main()
{ int no1,no2;
float no3,no4;
clrscr();
cout<<" Enter 2 interger no : ";
cin>>no1>>no2;
cout<<"Before swapping : ";
cout<<"no1 = "<<no1 <<" no2 = "<<no2;
swap(no1,no2);
cout<<"After swapping : ";
cout<<"
no1 = "<<no1 <<" no2 = "<<no2;
cout<<"Enter 2 float no : ";
cin>>no3>>no4;
cout<<"Before swapping : ";
cout<<"no1 = "<<no3 <<" no2 = "<<no4;
swap(no3,no4);
cout<<"After swapping : ";
cout<<"no1 = "<<no3 <<" no2 = "<<no4;
getch();
}
/* Write a C++ program to find maximum of two integer numbers and two float
numbers by using function template.*/
#include<conio.h>
#include<iostream.h>
template<class T>
T max(T n1,T n2)
{
if(n1>n2)
return n1;
else
return n2;
}
void main()
{ int no1,no2;
float no3,no4;
clrscr();
cout<<"Enter 2 interger no : ";
cin>>no1>>no2;
cout<<"no1 = "<<no1 <<" no2 = "<<no2;
cout<<" maximum = "<<max(no1,no2);
cout<<"Enter 2 float no : ";
cin>>no3>>no4;
cout<<"no1 = "<<no3 <<" no2 = "<<no4;
cout<<"maximum = "<<max(no3,no4);
getch();
}