Professional Documents
Culture Documents
C++ - Practicals Solution - 1 To 9
C++ - Practicals Solution - 1 To 9
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
cout<<"Hello";
getch();
return 0;
}
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
float a, b;
cout<<"Enter a:";
cin>>a;
cout<<"Enter b:";
cin>>b;
cout<<"a="<<a<<"\n"<<"b="<<b;
getch();
return 0;
}
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
float a, b, avg;
cout<<"Enter a:";
cin>>a;
cout<<"Enter b:";
cin>>b;
cout<<"Enter c:";
cin>>c;
avg=(a+b+c)/3;
cout<<"Average="<<avg;
getch();
return 0;
}
#include<iostream.h>
#include<conio.h>
enum week{Mon=1,Tue,Wed,Thur,Fri,Sat,Sun};
int main()
{
clrscr();
week day;
day= Fri;
cout<<"Day:"<<day+1<endl;
getch();
return 0;
}
2.5 Defining Constant
#include <iostream.h>
#include<conio.h>
int main ()
{
const int SIDE = 50;
int area;
area = SIDE*SIDE;
cout<<"The area of the square with side: " << SIDE <<" is: " << area << endl;
return 0;
}
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int a[5],i;
cout<<"Enter 5 elements of array:\n";
for(i=0;i<5;i++)
{
cout<<"a["<<i<<"]=";
cin>>a[i];
}
for(i=0;i<5;i++)
{
cout<<"a["<<i<<"]="<<a[i]<<endl;
}
getch();
return 0;
}
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int a[5],i,j,temp;
cout<<"Enter elements of array:\n";
for(i=0;i<5;i++)
{
cout<<"a["<<i<<"]=";
cin>>a[i];
}
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
cout<<"array elements are:"<<endl;
for(i=0;i<5;i++)
{
cout<<"a["<<i<<"]="<<a[i]<<endl;
}
getch();
return 0;
}
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int a[5], max, i;
cout<<"Enter elements of array:\n";
for(i=0;i<5;i++)
{
cout<<"a["<<i<<"]=";
cin>>a[i];
}
max=a[i];
for(i=0;i<5;i++)
{
if(a[i]>max)
{
max=a[i];
}
}
cout<<"Maximum="<<max<<endl;
getch();
return 0;
}
4.2 Write a C++ program to find largest no from 3 numbers using logical operators.
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int a,b,c;
cout<<"Enter a, b and c:";
cin>>a>>b>>c;
if(a>b && a>c)
{
cout<<a<<" is max";
}
else if(b>a && b>c)
{
cout<<b<<" is max";
}
else
{
cout<<c<<" is max";
}
getch();
return 0;
}
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int a;
cout<<"Enter number:";
cin>>a;
if(a%2==0)
{
cout<<a<<" is even";
}
else
{
cout<<a<<" is odd";
}
getch();
return 0;
}
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int a=10;
// cout<<endl<<sizeof(a);
cout<<endl<<"Size of int:"<<sizeof(int);
cout<<endl<"size of char:"<<sizeof(char);
cout<<endl<<sizeof(float);
cout<<endl<<sizeof(double);
cout<<endl<<sizeof(long int);
getch();
return 0;
}
4.5 Write a C++ program for bitwise operators.
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int a,b;
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int a,b,c;
cout<<"enter a and b:";
cin>>a>>b;
c=(a>b)?a:b;
cout<<c;
getch();
return 0;
}
4.7 Write a C++ program for memory management operators(new and delete).
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int *p=new int(10);
cout<<*p;
delete p;
p=new int[10];
*(p+1)=30
cout<<*(p+1);
delete (p+1);
getch();
return 0;
}
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int spi;
cout<<"enter spi:";
cin>>spi;
switch(spi)
{
case 10:
case 9:
cout<<"A";
break;
case 8:
case 7:
cout<<"B";
break;
case 6:
case 5:
cout<<"C";
break;
case 4:
cout<<"D";
break;
default:
cout<<"Fail";
}
getch();
return 0;
}
5.2 Write a C++ program to find factorial of number using while loop.
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int n,i=1,fact=1;
cout<<"Enter number:";
cin>>n;
while(i<=n)
{
fact=fact*i;
i++;
}
cout<<"Factorial="<<fact;
getch();
return 0;
}
5.3 Write a C++ program to display fibonaci series using do…while loop.
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int a,b,c,n,i=1;
cout<<"Enter a and b:";
cin>>a>>b;
cout<<"enter length of fibonaci series:";
cin>>n;
cout<<a<<" "<<b;
do
{
c=a+b;
a=b;
b=c;
cout<<" "<<c;
i++;
}while(i<=n);
getch();
return 0;
}
5.4 Write a C++ program to find reverse number using while loop.
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int n,rev=0;
cout<<"Enter number:";
cin>>n;
while(n>0)
{
rev=(rev*10)+(n%10);
n=n/10;
}
cout<<"reverse no:"<<rev;
getch();
return 0;
}
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int n,i,j;
cout<<"Enter number:";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
cout<<j<<" ";
}
cout<<endl;
}
getch();
return 0;
}
6. Develop programs using reference variable, scope resolution operator and simple manipulators.
6.1 Reference variable
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int sum=100;
int &total=sum;
cout<<"sum="<<sum<<endl;
cout<<"total="<<total<<endl;
total=total+100;
cout<<"sum="<<sum<<endl;
cout<<"total="<<total<<endl;
getch();
return 0;
}
#include<iostream.h>
#include<conio.h>
int a=10;
int main()
{
clrscr();
int a=20;
cout<<"Local a="<<a<<endl;
cout<<"global a="<<::a;
getch();
return 0;
}
6.3 Manipulators
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
int main()
{
clrscr();
int a=10;
float b=12.5456577;
cout<<a<<endl;
cout<<setw(5)<<a<<endl;
cout<<setw(10)<<setfill('*')<<"Hello"<<endl;
cout<<setprecision(2)<<b;
getch();
return 0;
}
7. Develop programs using call by reference and return by reference, default arguments, constant
arguments, and function overloading.
#include<iostream.h>
#include<conio.h>
void swap(int &,int &);
int main()
{
clrscr();
int a,b;
cout<<"Enter a:";
cin>>a;
cout<<"Enter b:";
cin>>b;
swap(a,b);
cout<<"Afetr swapping in main function:"<<endl;
cout<<"a="<<a<<endl;
cout<<"b="<<b;
getch();
return 0;
}
#include<iostream.h>
#include<conio.h>
void swap(int *,int *);
int main()
{
clrscr();
int a,b;
cout<<"Enter a:";
cin>>a;
cout<<"Enter b:";
cin>>b;
swap(&a,&b);
cout<<"Afetr swapping in main function:"<<endl;
cout<<"a="<<a<<endl;
cout<<"b="<<b;
getch();
return 0;
}
#include<iostream.h>
#include<conio.h>
int & min(int &,int &);
int main()
{
clrscr();
int a,b,c;
cout<<"Enter a:";
cin>>a;
cout<<"Enter b:";
cin>>b;
c=min(a,b);
cout<<"Min="<<c;
getch();
return 0;
}
int & min(int &x, int &y)
{
if(x<y)
return x;
else
return y;
}
#include<iostream.h>
#include<conio.h>
void sum(int,int,int c=30);
int main()
{
clrscr();
int a,b;
cout<<"Enter a:";
cin>>a;
cout<<"Enter b:";
cin>>b;
sum(a,b);
getch();
return 0;
}
void sum(int a, int b, int c)
{
int s=a+b+c;
cout<<"sum="<<s;
}
#include<iostream.h>
#include<conio.h>
void sum(int,int);
void sum(int,int,int);
int main()
{
clrscr();
int a=50,b=20,c=30;
sum(a,b);
sum(a,b,c);
getch();
return 0;
}
void sum(int x,int y)
{
cout<<"sum="<<x+y<<endl;
}
void sum(int x,int y,int z)
{
cout<<"sum="<<x+y+z;
}
#include<iostream.h>
#include<conio.h>
class student
{
private:
int no;
char name[20];
int per;
public:
void getdata()
{
cout<<"enter no:";
cin>>no;
cout<<"Enter name:";
cin>>name;
cout<<"enter Per:";
cin>>per;
}
void putdata()
{
cout<<"No="<<no<<endl;
cout<<"Name="<<name<<endl;
cout<<"Percentage="<<per;
}
};
int main()
{
clrscr();
student s1;
s1.getdata();
s1.putdata();
getch();
return 0;
}
8.2 program of class and object to find area and volume of room.
#include<iostream.h>
#include<conio.h>
class Room
{
private:
float length,width,height;
public:
void input(float l,float w, float h)
{
length=l;
width=w;
height=h;
}
void area()
{
float a;
a=length*width;
cout<<"area="<<a<<endl;
}
void volume()
{
cout<<"Volume="<<length*width*height;
}
};
int main()
{
clrscr();
Room R1;
R1.input(10.25,15.35,20.50);
R1.area();
R1.volume();
getch();
return 0;
}
#include<iostream.h>
#include<conio.h>
class student
{
private:
int no;
char name[20];
int per;
public:
void getdata();
void putdata();
};
void student::getdata()
{
cout<<"enter no:";
cin>>no;
cout<<"Enter name:";
cin>>name;
cout<<"enter Per:";
cin>>per;
}
void student::putdata()
{
cout<<"No="<<no<<endl;
cout<<"Name="<<name<<endl;
cout<<"Percentage="<<per;
}
int main()
{
clrscr();
student s1;
s1.getdata();
s1.putdata();
getch();
return 0;
}
#include<iostream.h>
#include<conio.h>
class Sample
{
private:
int a,b;
void read()
{
cout<<"enter a and b:";
cin>>a>>b;
}
public:
void write()
{
read();
int ans=a*b;
cout<<"Ans="<<ans;
}
};
int main()
{
clrscr();
Sample s;
s.write();
getch();
return 0;
}
#include<iostream.h>
#include<conio.h>
class Sample
{
private:
int a,b;
public:
void read()
{
cout<<"enter a and b:";
cin>>a>>b;
}
void write()
{
read();
int ans=a*b;
cout<<"Ans="<<ans;
}
};
int main()
{
clrscr();
Sample s;
//s.read();
s.write();
getch();
return 0;
}
8.5 Declaring data member of class as public
#include<iostream.h>
#include<conio.h>
class Employee
{
public:
int no,sal;
void display()
{
cout<<"employee no="<<no<<endl;
cout<<"Employee salary="<<sal<<endl;
}
};
int main()
{
clrscr();
Employee E1,E2;
cout<<"Enter employee no ans salary:";
cin>>E1.no;
cin>>E1.sal;
E2.no=2;
E2.sal=30000;
E1.display();
E2.display();
getch();
return 0;
}
#include<iostream.h>
#include<conio.h>
class student
{
private:
int no;
char name[20];
int per;
public:
void getdata()
{
cout<<"enter no:";
cin>>no;
cout<<"Enter name:";
cin>>name;
cout<<"enter Per:";
cin>>per;
}
void putdata()
{
cout<<"No="<<no<<endl;
cout<<"Name="<<name<<endl;
cout<<"Percentage="<<per<<endl;
}
};
int main()
{
clrscr();
student s[3];
for(int i=0;i<3;i++)
{
s[i].getdata();
}
for(i=0;i<3;i++)
{
s[i].putdata();
}
getch();
return 0;
}
#include<iostream.h>
#include<conio.h>
class Item
{
int no;
static int count;
public:
void getdata(int x)
{
no=x;
count++;
}
static void getcount()
{
cout<<"count="<<count<<endl;
}
void putdata()
{
cout<<"No="<<no<<endl;
}
};
int Item::count;
int main()
{
clrscr();
Item a1,a2;
a1.getdata(10);
a2.getdata(20);
Item::getcount();
a1.putdata();
a2.putdata();
getch();
return 0;
}