Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

Practical Solution

1CE2302 (Programming in C++)


1. Build C++ program to display Hello.

#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
cout<<"Hello";
getch();
return 0;
}

2. Develop program which demonstrate the use of data types.

2.1 Write a C++ program to scan and display value of variable.

#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;
}

2.2 Write a C++ program to find average of three numbers.

#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;
}

2.3 Write a c++ program to convert temperature from Celsius to Fahrenheit.


#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
float f, c;
cout<<"Enter Celsius:";
cin>>c;
f=(c*9.0)/5.0 +32 ;
cout<<"Fahrenheit="<<f;
getch();
return 0;
}

2.4 Enum data type

#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;
}

3. Develop minimum 2 programs using arrays.

3.1 Write a C++ program to scan and display elements of an array.

#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;
}

3.2 Write a C++ program to sort an element of an array in ascending order.

#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;
}

3.3 Write a C++ program to find maximum element from an array.

#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. Develop C++ program for Operators.

4.1 Write a C++ program for arithmetic operators.


#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int a,b,c,d,e,f,g;
cout<<"Enter a:";
cin>>a;
cout<<"Enter b:";
cin>>b;
c=a+b;
d=a-b;
e=a*b;
f=a/b;
g=a%b;
cout<<endl<<"addition="<<c<<endl;
cout<<endl<<"substraction="<<d;
cout<<endl<<"Multiplication="<<e;
cout<<endl<<"Division="<<f;
cout<<endl<<"Modulo="<<g;
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;
}

4.3 Write a C++ program to check entered no is odd or even(Relational operators).

#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;
}

4.4 Write a C++ program for sizeof operator.

#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;

cout<<"enter a and b:";


cin>>a>>b;
cout<<"bitwise AND:"<<(a&b)<<endl;
cout<<"bitwise OR:"<<(a|b)<<endl;
cout<<"bitwise exclusive or:"<<(a^b)<<endl;
cout<<"bitwise not:"<<(~a)<<endl;
cout<<"bitwise shift left:"<<(a<<2)<<endl;
cout<<"bitwise shift right:"<<(a>>2)<<endl;
getch();
return 0;
}

4.6 Write a C++ program for conditional operators.

#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;
}

5. Develop minimum 5 programs using control structures.

5.1 Write a C++ program for switch case.

#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;
}

5.5 Write a C++ program using for loop.

#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;
}

6.2 Scope Resolution Operator

#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.

7.1 call by value


#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;
}
void swap(int x, int y)
{
int temp;
temp=x;
x=y;
y=temp;
cout<<"In swap function:"<<endl;
cout<<"x="<<x<<endl<<"y="<<y<<endl;
}

7.2 call by reference

#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;
}

void swap(int &x, int &y)


{
int temp;
temp=x;
x=y;
y=temp;
cout<<"In swap function:"<<endl;
cout<<"x="<<x<<endl<<"y="<<y<<endl;
}

7.3 call by pointer or call by address

#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;
}

void swap(int *x, int *y)


{
int *temp;
*temp=*x;
*x=*y;
*y=*temp;
cout<<"In swap function:"<<endl;
cout<<"x="<<*x<<endl<<"y="<<*y<<endl;
}

7.4 Return by reference

#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;
}

7.5 Default arguments

#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;
}

7.6 Constant arguments


#include<iostream.h>
#include<conio.h>
void fun(const int,const int);
int main()
{
clrscr();
const int a=10,b=20;
fun(a,b);
getch();
return 0;
}
void fun(const int x,const int y)
{
x=y++;
cout<<x<<endl<<y;
}

7.7 function overloading

#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;
}

8. Define minimum 5 different classes.


8.1 program of class and object

#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;
}

8.3 Defining member function outside the class

#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;
}

8.4 calling of private member function of class or nesting of member function

#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;
}

8.4.1 nesting of member function

#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;
}

8.6 Friend function


#include<iostream.h>
#include<conio.h>
class test
{
int a,b;
public:
void getdata()
{
cout<<”Enter a and b:”;
cin>>a>>b;
}
friend void sum(test );
};
void sum(test t1 )
{
int s;
s=t1.a+t1.b;
cout<<”sum=”<<s;
}
int main ()
{
test t;
t.getdata();
sum(t);
getch();
return 0;
}

9. Develop Programs using array of objects and static member functions.

9.1 Array of Object

#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;
}

9.2 Static data member and static member function.

#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;
}

You might also like