0% found this document useful (0 votes)
135 views45 pages

PROGRAM 1: Write A Program To Check Whether A Given Number Is Even or Odd

This document contains code for 20 programming practicals covering fundamental C++ concepts like conditional statements, loops, functions, arrays, pointers, classes, objects, constructors, inheritance and more. The practicals include programs to check even/odd numbers, find largest of 3 numbers, calculate sum and factorial of a number, print patterns, demonstrate call by value/reference, work with matrices, strings, structures within structures, object classes, static class members, constructors, constructor overloading and more. The document provides code examples to help learn and practice essential C++ programming skills.

Uploaded by

Shobhita Mahajan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
135 views45 pages

PROGRAM 1: Write A Program To Check Whether A Given Number Is Even or Odd

This document contains code for 20 programming practicals covering fundamental C++ concepts like conditional statements, loops, functions, arrays, pointers, classes, objects, constructors, inheritance and more. The practicals include programs to check even/odd numbers, find largest of 3 numbers, calculate sum and factorial of a number, print patterns, demonstrate call by value/reference, work with matrices, strings, structures within structures, object classes, static class members, constructors, constructor overloading and more. The document provides code examples to help learn and practice essential C++ programming skills.

Uploaded by

Shobhita Mahajan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 45

PROGRAM 1 : Write a program to check whether a given number is even or odd.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n;
cout<<"Enter number: ";
cin>>n;
if(n%2==0)
{
cout<<"Number is even";
}
else
{
cout<<"Number is odd";
}
getch();
}

1
2
PRACTICAL 2: Write a program to find the greatest of three numbers using conditional
operator.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
cout<<"Enter a: ";
cin>>a;
cout<<"Enter b: ";
cin>>b;
cout<<"Enter c: ";
cin>>c;
if(a>b)
{
if(a>c)
cout<<"Largest is "<<a;
else
{
if (b>c)
cout<<"Largest is "<<b;
}}
else
cout<<"Largest is "<<c;
getch();
}
PRACTICAL 3 : Write a program to find the sum of digits of a number.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,num,sum=0;
cout<<"\n Enter the number: ";
cin>>n;
num=n;
while(num>0)
{
sum+=num%10;
num/=10;
}
cout<<"\n The sum of digits of a number is "<<sum;
getch();
}
PRACTICAL 4 : Write a program to generate Fibonacci series of first n numbers.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n;
int f1,f2,f3;
cout<<"Enter how many number series you need: ";
cin>>n;
f1=1;
f2=1;
cout<<f1<<endl;
cout<<f2<<endl;
int c=2;
while(c<n)
{
f3=f1+f2;
f1=f2;
f2=f3;
cout<<f3<<endl;
c++;
}
getch();
}
PRACTICAL 5: Write a program to print the multiplication table of a given number.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i;
cout<<"enter a no. for which you need a table";
cin>>n;
for(i=1;i<=10;i++)
cout<<n<<"*"<<i<<"="<<n*i<<endl;
getch();
}
PRACTICAL 6 : Write a program to calculate the factorial of a number.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,num,fact=1;
cout<<"Enter the number: ";
cin>>num;
i=num;
while (num>0)
{
fact=fact*num;
--num;
}
cout<<"Factorial of "<<i<<" is "<<fact;
getch();
}
PRACTICAL 7 : Write a program to add, subtract, multiply & division o two numbers
using switch statement.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
char opr;
cout<<"Enter any two numbers: ";
cin>>a>>b;
cout<<"Choose any operator(+,-,*,/): ";
cin>>opr;
switch(opr)
{
case'+':
int c=a+b;
cout<<"The addition of two numbers is: "<<c;
break;
case'-':
int d=a-b;
cout<<"The subtraction of two numbers is: "<<d;
break;
case'*':
int e=a*b;
cout<<"The multiplication of two numbers is: "<<e;
break;
case'/':
int f=a/b;
cout<<"The division of two numbers is: "<<f;
break;
default:
cout<<"Wrong operator";
}
getch();
}
PRACTICAL 8 : Write a program to check whether a given number is prime or not.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num,i,c=0;
cout<<"enter number: ";
cin>>num;
for(i=2;i<=num-1;i++)
{
if(num%i==0)
c++;
}
if(c!=0)
cout<<"it is not prime no.";
else
cout<<"it is prime no.";
getch();
}
PRACTICAL 9 : Write a program to print the given pattern.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
for(int i=1;i<6;i++)
{
for(int j=6-i;j>=0;j--)
cout<<" ";
for(int k=i;k>=2;k--)
cout<<k;
for(int l=1;l<=i;l++)
cout<<l;
cout<<"\n";
}
getch();
}

10
PRACTICAL 10: Write a program to demonstrate the working of call by value.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,y;
x=10;
y=20;
void swap(int,int);
swap(x,y);
cout<<"x= "<<x<<endl;
cout<<"y= "<<y;
getch();
}
void swap(int a, int b)
{
int t=a;
a=b;
b=t;
}

11
PRACTICAL 11: Write a program to demonstrate the working of call by reference.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,y;
x=10;
y=20;
void swap(int&, int&);
swap(x,y);
cout<<"x= "<<x<<endl;
cout<<"y= "<<y;
getch();
}
void swap(int &a, int &b)
{
int t=a;
a=b;
b=t;
}

12
PRACTICAL 12: Write a program to demonstrate the working of pointers.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int *p;
int x;
cout<<"Enter a value: ";
cin>>x;
p=&x;
*p=*p+10;
cout<<"Now x is: "<<x<<endl;
cout<<"Value of x through p: "<<*p<<endl;
cout<<"The address of x is: "<<&x<<endl;
cout<<"The address of x is: "<<p<<endl;
getch();
}

13
PRACTICAL 13: Write a program to demonstrate the use of inline functions.

#include<iostream.h>
#include<conio.h>
inline double cube(double x)
{
return(x*x*x);
}
void main()
{
clrscr();
double a;
cout<<"Enter a number: "
cin>>a;
cout<<"The cube of "<<a<<"is: "
cout<<cube(a);
getch();
}

14
PRACTICAL 14: Write a program to demonstrate the working of default arguments.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sum(int=30,int=30);
int a,b;
cout<<"Enter value of a: ";
cin>>a;
cout<<"Enter value of b: ";
cin>>b;
sum(a,b);
sum(a);
sum();
getch();
}
int sum(int x,int y)
{
int s=x+y;
cout<<"sum= "<<s<<endl;
return 0;
}

15
PRACTICAL 15: Write a program to demonstrate the use of recursion.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int fact(int);
int f,n;
cout<<"Enter the number: ";
cin>>n;
f=fact(n);
cout<<"FACTORIAL= "<<f;
getch();
}
int fact(int n)
{
int value=1;
if(n==1)
return(value);
else
{
value=n*fact(n-1);
return(value);
}
getch();
}

16
PRACTICAL 16: Write a program which reads two matrices and then print a matrix
which is addition of these two.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][3],b[3][3],s[3][3];
cout<<"Enter first matrix elements: "<<endl;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
cin>>a[i][j];
}
cout<<"\n Enter second matrix: "<<"\n\n";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
cin>>b[i][j];
}
cout<<"\n The first matrix is:"<<"\n\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<a[i][j];
cout<<"\t";
}
cout<<endl;
}
cout<<"\n The second matrix is:"<<"\n\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<b[i][j];
cout<<"\t";
}
cout<<endl;
}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
s[i][j] = a[i][j] + b[i][j];
}

17
cout<<"\n The addition of two matrices is: "<<"\n\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<s[i][j]<<'\t';
}
cout<<endl;
}
getch();
}

18
PRACTICAL 17: Write a program to read a string and then copy that string into another.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char a[80],b[80];
void string_copy(char b[],char a[]);

cout<<"Enter a string: "<<endl;


cin.get(a,80);
string_copy (b,a);
cout<<"First string is: "<<endl;
cout<<a<<endl;
cout<<"Second string is: "<<endl;
cout<<b<<endl;
getch();
}
void string_copy (char b[],char a[])
{
int i,j;
i=0;
while(a[i]!='\0')
{
b[i]=a[i];
i++;
}
b[i]='\0';
}

19
PRACTICAL 18: Write a program to demonstrate the working of structure within a
structure.

#include<iostream.h>
#include<conio.h>
struct date
{
int day;
char month_name[20];
int year;
};
struct biodata
{
char name[80];
date dob;
};
void main()
{clrscr();
biodata student;
cout<<"Enter name: "<<endl;
cin>>student.name;
cout<<"ENTER DATE OF BIRTH:"<<endl;
cout<<"Enter day: ";
cin>>student.dob.day;
cout<<"Enter month name: ";
cin>>student.dob.month_name;
cout<<"Enter year: ";
cin>>student.dob.year;
cout<<"The biodata is: "<<endl;
cout<<"Name: "<<student.name<<endl;
cout<<"Date of birth: ";
cout<<student.dob.day<<"-"<<student.dob.month_name<<"-";
cout<<student.dob.year<<endl;
getch();
}
PRACTICAL 19: Write a program to calculate the area of rectangle using the concept of
object class.

#include<iostream.h>
#include<conio.h>
class area
{
public:
int l,b,area;
void getdata()
{
cout<<"Enter the length of the rectangle: ";
cin>>l;
cout<<"Enter the breadth of the rectangle: ";
cin>>b;
}
void display()
{
area=l*b;
cout<<"The area of rectangle is: "<<area;
}};
void main()
{
clrscr();
area s1;
s1.getdata();
s1.display();
getch();
}
PRACTICAL 20 : Write a program to demonstrate data hiding, encapsulation and
abstraction.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
class date
{
public:
int day;
int month;
int year;
};
class date today;
today.day=12;
today.month=1;
today.year=1992;
cout<<"Today's date is "<<today.day<<"/";
cout<<today.month<<"/"<<today.year<<endl;
getch();
}

23
PRACTICAL 21 : Write a program to show the working of static data members and static
member functions.

#include<iostream.h>
#include<conio.h>
class alpha
{
private:
static int x;
public:
alpha();
static void display()
{
cout<<"Content of x ";
cout<<"after incremented by one= "<<x<<endl;
}};
class beta
{
private:
int y;
public:
void getdata()
{
cout<<"Enter a value for y= \n";
cin>>y;
}
void display()
{
cout<<"Content of y= "<<this->y<<endl;
}};
int alpha::x=10;
alpha::alpha()
{
++x;
}
void main()
{
clrscr();
alpha objx;
beta objy;
objy.getdata();
alpha::display();
objy.display();
getch();
}
PRACTICAL 22 : Write a program to demonstrate the working of constructor.

#include<iostream.h>
#include<conio.h>
class sample
{
int a,b;
public:
sample()
{
cout<<"This is constructor. "<<endl;
a=100;
b=200;
}
int add()
{
return(a+b);
}};
void main()
{
clrscr();
sample s;
cout<<"Output is: "<<s.add()<<endl;
getch();
}
PRACTICAL 23: Write a program to show the working of parameterized constructor.

#include<iostream.h>
#include<conio.h>
class student
{
int a,b;
public:
student(int a1,int b1)
{
a=a1;
b=b1;
}
int mul()
{
return(a*b);
}};
void main()
{
clrscr();
int i,j;
cout<<"Enter first number: ";
cin>>i;
cout<<"Enter second number: ";
cin>>j;
student c1(i,j);
cout<<"Multiplication is: ";
cout<<c1.mul()<<endl;
getch();
}

27
PRACTICAL 24: Write a program to demonstrate constructor overloading.

#include<iostream.h>
#include<conio.h>
class complex
{
int x,y;
public:
complex(int a)
{
x=a;
y=a;
}
complex(int a,int b)
{
x=a;
y=b;
}
void add(complex c1,complex c2)
{
x=c1.x-c2.x;
y=c1.x-c2.y;
}
void show()
{
cout<<x<<"+i"<<y<<endl;
}};
void main()
{
clrscr();
complex a(3,5);
complex b(2);
complex c(0);
c.add(a,b);
a.show();
b.show();
cout<<"a-b is: ";
c.show();
getch();
}
PRACTICAL 25: Write a program to demonstrate copy constructor.

#include<iostream.h>
#include<conio.h>
class complex
{
int r,i;
public:
complex(int a,int b)
{
r=a;
i=b;
}
complex(complex&c)
{
r=c.r;
i=c.i;
}
void show()
{
cout<<r<<"+i"<<i<<endl;
}};
void main()
{
clrscr();
int x,y;
cout<<"Enter real part: ";
cin>>x;
cout<<"Enter imaginary part: ";
cin>>y;
complex c1(x,y);
complex c2(c1);
cout<<"First number is ";
c1.show();
cout<<"Second number is ";
c2.show();
getch();
}
PRACTICAL 26 : Write a program to demonstrate destructor.

include<iostream.h>
#include<conio.h>
class sample
{
int a,b;
public:
~sample()
{
cout<<"This is destructor"<<endl;
a=100;
b=200;
}
int add()
{
return(a+b);
}};
void main()
{
clrscr();
sample s;
cout<<"Output is: "<<s.add()<<endl;
getch();
}
PRACTICAL 27: Write a program to demonstrate hybrid inheritance.

#include<iostream.h>
#include<conio.h>
class b1
{
protected:
int x;
public:
void assignx()
{ x=2
0;
}};
class d1:public b1
{
protected:
int y;
public:
void assigny()
{ y=4
0;
}};
class d2:public d1
{
protected:
int z;
public:
void assignz()
{ z=6
0;
}};
class b2
{
protected:
int k;
public:
void assignk()
{ k=8
0;
}};
class d3:public b2,public d2
{
private:
int total;
public:

33
void output()
{
total=x+y+z+k;
cout<<"x+y+z+k= "<<total<<endl;
}};
void main()
{
clrscr();
d3 s;
s.assignx();
s.assigny();
s.assignz();
s.assignk();
s.output();
getch();
}

34
PRACTICAL 28: Write a program which concatenates two strings by overloading the
binary operator ‘+’.

#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
char str[80];
public:
string()
{
strcpy(str," ");
}
string(char s[])
{
strcpy(str,s);
}
void display()
{
cout<<str<<endl;
}
string operator+(string s4)
{
if((strlen(str)+strlen(s4.str))<80)
{
string temp;
strcpy(temp.str,str);
strcat(temp.str,s4.str);
return(temp);
}
else
cout<<"String overflow"<<endl;
}};
void main()
{
clrscr();
string s1("ECE");
string s2=("ROCKS");
string s3;
s3=s1+s2;
s3.display();
getch();
}
PRACTICAL 29: Write a program to show function overloading.

#include<iostream.h>
#include<conio.h>
void sum(int,int);
void sum(int,int,int);
void sum(float,float);
void main()
{
clrscr();
int a,b,c;
float p,q;
cout<<"Enter integer values: ";
cin>>a>>b>>c;
cout<<"Enter float values: ";
cin>>p>>q;
sum(a,b,c);
sum(a,b);
sum(a,c);
sum(p,q);
getch();
}
void sum(int x,int y)
{
int z=x+y;
cout<<"sum 1= "<<z<<endl;
}
void sum(int x,int y,int z)
{
int s=x+y+z;
cout<<"sum 2= "<<s<<endl;
}
void sum(float x,float y)
{
float z=x+y;
cout<<"sum 3= "<<z<<endl;
}
PRACTICAL 30: Write a program to demonstrate virtual functions.

#include<iostream.h>
#include<conio.h>
class b
{
public:
virtual void display()
{
cout<<"This is class b."<<endl;
}};
class d1:public b
{
public:
void display()
{
cout<<"This is class d1."<<endl;
}};
class d2:public b
{
public:
void display()
{
cout<<"This is class d2."<<endl;
}};
void main()
{
clrscr();
b*p;
d1 obj1;
d2 obj2;
b obj;
p=&obj;
p->display();
p=&obj1;
p->display();
p=&obj2;
p->display();
getch();
}
PRACTICAL 31: Write a program to demonstrate friend function.

#include<iostream.h>
#include<conio.h>
class sample
{
int x;
int y;
public:
sample(int a,int b)
{
x=a;
y=b;
}
friend int add(sample s1);
};
int add(sample s1)
{
return(s1.x+s1.y);
}
void main()
{
clrscr();
sample s2(20,30);
int sum;
sum=add(s2);
cout<<"Sum of private data of object s2= ";
cout<<sum<<endl;
getch();
}
PRACTICAL 32: Write a program to read multiple line string using get function.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char str[80];
cout<<"Enter multiple lines. "<<endl;
cout<<"Press enter after each
line."<<endl; cout<<"For terminating
program,enter $."; cout<<"and then press
enter. "<<endl; cin.get(str,80,'$');
cout<<"Entered multiple lines: "<<endl;
cout<<str<<endl;
getch();
}

42
PRACTICAL 33: Write a program to demonstrate class template with constructor.

#include<iostream.h>
#include<conio.h>
template<class T>
class sample
{
private:T a,b;
public:sample (Ta1,Tb1)
{
a=a1;
b=b1;
}
T small()
{
if(a<b)
return(a);
else
return(b);
}};
void main()
{
sample<int>obj1(10,5);
cout<<"smaller no. is "<<obj1.small();
sample<float>obj2(10.5,20.5);
cout<<"smaller no. is "<<obj2.small();
getch();
}

43
PRACTICAL 34: Write a program to show function template with multiple parameters.

#include<iostream.h>
#include<conio.h>
template<class T1,class T2>
void show(T1a,T2b)
{
cout<<a<<"\t"<<b<<endl;
}
void main()
{
clrscr();
show(1234,"kimmy");
show(5678,"rimmy");
show(9109,9019);
getch();
}

44

You might also like