Professional Documents
Culture Documents
C++ Lab
C++ Lab
# include <iostream.h>
# include <conio.h>
void main()
{
clrscr();
cout<<”enter the world in c++”;
getch();
}
Output:
enter the world in c++
2. Write a Program for
a. Addition of two numbers b. Subtraction of two numbers
clrscr(); clrscr();
int a,b, c; int a,b, c;
cout<<”enter the values”; cout<<”enter the values”;
cin>>a>>b; cin>>a>>b;
c=a+b; c=a-b;
cout<<”The ans is=”<<c; cout<<”The ans is=”<<c;
getch(); getch();
} }
Output: Output:
(A)Fibonacci series
# include <iostream.h>
# include <conio.h>
void main()
{
int a = -1, b = 1, c = 0, i, n, sum = 0 ;
clrscr() ;
cout<<"Enter the limit : ";
cin>>n;
cout<<"\nThe fibonacci series is : \n\n" ;
for(i = 1 ; i <= n ; i++)
{
c=a+b;
cin>>c;
sum = sum + c ;
a=b;
b=c;
}
cout<<"\n\nThe sum of the fibonacci series is : %d", sum ;
getch() ;
}
#include <iostream>
using namespace std;
int main()
{
int n1, n2;
cout << "Enter two numbers: ";
cin >> n1 >> n2;
while(n1 != n2)
{
if(n1 > n2)
n1 -= n2;
else
n2 -= n1;
}
cout << "HCF = " << n1;
return 0;
}
Output
Enter two numbers: 78
52
HCF = 26
Find HCF/GCD using for loop
#include <iostream>
using namespace std;
int main() {
int n1, n2, hcf;
cout << "Enter two numbers: ";
cin >> n1 >> n2;
void main()
{
int i,n,sum=0;
clrscr();
n=10;
for(i=1;i<=n;i++)
{
sum+=i;
}
cout<<"Sum: "<<sum;
getch();
}
Output
Sum: 55
(D)Even and Odd series
#include<iostream.h>
#include<conio.h>
void main()
{
int no;
clrscr();
cout<<"Enter any num: ";
cin>>no;
if(no%2==0)
{
cout<<"Even num";
}
else
{
cout<<"Odd num";
}
getch();
}
Output
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
int num,ans;
clrscr();
cout<<"Enter any Num: ";
cin>>num;
ans=pow(num,0.5);
cout<<"\n Squre of "<<num<<" is: "<<ans;
getch();
}
Download Code
Output
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
int num, ans;
clrscr();
cout<<"Enter any number";
cin>>num;
ans=pow(num, 1.0/3.0);
ans++;
cout<<"\n\Cube of "<<num<<" is: "<<ans;
getch();
}
Output
#include <iostream>
using namespace std;
int main ()
{
int num, x, y;
cout << "Enter a Number: ";
cin >> num;
cout << '\n';
for ( x=1; x<= num; x++)
{
}
cout << '\n';
}
system ("pause >0");
}
MY OUTPUT IS:
1234
1 2 3 4
1234
1234
(H)Checking prime number
#include<iostream.h>
#include<conio.h>
void main()
{
int i,no;
clrscr();
cout<<"Enter any num: ";
cin>>no;
if(no==1)
{
cout<<"Smallest prime num is 2";
}
for(i=2;i<no;i++)
{
if(no%i==0)
{
cout<<"Not prime num";
break;
}
}
if(no==i)
{
cout<<"Prime num";
}
getch();
}
Output
#include<conio.h>
#include<iostream.h>
void main()
{
clrscr();
int a, b, c;
cout <<"Enter any three numbers: ";
cin>>a;
cin>>b
cin>>c;
if(a>=b && a>=c)
{
cout<<"Largest number: "<<a;
}
if(b>=a && b>=c)
{
cout<<"Largest number: "<<b;
}
if(c>=a && c>=b)
{
cout<<"Largest number: "<<c;
}
getch();
}
Output
#include<iostream.h>
#include<conio.h>
void pass(int[],int);
void main()
{
int a[]={1,2,3,4,5};
clrscr();
pass(a,5);
getch();
}
void pass(int b[],int n)
{
int i;
for(i=0;i<n;i++)
{
cout<<"/n"<<b[i];
}
}
Output
1
2
3
4
5
(B) To Reverse an array
#include<iostream.h>
#include<conio.h>
void main()
{
int a[20],b[20],i,j,n;
clrscr();
cout<<"How many elements you want to enter: ";
cin>>n;
cout<<"Enter any "<<n<<" elements in Array: ";
for(i=0; i<n ;i++)
{
cin>>a[i];
}
cout<<"Reverse of Array: ";
for(i=n-1,j=0; i>=0;i--,j++)
{
b[i]=a[j];
}
for(i=0; i<n ;i++)
{
cout<<b[i];
}
getch();
}
Output
#include<iostream.h>
#include<conio.h>
void main()
{
int arr[20],i,n,sum=0;
clrscr();
cout<<"How many elements you want to enter: ";
cin>>n;
cout<<"Enter any "<<n<<" elements in Array: ";
for(i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"Sum of all Elements are: ";
for(i=0;i<n;i++)
{
sum=sum+arr[i];
}
for(i=0;i<n;i++)
{
}
cout<<sum;
getch();
}
Output
#include<iostream>
using namespace std;
int main()
{
int Arr[100],n,i,small,large;
for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}
small=Arr[0];
large=Arr[0];
for(i=1;i<n;i++)
{
if(Arr[i]<small)
small=Arr[i];
if(Arr[i]>large)
large=Arr[i];
}
cout<<"\nLargest element is :"<<large;
cout<<"\nSmallest element is :"<<small;
return 0;
}
Output
Enter number of elements you want to insert 5
Enter element 1: 13
Enter element 2: 69
Enter element 3: 30
Enter element 4: 51
Enter element 5: 11
Largest element is : 69
Smallest element is : 11
(E) To insert an element on array
#include<iostream.h>
#include<conio.h>
void main()
{
int i,a[5],no,pos;
clrscr();
cout<<"Enter data in Array: ";
for(i=0;i<5;i++)
{
cin>>a[i];
}
cout<<"\n\nStored Data in Array: ";
for(i=0;i<5;i++)
{
cout<<a[i];
}
cout<<"\n\nEnter position to insert number: ";
cin>>pos;
if(pos>5)
{
cout<<"\n\nThis is out of range";
}
else
{
cout<<"\n\nEnter new number: ";
cin>>no;
--pos;
for(i=5;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=no;
cout<<"\n\nNew data in Array: ";
for(i=0;i<6;i++)
{
cout<<a[i];
}
}
getch();
}
Download Code
Output
(E) To delete an element of from an array
#include<iostream.h>
#include<conio.h>
void main()
{
int i,a[5],no,pos;
clrscr();
cout<<"Enter data in array: ";
for(i=0;i<5;i++)
{
cin>>a[i];
}
cout<<"\n\nStored Data in array: ";
for(i=0;i<5;i++)
{
cout<<a[i];
}
cout<<"\n\nEnter poss. of element to delete: ";
cin>>pos;
if(pos>5)
{
cout<<"\n\nThis value is out of range: ";
}
else
{
--pos;
for(i=pos;i<=4;i++)
{
a[i]=a[i+1];
}
cout<<"\n\nNew data in array: ";
for(i=0;i<4;i++)
{
cout<<a[i];
}
}
getch();
}
Output
class Employee
{
public:
int salary // data member
void sal()
{
cout<<"Enter salary: ";
cin>>salary;
cout<<"Salary: "<<salary;
}
};
void main()
{
clrscr();
Employee e; //creating an object of Employee
e.sal();
getch();
}
Output
#include <iostream>
class Line {
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor
private:
double length;
};
return 0;
}
Output:
(B)Destructor
#include <iostream>
class Line {
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor declaration
~Line(); // This is the destructor: declaration
private:
double length;
};
Line::~Line(void) {
cout << "Object is being deleted" << endl;
}
return 0;
}
Output:
Object is being created
Length of line : 6
Object is being deleted
#include <iostream>
return 0;
}
Output:
Max (20,10): 20
Max (0,200): 200
Max (100,1010): 1010
#include <iostream>
class Box {
double width;
public:
friend void printWidth( Box box );
void setWidth( double wid );
};
return 0;
}
Output:
Width of box : 10
#include <iostream>
using namespace std;
class Box {
public:
double getVolume(void) {
return length * breadth * height;
}
// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;
// volume of box 2
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl;
// volume of box 3
volume = Box3.getVolume();
cout << "Volume of Box3 : " << volume <<endl;
return 0;
}
Output:
(A)Default argument
#include <iostream>
using namespace std;
void display(char = '*', int = 1);
int main()
{
cout << "No argument passed:\n";
display();
return 0;
}
Output
No argument passed:
$$$$$
(B)Function overloading
#include <iostream>
using namespace std;
class printData {
public:
void print(int i) {
cout << "Printing int: " << i << endl;
}
void print(double f) {
cout << "Printing float: " << f << endl;
}
void print(char* c) {
cout << "Printing character: " << c << endl;
}
};
int main(void) {
printData pd;
return 0;
}
Output:
Printing int: 5
Printing float: 500.263
Printing character: Hello C++
(C)Functions overriding.
class Base
{
public:
void shaow()
{
cout << "Base class\t";
}
};
class Derived:public Base
{
public:
void show()
{
cout << "Derived Class";
}
}
int main()
{
Base b; //Base class object
Derived d; //Derived class object
b.show(); //Early Binding Ocuurs
d.show();
}
Output : Base class Derived class
#include <iostream>
// Base class
class Shape {
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
// Derived class
class Rectangle: public Shape {
public:
int getArea() {
return (width * height);
}
};
int main(void) {
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
Total area: 35
REFERENCE BOOKS
Shukla, object oriented programming in C++, wiley India.
Stevens, Teach Yourself C++, BPB
Schildt H, 1997, C++ Complete Reference, TMH
Kanetkar Y, Programming in C++ ,BPB.
Mahapatra P.B, Thinking in C++, Khanna Publisher.
Bruce Euckel , Thinking in C++.
The second possibility was correct: the division was performed first. We say that
division takes precedence over addition.
Now consider this expression. What are two reasonable possible values? Which do
you think Maple will find?
> 2-3*4;
Here, the multiplication was performed prior to the subtraction. (Had the subtraction
been performed first, the answer would have been -4.) We say that multiplication
takes precedence over addition.
In the second expression above, replace the - operator with a + operator and
reevaluate. Which operator takes precedence, addition or multiplication?
Your experiments should reveal that in Maple (as in most other programming
languages) multiplication and division take precedence over addition and subtraction.
We say that multiplication and division are at a higher level of precedence than are
addition and subtraction.
What happens when an expression involves more than operator at the same level of
precedence? (For example, more than one multiplication and division or more than
one addition and subtraction.)
The division was performed first (to obtain 2/3), and the result was then multiplied by
4 (to obtain 8/3). Had the multiplication been performed before the division, the result
would have been 1/6.
This does not mean that multiplication takes precedence over division, however. In
the absence of parentheses, multiplication and division are performed left to right. We
say that multiplication and division are left associative.
Try to predict the outcome of each of the following calculations before you try it. If
your prediction turns out to be incorrect, be sure to figure out where the flaw in your
reasoning occurred.
> 2*3/4;
> 2/3/4;
> 4/3*2/5;
> 6*3/4*5;
Addition and subtraction are also left associative. In the absence of parentheses,
addition and multiplication is performed from left to right. In rational arithmetic (at
least in the absence of overlow) this does not make any difference, but in floating-
point arithmetic it can become important.
The following two expressions are mathematically identical; they differ only in the
fact that their last two terms have been interchanged. Evaluate them.
> 1 - 1 + 1e-12;
> 1 + 1e-12 - 1;
In the first expression, the subtraction is performed first and the result is zero. When
zero is added to the number 1e-12, we end up with the result 1e-12.
In the second expression, the addition is performed first. With only ten digits of
mantissa, however, the number 1e-12 is the lost in the roundoff and the result is 1.
The subtraction thus yields a result of zero. (This example shows that floating-point
arithmetic need not satisfy the associative rule.)
Exponentiation
What about the precedence of the exponentiation operator? See if you can figure out
the rule by evaluating the following expressions:
> 4*3^2;
> 1+3^2;
> 5/2^3;
> 3/10^3*7;
Notice that in each of these examples, the exponentiation was performed first. This is
indeed the way that it works: exponentiation has higher precedence than
multiplication and division, which in turn have higher precedence than addition and
multiplication.
or
> 2^(3^4);
Parentheses
> 2^(3*4);
> 2/(3/3);
If you have any doubt, use parentheses to make usre Maple executes your expressions
in the order in which you intend.
Summary