C++ Lab Manual
C++ Lab Manual
DATE:
FACTORIAL OF NUMBERS
AIM:
ALGORITHM:
SOURCE CODE:
#include<iostream>
using namespace std;
int main()
{
int n,i,fact=1;
cout<<"Enter the value"<<endl;
cin>>n;
for(i=1;i<=n;i++)
{
fact=fact*i;
}
cout<<"The Factorical is"<<fact;
return 0;
}
OUTPUT:
RESULT:
EX.NO:2
DATE:
FIBONACCI SERIES
AIM:
ALGORITHM:
SOURCE CODE:
#include <iostream>
using namespace std;
int main() {
while(n != 0) {
remainder = n % 10;
reversed_number = reversed_number * 10 + remainder;
n /= 10;
}
return 0;
}
OUTPUT:
RESULT:
EX.NO:3
DATE:
SUM OF DIGITS
AIM:
ALGORITHM:
SOURCE CODE:
#include <iostream>
using namespace std;
int main()
{
int n,sum=0,m;
cout<<"Enter a number: ";
cin>>n;
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
}
cout<<"Sum is= "<<sum<<endl;
return 0;
}
OUTPUT:
RESULT:
EX.NO:4
TO CHECK THE GIVEN NUMBER IS
DATE: PALINDROME
AIM:
ALGORITHM:
SOURCE CODE:
#include <iostream>
using namespace std;
int main()
{
int n, num, digit, rev = 0;
cout <<"Enter a positive number: ";
cin >> num;
n = num;
do
{
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
} while (num != 0);
cout <<" The reverse of the number is: "<< rev << endl;
if (n == rev)
cout <<" The number is a palindrome.";
else
cout <<" The number is not a palindrome.";
return 0;
}
OUTPUT:
RESULT:
EX.NO:5
DATE:
AMSTRONG NUMBER
AIM:
ALGORITHM:
SOURCE CODE:
#include <iostream>
using namespace std;
int main() {
int num, originalNum, remainder, result = 0;
cout <<"Enter a three-digit integer: ";
cin >> num;
originalNum = num;
while (originalNum != 0)
{
remainder = originalNum % 10;
result += remainder * remainder * remainder;
originalNum /= 10;
}
if (result == num)
cout << num <<" is an Armstrong number.";
else
cout << num <<" is not an Armstrong number.";
return 0;
}
OUTPUT:
RESULT:
EX.NO:6
DATE:
BULIT IN FUNCTION IN STRING
AIM:
ALGORITHM:
SOURCE CODE:
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char str1[10] = "Hello";
char str2[10] = "World";
char str3[10];
int len ;
strcpy( str3, str1);
cout <<"strcpy( str3, str1) : "<< str3 << endl;
strcat( str1, str2);
cout <<"strcat( str1, str2): "<< str1 << endl;
len = strlen(str1);
cout <<"strlen(str1) : "<< len << endl;
return 0;
}
OUTPUT:
RESULT:
EX.NO:7
TO FIND THE SUM AND AVERAGE USING
DATE: ARRAY
AIM:
ALGORITHM:
SOURCE CODE:
include <iostream>
using namespace std;
int main() {
double numbers[] = {7, 5, 6, 12, 35, 27};
double sum = 0;
double count = 0;
double average;
RESULT:
EX.NO:8
DATE:
FIBONACCI SERIES USING RECURSION
AIM:
ALGORITHM:
SOURCE CODE:
#include <iostream>
using namespace std;
int fib(int x) {
if((x==1)||(x==0)) {
return(x);
}else {
return(fib(x-1)+fib(x-2));
}
}
int main() {
int x , i=0;
cout <<"Enter the number of terms of series : ";
cin >> x;
cout <<"\nFibonnaci Series : ";
while(i < x) {
cout <<""<< fib(i);
i++;
cout<<"\n";
}
return 0;
}
OUTPUT:
RESULT:
EX.NO:9
TO FIND LARGEST NUMBER USING IF
DATE: STATEMENT
AIM:
ALGORITHM:
SOURCE CODE:
#include <iostream>
using namespace std;
int main() {
float a, b, c;
cout<<"Enter the value of a:";
cin >> a;
cout<<"Enter the value of b:";
cin >> b;
cout<<"Enter the value of c:";
cin >> c;
if(a >= b && a >= c)
cout <<"Largest number: "<< a<<"\n";
if(b >= a && b >= c)
cout <<"Largest number: "<<b<<"\n";
if(c >= a && c >= b)
cout <<"Largest number: "<<c<<"\n";
return 0;
}
OUTPUT:
RESULT:
EX.NO:10
DATE:
STRUCTURE USING ARRAY
AIM:
ALGORITHM:
SOURCE CODE:
#include <iostream>
using namespace std;
struct Person
{
char name[50];
int age;
float salary;
};
int main()
{
Person p1;
return 0;
}
OUTPUT:
RESULT:
EX.NO:11
CHANGING POINTED VALUE USING
DATE: POINTER
AIM:
ALGORITHM:
SOURCE CODE:
#include <iostream>
using namespace std;
int main()
{
int var = 5;
int* pointVar;
pointVar = &var;
cout <<"var = "<< var << endl;
cout <<"*pointVar = "<< *pointVar << endl<< endl;
cout <<"Changing value of var to 7:"<< endl;
var = 7;
cout <<"var = "<< var << endl;
cout <<"*pointVar = "<< *pointVar << endl<< endl;
cout <<"Changing value of *pointVar to 16:"<< endl;
*pointVar = 16;
cout <<"var = "<< var << endl;
cout <<"*pointVar = "<< *pointVar << endl;
return 0;
}
OUTPUT:
RESULT:
EX.NO:12
DATE:
CALL BY REFERENCE
AIM:
ALGORITHM:
SOURCE CODE:
#include<iostream>
using namespace std;
void swap(int *a, int *b)
{
int swap;
swap=*a;
*a=*b;
*b=swap;
}
int main()
{
int a=4500, b=1300;
swap(&a, &b); // passing value to function
cout<<"The value of a is: "<<a<<"\n";
cout<<"The value of b is: "<<b<<"\n";
return 0;
}
OUTPUT:
RESULT:
EX.NO:13
DATE:
CALL BY VALUE
AIM:
ALGORITHM:
SOURCE CODE:
#include<iostream>
#include<conio.h>
int main()
{
int a = 100, b = 200;
clrscr();
swap(a, b);
cout<<"Value of a"<<a;
cout<<"Value of b"<<b;
getch();
return 0;
}
OUTPUT:
RESULT:
EX.NO:14
DATE:
SCOPE RESOLUTION OPERATOR
AIM:
ALGORITHM:
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
int m=10;
int main()
clrscr();
int m=20;
int k=m;
int m=30;
cout<<"k="<<k<<"\n";
cout<<"m="<<m<<"\n";
cout<<"::m="<<::m<<"\n";
cout<<"m="<<m<<"\n";
cout<<"::m="<<::m<<"\cn";
getch();
return 0;
}
OUTPUT:
RESULT:
EX.NO:15
DATE:
USING CLASS
AIM:
ALGORITHM:
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class person
{
char name[30];
int age;
public:
void getdata(void);
void display(void);
};
void person::getdata(void)
{
cout<<"Enter name:";
cin>>name;
cout<<"Enter age:";
cin>>age;
}
void person::display(void)
{
cout<<"\nName:"<<name;
cout<<"\nAge:"<<age;
}
int main()
{
person p;
p.getdata();
p.display();
return 0;
}
OUTPUT:
RESULT:
EX.NO:16
DATE:
CONSTRUCTOR
AIM:
ALGORITHM:
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class integer
{
int m,n;
public:
integer(int,int);
void display(void)
{
cout<<"m="<<m<<"\n";
cout<<"n="<<n<<"\n";
}
};
integer::integer(int x,int y)
{
m=x,n=y;
}
int main()
{
integer int1(0,100);
integer int2=integer(25,75);
clrscr();
cout<<"\n OBJECT1"<<"\n";
int1.display();
cout<<"\n OBJECT2"<<"\n";
int2.display();
getch();
return(0);
}
OUTPUT:
RESULT:
EX.NO:17
DATE:
DESTRUCTOR
AIM:
ALGORITHM:
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
int count=0;
class alpha
{
public:
alpha()
{
count++;
cout<<"\n No of object destroyed:"<<count;
}
~alpha()
{
cout<<"\n No of object destroyed:"<<count;
count--;
}
};
int main()
{
cout<<"\n\n ENTER MAIN";
alpha A1,A2,A3,A4;
{
cout<<"\n\nENTER BLOCK1\n";
alpha A5;
}
{
cout<<"\n\nENTER BLOCK2\n";
alpha A6;
}
cout<<"\n\n REENTER MAIN\n";
getch();
return(0);
}
OUTPUT:
RESULT:
EX.NO:18
DATE:
FRIEND FUNCTION
AIM:
ALGORITHM:
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
#include<math.h>
class sample
{
int a;
int b;
public:
void setvalue()
{
a=25;
b=40;
}
friend float mean(sample s);
};
float mean(sample s)
{
return float(s.a+s.b)/2.0;
}
int main()
{
clrscr();
sample x;
x.setvalue();
cout<<"\n\t mean value="<<mean(x)<<"\n";
getch();
return(0);
}
OUTPUT:
RESULT:
EX.NO:19
DATE:
VIRTUAL FUNCTION
AIM:
ALGORITHM:
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class base
{
public:
void display()
{
cout<<"\ndisplay base";
}
virtual void show()
{
cout<<"\n Show base";
}
};
class derived:public base
{
public:
void display()
{
cout<<"\n display derived";
}
void show()
{
cout<<"\n show derived";
}
};
int main()
{
clrscr();
base b;
derived d;
base*bptr;
cout<<"\n\t bptr pointer to base\n";
bptr=&b;
bptr->display();
bptr->show();
cout<<"\n\nbptr pointer to derived\n";
bptr=&d;
bptr->display();
bptr->show();
getch();
return(0);
}
OUTPUT:
RESULT:
EX.NO:20
DATE:
POLYMORPHISM
AIM:
ALGORITHM:
SOURCE CODE:
#include <iostream>
using namespace std;
class Geeks {
public:
void func(int x)
{
cout <<"value of x is "<< x << endl;
}
void func(double x)
{
cout <<"value of x is "<< x << endl;
}
void func(int x, int y)
{
cout <<"value of x and y is "<< x <<", "<< y<< endl;
}
};
int main()
{
Geeks obj1;
obj1.func(7);
obj1.func(9.132);
obj1.func(85, 64);
return 0;
}
OUTPUT:
RESULT:
EX.NO:21
DATE:
NESTING OF MEMBER FUNCTION
AIM:
ALGORITHM:
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class set
{
int m,n;
public:
void input(void);
void display(void);
int largest(void);
};
int set::largest(void)
{
if(m>=n)
return(m);
else
return(n);
}
void set::input(void)
{
cout<<"\n\tINPUT VALUES OF M:";
cin>>m;
cout<<"\n\tOUTPUT VALUES OF N:";
cin>>n;
}
void set::display(void)
{
cout<<"\n\tLARGEST VALUES="<<largest()<<"\n";
}
int main()
{
clrscr();
set a;
a.input();
a.display();
getch();
return(0);
}
OUTPUT:
RESULT:
EX.NO:22
DATE:
INLINE FUNCTION
AIM:
ALGORITHM:
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
if(a>b&&a>c)
return(a);
if(b>c&&b>a)
return(b);
else
return(c);
int main()
int a,b,c;
clrscr();
cout<<"\t\tINLINE FUNCTION\n";
cin>>a>>b>>c;
cout<<largest(a,b,c);
getch();
return(0);
}
OUTPUT:
RESULT:
EX.NO:23
DATE:
FUNCTION OVERLOADING
AIM:
ALGORITHM:
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
int vol(int);
double vol(double,int);
long vol(long,int,int);
int main()
{
clrscr();
cout<<"\n\tfunction overloading\n";
cout<<"\n\t***********************\n\n";
cout<<vol(10)<<"\n\n";
cout<<vol(2.5,8)<<"\n";
cout<<vol(100,75,15);
getch();
return(0);
}
int vol(int s)
{
return(s*s*s);
}
double vol(double r,int n)
{
return(3.14519*r*r*n);
}
long vol(long l,intb,int n)
{
return(l*b*n);
}
OUTPUT:
RESULT:
EX.NO:24
DATE:
OPERATOR OVERLOADING
AIM:
ALGORITHM:
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class complex
float x,y;
public:
complex()
x=real;y=image;
complex operator+(complex);
void display(void);
};
complex complex::operator+(complex c)
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
void complex::display(void)
cout<<x<<"+j"<<y<<"\n";
}
int main()
clrscr();
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
c3=c1+c2;
cout<<"\n\tOperatoroverloding";
cout<<"\n\t*********************";
cout<<"\n\n c1=";
c1.display();
cout<<"\n\n c2=";
c2.display();
cout<<"\n\n c3=";
c3.display();
getch();
return(0);
}
OUTPUT:
RESULT:
EX.NO:25
DATE:
THIS POINTER
AIM:
ALGORITHM:
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
#include<string.h>
class person
char name[20];
float age;
public:
person(char*s,float a)
strcpy(name,s);
age=a;
person&person::greator(person&x)
if(x.age>=age)
return x;
else
return*this;
void display(void)
cout<<"NAME:"<<name<<"\n"<<"AGE:"<<age<<"\n";
};
int main()
clrscr();
person p1("john",37.50),
p2("ahmad",29.0),
p3("hebber",40.25);
person p=p1.greator(p3);
p.display();
p=p1.greator(p2);
p.display();
getch();
return(0);
}
OUTPUT:
RESULT:
EX.NO:26
DATE:
INPUT OUTPUT MANIPULATION
AIM:
ALGORITHM:
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
int main()
clrscr();
int f=1,i,n;
cout<<"\n\t************************";
cin>>n;
for(i=1;i<=n;i++)
f=f*i;
cout<<setw(5)<<"\t"<<i<<"!"<<setw(10)<<"\t"<<f<<"\n";
getch();
return(0);
}
OUTPUT:
RESULT:
EX.NO:27
DATE:
DEFAULT ARGUMENT
AIM:
ALGORITHM:
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
#include<math.h>
class defa
{
int m,n,r;
public:
void put();
void compute(int,int=4);
};
void defa::compute(int m,int n)
{
r=pow(m,n);
}
void defa::put()
{
cout<<r;
}
void main()
{
clrscr();
int m;
defa d;
cout<<"\n DEFAULT ARGUMENT";
cout<<"\n******************";
cout<<"\n enter the value:";
cin>>m;
d.compute(m);
cout<<"\n THE VALUE IS:";
d.put();
getch();
}
OUTPUT:
RESULT:
EX.NO:28
DATE:
SINGLE INHERITANCE
AIM:
ALGORITHM:
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class B
int a;
public:
int b;
void set_ab();
int get_a(void);
void show_a(void);
};
class D:public B
int c;
public:
void mul(void);
void display(void);
};
void B::set_ab(void)
a=5;b=10;
int B::get_a()
return a;
}
void B::show_a()
cout<<"a="<<a<<"\n";
void D::mul()
c=b*get_a();
void D::display()
cout<<"a="<<get_a()<<"\n";
cout<<"b="<<b<<"\n";
cout<<"c="<<c<<"\n\n";
int main()
clrscr();
D d;
d.set_ab();
d.mul();
d.show_a();
d.display();
d.b=20;
d.display();
getch();
return(0);
}
OUTPUT:
RESULT:
EX.NO:29
DATE:
MULTILEVEL INHERITANCE
AIM:
ALGORITHM:
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rno;
public:
void get_no(int);
void put_no(void);
};
void student::get_no(int a)
{
rno=a;
}
void student::put_no()
{
cout<<"\n\n\t roll number:\t"<<rno<<endl;
}
class test:public student
{
protected:
int sub1;
int sub2;
int sub3;
public:
void get_marks(int,int,int);
void put_marks(void);
};
void test::get_marks(int x,inty,int z)
{
sub1=x;
sub2=y;
sub3=z;
}
void test::put_marks()
{
cout<<"\n\n\t marks in sub1:"<<sub1<<"\n";
cout<<"\n\t marks in sub2:"<<sub2<<"\n";
cout<<"\n\t marks in sub3:"<<sub3<<"\n";
}
class result:public test
{
float total;
public:
void display(void);
};
void result::display(void)
{
total=sub1+sub2+sub3;
put_no();
put_marks();
cout<<"\n\n\tTotal="<<total<<"\n";
}
int main()
{
clrscr();
cout<<"\n\t\tMULTILEVEL INHERITANCE";
cout<<"\n\t\t**********************";
result res;
res.get_no(123);
res.get_marks(90,80,70);
res.display();
getch();
return(0);
}
OUTPUT:
RESULT:
EX.NO:30
DATE:
MULTIPLE INHERITANCE
AIM:
ALGORITHM:
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class M
{
protected:
int m;
public:
void get_m(int);
};
class N
{
protected:
int n;
public:
void get_n(int);
};
class P:public M,public N
{
public:
void display(void);
};
void M::get_m(int x)
{
m=x;
}
void N::get_n(int y)
{
n=y;
}
void P::display(void)
{
cout<<"\n\n\t\tThe value of m and n are:";
cout<<"\n\n\t\tm="<<m;
cout<<"\n\n\t\tn="<<n;
cout<<"\n\n\t\tAddition";
cout<<"\n\n\t\tm+n="<<m+n;
cout<<"\n\n\t\tSubtraction";
cout<<"\n\n\t\tm-n="<<m-n;
cout<<"\n\n\t\tMultiplication";
cout<<"\n\n\t\tm*n="<<m*n;
cout<<"\n\n\t\tDivision";
cout<<"\n\n\t\tm/n="<<m/n;
}
int main()
{
clrscr();
cout<<"\n\nMULTIPLE INHERITANCE";
cout<<"\n\n*********************";
P p;
p.get_m(50);
p.get_n(2);
p.display();
getch();
return(0);
}
OUTPUT:
RESULT:
EX.NO:31
DATE:
HIERARCHICAL INHERITANCE
AIM:
ALGORITHM:
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
#include<math.h>
class publish
{
char title[50];
int noitem;
public:
void get();
void disp();
};
class book:public publish
{
int pages;
public:
void get1();
void disp1();
};
class tape:public publish
{
int length;
public:
void get2();
void disp2();
};
void publish::get()
{
cout<<"\n\nEnter the title:";
cin>>title;
cout<<"\n\nEnter the no of item:";
cin>>noitem;
}
void publish::disp()
{
cout<<"\n\tTitle:"<<title;
cout<<"\n\tno of item:"<<noitem;
}
void book::get1()
{
get();
cout<<"\n\nEnter the book pages:";
cin>>pages;
}
void book::disp1()
{
disp();
cout<<"\n\tPages:"<<pages;
}
void tape::get2()
{
get();
cout<<"\n\n Enter the tape length:";
cin>>length;
}
void tape::disp2()
{
disp();
cout<<"\n\n\ttape length:"<<length;
}
void main()
{
book b1;
tape b2;
clrscr();
cout<<"\n\t\tHIERARCHICAL INHERITANCE";
cout<<"\n\t\t........................\n\n";
b1.get1();
b1.disp1();
cout<<"\n\n\tTAPE";
b2.get2();
b2.disp2();
getch();
}
OUTPUT:
RESULT:
EX.NO:32
DATE:
HYBRID INHERITANCE
AIM:
ALGORITHM:
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rollno;
public:
void get_no(int a)
{
rollno=a;
}
void put_no(void)
{
cout<<"\n\n\n\n\tRoll no:"<<rollno<<"\n";
}
};
class test:public student
{
protected:
float part1,part2;
public:
void get_marks(float x,float y)
{
part1=x;
part2=y;
}
void put_marks(void)
{
cout<<"\n\tMARKS OBTAINED:\n";
cout<<"\n\t*****************";
cout<<"\n\n\tpart1="<<part1;
cout<<"\n\n\tpart2="<<part2;
}
};
class sports
{
protected:
float score;
public:
void get_score(float s)
{
score=s;
}
void put_score(void)
{
cout<<"\n\n\n\tsportswt:"<<score<<"\n";
}
};
class result:publictest,public sports
{
float total;
public:
void display(void);
};
void result::display(void)
{
total=part1+part2+score;
put_no();
put_marks();
put_score();
cout<<"\n\t\ttotal score:"<<total<<"\n";
}
int main()
{
result r;
clrscr();
cout<<"\n\t\tHYBRID INHERITANCE:";
cout<<"\n\t\t******************";
r.get_no(1234);
r.get_marks(27.5,53.0);
r.get_score(6.0);
r.display();
getch();
return(0);
}
OUTPUT:
RESULT:
EX.NO:33
DATE:
CREATING A FILE
AIM:
ALGORITHM:
SOURCE CODE:
#include<iostream>
#include<fstream>
using namespace std;
int main(){
fstream FileName;
FileName.open("FileName", ios::out);
if (!FileName){
cout<<"Error while creating the file";
}
else{
cout<<"File created successfully";
FileName.close();
}
return 0;
}
OUTPUT:
RESULT:
EX.NO:34
DATE:
PROGRAM TO READ AND WRITE A FILE
AIM:
ALGORITHM:
SOURCE CODE:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int rno,fee;
char name[50];
cout<<"Enter the Roll Number:";
cin>>rno;
cout<<"\nEnter the Name:";
cin>>name;
cout<<"\nEnter the Fee:";
cin>>fee;
ofstream fout("d:/student.doc");
fout<<rno<<"\t"<<name<<"\t"<<fee;
fout.close();
ifstream fin("d:/student.doc");
fin>>rno>>name>>fee;
fin.close();
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
return 0;
}
OUTPUT:
RESULT:
EX.NO:35
FILE HANDLING PROGRAM IN C++ TO READ
DATE: AND WRITING ON A FILE
AIM:
ALGORITHM:
SOURCE CODE:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int rno,fee;
char name[50];
cout<<"Enter the Roll Number:";
cin>>rno;
cout<<"\nEnter the Name:";
cin>>name;
cout<<"\nEnter the Fee:";
cin>>fee;
ofstream fout("d:/student.doc");
fout<<rno<<"\t"<<name<<"\t"<<fee; //write data to the file student
fout.close();
ifstream fin("d:/student.doc");
fin>>rno>>name>>fee; //read data from the file student
fin.close();
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
return 0;
}
OUTPUT:
RESULT: