You are on page 1of 43

C++ PROGRAM

1. Write a Program for printing a single statement in C++

# 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

# include <iostream.h> # include <iostream.h>


# include <conio.h> # include <conio.h>
void main() void main()
{ {

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:

enter the values enter the values


6 2 6 2
The ans is=8 The ans is=4
3. Write a Programmes such as: Fibonacci Series ,GCD, Sum of
series, Even and Odd series, Finding root of a function, Sequence of
a numbers, Checking prime number, Largest among given number
etc.

(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() ;
}

Output of above program 

Enter the limit : 5


The fibonacci series is : 0 1 1 2 3
The sum of the fibonacci series is : 7
(B)GCD

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

// Swapping variables n1 and n2 if n2 is greater than n1.


if ( n2 > n1) {
int temp = n2;
n2 = n1;
n1 = temp;
}

for (int i = 1; i <= n2; ++i) {


if (n1 % i == 0 && n2 % i ==0) {
hcf = i;
}
}

cout << "HCF = " << hcf;


return 0;
}
(C)Sum of series
#include<iostream.h>
#include<conio.h>

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

Enter any num : 5


Odd num
(E)Finding root of a function

#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

Enter any Num: 9


Squre of 9 is: 3
(F)Cubic root

#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

Enter any Number: 27


Cube of 27 is: 3
(G)Sequence of a numbers

#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++)
{

for (y=1; y<=num; y++)


{
cout << ' ' << y;

}
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

Enter any num: 10


Not Prime Num
(I)Largest among given number

#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

Enter any three numbers:


15 30 20
Largest number: 30
4. Write a program for an Array :

(A) To Print an array

#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

How many elements you want to enter : 5


Enter any 5 elements in Array: 1 4 2 7 5
Reverse of Array: 5 7 2 4 1
(C) To Sum of an array

#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

How many elements you want to enter : 5


Enter any 5 elements in Array: 1 4 2 7 5
Sum of all Elements are: 19
(D) To Find max and min element of an array

#include<iostream>
using namespace std;
int main()
{
int Arr[100],n,i,small,large;

cout<<"Enter number of elements you want to insert ";


cin>>n;

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

Enter data in array: 10 20 30 40 50


Stored Data in array: 10 20 30 40 50
Enter poss. of element to delete: 2
New data in array: 10 20 40 50

5. Write a program for classes and objects.


#include<iostream.h>
#include<conio.h>

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

Enter salary: 4500


Salary: 4500

6. Write a program for constructor & destructor.


(A).Constructor

#include <iostream>

using namespace std;

class Line {
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor

private:
double length;
};

// Member functions definitions including constructor


Line::Line(void) {
cout << "Object is being created" << endl;
}

void Line::setLength( double len ) {


length = len;
}

double Line::getLength( void ) {


return length;
}
// Main function for the program
int main( ) {
Line line;

// set line length


line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;

return 0;
}

Output:

Object is being created


Length of line : 6

(B)Destructor

#include <iostream>

using namespace std;

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

// Member functions definitions including constructor


Line::Line(void) {
cout << "Object is being created" << endl;
}

Line::~Line(void) {
cout << "Object is being deleted" << endl;
}

void Line::setLength( double len ) {


length = len;
}

double Line::getLength( void ) {


return length;
}
// Main function for the program
int main( ) {
Line line;

// set line length


line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;

return 0;
}

Output:
Object is being created
Length of line : 6
Object is being deleted

7. Write a program for inline functions.

#include <iostream>

using namespace std;


inline int Max(int x, int y) {
return (x > y)? x : y;
}

// Main function for the program


int main( ) {

cout << "Max (20,10): " << Max(20,10) << endl;


cout << "Max (0,200): " << Max(0,200) << endl;
cout << "Max (100,1010): " << Max(100,1010) << endl;

return 0;
}

Output:

Max (20,10): 20
Max (0,200): 200
Max (100,1010): 1010

8. Write a program for friend functions.

#include <iostream>

using namespace std;

class Box {
double width;
public:
friend void printWidth( Box box );
void setWidth( double wid );
};

// Member function definition


void Box::setWidth( double wid ) {
width = wid;
}

// Note: printWidth() is not a member function of any class.


void printWidth( Box box ) {
/* Because printWidth() is a friend of Box, it can
directly access any member of this class */
cout << "Width of box : " << box.width <<endl;
}

// Main function for the program


int main( ) {
Box box;

// set box width with member function


box.setWidth(10.0);

// Use friend function to print the wdith.


printWidth( box );

return 0;
}

Output:

Width of box : 10

9. Write a program for operator overloading.

#include <iostream>
using namespace std;

class Box {
public:
double getVolume(void) {
return length * breadth * height;
}

void setLength( double len ) {


length = len;
}

void setBreadth( double bre ) {


breadth = bre;
}

void setHeight( double hei ) {


height = hei;
}

// Overload + operator to add two Box objects.


Box operator+(const Box& b) {
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
return box;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
// Main function for the program
int main( ) {
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
Box Box3; // Declare Box3 of type Box
double volume = 0.0; // Store the volume of a box here
// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);

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

// Add two object as follows:


Box3 = Box1 + Box2;

// volume of box 3
volume = Box3.getVolume();
cout << "Volume of Box3 : " << volume <<endl;
return 0;
}
Output:

Volume of Box1 : 210


Volume of Box2 : 1560
Volume of Box3 : 5400

10. Write a program for default arguments, function overloading,


functions overriding.

(A)Default argument

// C++ Program to demonstrate working of default argument

#include <iostream>
using namespace std;
void display(char = '*', int = 1);

int main()
{
cout << "No argument passed:\n";
display();

cout << "\nFirst argument passed:\n";


display('#');

cout << "\nBoth argument passed:\n";


display('$', 5);

return 0;
}

void display(char c, int n)


{
for(int i = 1; i <= n; ++i)
{
cout << c;
}
cout << endl;
}

Output
No argument passed:

First argument passed:

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

// Call print to print integer


pd.print(5);

// Call print to print float


pd.print(500.263);

// Call print to print character


pd.print("Hello C++");

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

11. Write a Program for inheritance.

#include <iostream>

using namespace std;

// 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);

// Print the area of the object.


cout << "Total area: " << Rect.getArea() << endl;
return 0;
}
Output:

Total area: 35

12. Write a program to counting a character in words.


Practicle

1. Problems involving sequence,


selection and iteration.

2. Small problems mainly computational to


illustrate expression and operator
precedence.

7. Problems related to inline functions.

8. Problems related to friend functions.

9. Problems related to operator


overloading.

10. Problems related to default arguments,


function overloading, functions overriding.

11. Problems related to different types of


inheritance.

12. Moderately large function based problems


for which the solutions should be represented
by coordinating modules. Formatting a text,
replacing a given word in a text with another,
counting the number of words, in a text.
REFERENCES
TEXT BOOKS
Balguruswamy E. (2001), Object-Oriented Programming with Turbo C++, 3rd edition, TMH.
Lafore Rober, 2001), Object-Oriented Programming in Turbo C++, 3rd edition, Galgotia
Publications.
M. kumar, programming with C ++,

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

Introduction to object oriented programming in C++,TMH ISRD group

2. Multiplication/Division vs. Addition/Subtraction


Examine the Maple expression below. Try to predict what its value will be before you
ask Maple to evaluate it. There seem to be two possibilities. If the addition is
performed first, the result will be 4/2, which is 2. If the division is performed first the
result will be 5/2. Which is it? Try it out and see.
> 1+3/2;

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.

Now try the following experiments:


 In the first expression above, replace the + operator with a - operator and
reevaluate. Which operator takes precedence, subtraction or division?

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

Multiplication vs. Division and Addition vs. 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.)

What would be two possible values for this expression?


> 2/3*4;

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;

The answers are not the same! Why?

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.

Now try this expression.


> 2^3^4;

The exponentiation operator is non-associative. If you wish to do more than one


exponentiation in an expression, you must use parentheses to tell Maple whether you
want
> (2^3)^4;

or
> 2^(3^4);

Parentheses

Speaking of parentheses, you can override Maple's precedence rules by using


parentheses to group expressions in the order that you wish Maple to evaluate them.
Here are some examples where we use parentheses to force a different evaluation
order than Maple would otherwise use.
> 2*(3+4);

> 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

 There are five arithmetic operators: ^, *, /, +, and -.

 Exponentiation (^) is at the highest level of precedence, multiplication (*) and


division (/) are at a lower level of precedence, and addition (+) and subtraction
(-) are at an even lower level of precedence.

 In the absence of parentheses, operators at a higher level of precedence are


performed before operators at a lower level of precedence.

 In the absence of parentheses, multiplication and division are performed from


left to right, as are addition and subtraction.

 Parentheses can be used to explicitly control the order of evaluation of an


expression

You might also like