Uploaded by pexel46440

c++ object oriented programming

advertisement
DIFFERENCE BETWEEN C AND C++
 c is a procedure/function-oriented language
and c++ language is driven by a
procedure/object.
 Data is not protected in c, whereas data is
secured in c++. Data hiding concept is absent
in c.
 C uses a top down approach while c++ uses a
bottom up approach.
 In c we can’t give the same name to two
function in a program, whereas due to the
function overloading feature, the above concept
is possible in c++.
 C uses printf ( ) and scanf ( ) functions to write
and read the data, while c++ uses cout and cin
objects for output and input operations.
 C uses stdio.h file for input and output functions
whereas c++ uses iostream.h for these
functions.
 In c including of header file is optional but in c++
it is compulsory.
INTRODUCTION TO OBJECT ORIENTED
PROGRAMMING
Oop is a programming paradigm based on the
concept of “objects” and “class”.
Oops technique is added in a program to
improve security and reduce complexity.
In oop, computer programs are designed by
making them out of objects that interact with
one another.
Oops give facility to collect more than variable
and function into a single name.
Most of the oops based language are c++, java,
python, etc.
FEATURES OF OOPS
A.
Data hiding
B.
Encapsulation
C.
Inheritance
D.
Containership
E.
Polymorphism
F.
Templates
G.
Exception handling
H.
Reflection
Characteristics of object- oriented languages:-
A. Class: A class is only the declaration that
declares more than variable and function having
same purpose into a single name.
B. Object: An object is an instance of class that
means object is responsible to allocate memory
space for all members of class.
C. Encapsulation: The packing of data and
functions into a single component is known as
encapsulation.
D. Method: An operation required for an object or
entity when coded in class is called a method.
E.
Data abstraction: Abstraction refers to the
procedure of representing essential features
without including the background details.
F. Inheritance: Inheritance is the method by which
objects of one class get the properties of objects
of another class.
G. Polymorphism: polymorphism allows the same
function to act in a different way in different class.
H. Reusability: oop technology allows reusability
of the class by extending them to other classes
using inheritance.
ADVANTAGE OF OOP
Oop has many advantage but some of them
are: Oop can be comfortably upgraded.
 Using inheritance, we can eliminate
Redundant program code and continue the use
of previously defined class.
 The technology of data hiding facilitates the
programmers to design and develop safe
programs that do not disturb code in other parts
of program.
 The encapsulation feature provided by oop
languages allows programmers to define the
class with many functions and characteristics and
only few functions are exposed to the user.
 All oop languages allows creating extended and
reusable parts of programs.
 Oop changes the way of thinking of the
programmer .This result in rapid development of
new software in a short time.
 Objects communicate with each other and pass
messages.
Q1. Use of C++
C++ is a versatile language for handling very large
programs. It is suitable for virtually any
programming task including development of editors,
compilers, databases, communication systems and
any complex real-life application systems.
1. Since C++ allows us to create hierarchy-related
objects, we can build special object-oriented
libraries which can be used later by many
programmers.
2. While C++ is able to map the real-world problem
property, the c part of C++ gives the language the
ability to get close to the machine-level details.
3. C++ programs are easily maintainable and
expandable. When a new feature needs to be
implemented, it is very easy to add to the existing
structure of an object.
4. It is expected that C++ will replace as a generalpurpose language in the near future.
Q2. STRUCTURE OF C++ PROGRAM
A typical C++ program would contain four sections
as given below:
• • include files
• • class declaration
• • member functions definitions
• • main function program
These sections may be placed in separate code
files and then compiled independently or jointly.
It is a common practice to organize a program into
three separate files. The declarations are placed in
a header file and the definitions of member
functions go into another file. This approach
enables the programmer to separate the abstract
specification of the interface (class definition) from
the implementation details (member functions
definition). Finally, the main program that uses the
class is placed in a third file which “includes” the
previous two files as well as any other files required.
This approach is based on the concept of clientserver model as given below:
Member functions
Class definition
Main function program
The class definition including the member functions
constitute the server that provides services to the
main program known as client. The client uses the
server through the public interface of the class.
Q3. Important terminology of object-oriented
programming
1 Dynamic binding
Binding refers to the linking of a procedure call to
the code to be executed in response to the call.
Dynamic binding also known as late binding means
that the code associated with a given procedure call
is not known until the time of the call at run-time. It
is associated with polymorphism and inheritance. A
function call associated with a polymorphic
reference depends on the dynamic type of that
reference
2 message passing
An object-oriented program consists of a set of
objects that communicate with each other. The
process of programming in an object-oriented
language, therefor, involves the following basic
steps:
I
I. creating classes that define objects and their
behaviour,
II II. creating objects from class definitions, and
III III. Establishing communication among objects.
Objects communicate with one another by sending
and receiving information much the same way as
people pass messages to one another. The concept
of message passing makes it easier to talk about
building systems that directly model or simulate
their real-world counterpart.
A message for an object is a request for execution
of a procedure, and therefore will invoke a function
(procedure) in the receiving object that generates
the desired result. Message passing involves
specifying the name of the object, the name of the
function (message) and the information to be sent.
3 Delegation in OOPs
Two classes can be joined either by inheritance or
delegation which provide reusability of program.
When object of one class is used as data mamboing
in other class such composition of object is known
as delegation.
4 Generosity
The software component of the program has more
than one version depending upon the data type of
the arguments. This feature allow the declaration of
variable without specifying exact data type. The
compiler identify the data type at run time.
Programmer can create a function that can be used
or any type of data.
(Data
Type
in
C++)
While writing program in any language, you need
to use various variables to store various
information. Variables are nothing but reserved
memory locations to store values. This means that
when you create a variable you reserve some
space in memory.
You may like to store information of various data
types like character, wide character, integer,
floating point, double floating point, Boolean etc.
Based on the data type of a variable, the operating
system allocates memory and decides what can be
stored in the reserved memory.
Primitive Built-in Types
C++ offers the programmer a rich assortment of
built-in as well as user defined data types.
Following table lists down seven basic C++ data
types −
Type
Keyword
Boolean
bool
Character
char
Integer
int
Floating
point
float
Double
double
floating point
Valueless
void
Wide
character
wchar_t
Several of the basic types can be modified using
one or more of these type modifiers −
signed
 unsigned
 short
 long
The following table shows the variable type, how
much memory it takes to store the value in
memory, and what is maximum and minimum value
which can be stored in such type of variables.

Type
Typical Typical
Bit
Range
Width
char
1byte
-127 to 127 or
0 to 255
unsigned 1byte
char
0 to 255
signed
char
-127 to 127
1byte
int
4bytes
-2147483648
to
2147483647
unsigned 4bytes
int
0
to
4294967295
signed
int
4bytes
-2147483648
to
2147483647
short int
2bytes
-32768
32767
to
unsigned Range
short int
0 to 65,535
signed
short int
Range
-32768
32767
long int
4bytes
2,147,483,648
to
2,147,483,647
to
signed
long int
4bytes
same as long
int
unsigned 4bytes
long int
0
to
4,294,967,295
float
4bytes
+/- 3.4e +/- 38
(~7 digits)
double
8bytes
+/- 1.7e +/308
(~15
digits)
long
double
8bytes
+/- 1.7e +/308
(~15
digits)
wchar_t
2 or 4 1
wide
bytes
character
The size of variables might be different from those
shown in the above table, depending on the
compiler and the computer you are using.
Following is the example, which will produce
correct size of various data types on your
computer.
Live Demo
#include <iostream>
using namespace std;
int main() {
cout << "Size of char : " << sizeof(char) << endl;
cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of short int : " << sizeof(short int) <<
endl;
cout << "Size of long int : " << sizeof(long int) <<
endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) <<
endl;
cout << "Size of wchar_t : " << sizeof(wchar_t) <<
endl;
return 0;
}
This example uses endl, which inserts a new-line
character after every line and << operator is being
used to pass multiple values out to the screen. We
are also using sizeof() operator to get size of
various data types.
When the above code is compiled and executed, it
produces the following result which can vary from
machine to machine −
Size of char : 1
Size of int : 4
Size of short int : 2
Size of long int : 4
Size of float : 4
Size of double : 8
Size of wchar_t : 4
typedef Declarations
You can create a new name for an existing type
using typedef. Following is the simple syntax to
define a new type using typedef −
typedef type newname;
For example, the following tells the compiler that
feet is another name for int −
typedef int feet;
Now, the following declaration is perfectly legal and
creates an integer variable called distance −
feet distance;
Enumerated Types
An enumerated type declares an optional type
name and a set of zero or more identifiers that can
be used as values of the type. Each enumerator is
a constant whose type is the enumeration.
Creating an enumeration requires the use of the
keyword enum. The general form of an
enumeration type is −
enum enum-name { list of names } var-list;
Here, the enum-name is the enumeration's type
name. The list of names is comma separated.
For example, the following code defines an
enumeration of colours called colours and the
variable c of type colour. Finally, c is assigned the
value "blue".
enum colour { red, green, blue } c;
c = blue;
By default, the value of the first name is 0, the
second name has the value 1, and the third has the
value 2, and so on. But you can give a name, a
specific value by adding an initializer. For example,
in the following enumeration, green will have the
value 5.
enum colour { red, green = 5, blue };
Here, blue will have a value of 6 because each
name will be one greater than the one that
precedes it.
…………………………………..
…………………..
………………………………..
(Operators in C++)
An operator is a symbol that tells the compiler to
perform specific mathematical or logical
manipulations. C++ is rich in built-in operators and
provide the following types of operators −
Arithmetic Operators
 Relational Operators
 Logical Operators
 Bitwise Operators
 Assignment Operators
 Misc Operators
This chapter will examine the arithmetic, relational,
logical, bitwise, assignment and other operators
one by one.

Arithmetic Operators
There are following arithmetic operators supported
by C++ language −
Assume variable A holds 10 and variable B holds
20, then −
Show Examples
Operator Description Example
+
Adds
two A + B will
operands
give 30
-
Subtracts
A - B will
second
give -10
operand
from the first
*
Multiplies
both
operands
/
Divides
B / A will
numerator
give 2
by
denumerator
%
Modulus
B % A
Operator
will give
and
0
remainder
of after an
integer
division
A * B will
give 200
++
Increment A++ will
operator,
give 11
increases
integer
value
by
one
--
Decrement A-- will
operator,
give 9
decreases
integer
value
by
one
Relational Operators
There are following relational operators supported
by C++ language
Assume variable A holds 10 and variable B holds
20, then −
Show Examples
Operator Description Example
==
Checks
if (A == B)
the values is
not
of
two true.
operands
are equal or
not, if yes
then
condition
becomes
true.
!=
Checks
if (A != B)
the values is true.
of
two
operands
are equal or
not, if values
are
not
equal then
condition
becomes
true.
>
Checks
if (A > B) is
the value of not true.
left operand
is
greater
than
the
value
of
right
operand, if
yes
then
condition
becomes
true.
<
Checks
if (A < B) is
the value of true.
left operand
is less than
the value of
right
operand, if
yes
then
condition
becomes
true.
>=
Checks
if (A >= B)
the value of is
not
left operand true.
is
greater
than
or
equal to the
value
of
right
operand, if
yes
then
condition
becomes
true.
<=
Checks
if (A <= B)
the value of is true.
left operand
is less than
or equal to
the value of
right
operand, if
yes
then
condition
becomes
true.
Logical Operators
There are following logical operators supported by
C++ language.
Assume variable A holds 1 and variable B holds 0,
then −
Show Examples
Operator Description Example
&&
Called
(A && B)
Logical AND is false.
operator. If
both
the
operands
are
nonzero, then
condition
becomes
true.
||
Called
(A || B) is
Logical OR true.
Operator. If
any of the
two
operands is
non-zero,
then
condition
becomes
true.
!
Called
!(A && B)
Logical NOT is true.
Operator.
Use
to
reverses the
logical state
of
its
operand. If a
condition is
true,
then
Logical NOT
operator will
make false.
Bitwise Operators
Bitwise operator works on bits and perform bit-bybit operation. The truth tables for &, |, and ^ are as
follows −
p
q
p & p|q p ^
q
q
0
0
0
0
0
0
1
0
1
1
1
1
1
1
0
1
0
0
1
1
Assume if A = 60; and B = 13; now in binary format
they will be as follows −
A = 0011 1100
B = 0000 1101
----------------A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
The Bitwise operators supported by C++ language
are listed in the following table. Assume variable A
holds 60 and variable B holds 13, then −
Show Examples
Operator Description Example
&
Binary AND
Operator
copies a bit
to the result
if it exists in
both
operands.
(A & B) will
give
12
which
is
0000 1100
|
Binary OR
Operator
copies a bit if
it exists in
either
operand.
(A | B) will
give
61
which
is
0011 1101
^
Binary XOR
Operator
copies the
bit if it is set
(A ^ B) will
give
49
which
is
0011 0001
in
one
operand but
not both.
~
Binary Ones
Complement
Operator is
unary and
has
the
effect
of
'flipping' bits.
<<
Binary Left
Shift
Operator.
The
left
operands
value
is
moved left
by
the
number of
bits
(~A ) will
give
-61
which
is
1100 0011
in
2's
complement
form due to
a
signed
binary
number.
A << 2 will
give
240
which
is
1111 0000
specified by
the
right
operand.
>>
Binary Right
Shift
Operator.
The
left
operands
value
is
moved right
by
the
number of
bits
specified by
the
right
operand.
A >> 2 will
give
15
which
is
0000 1111
Assignment Operators
There are following assignment
supported by C++ language −
Show Examples
Operator Description Example
operators
=
+=
-=
Simple
assignment
operator,
Assigns
values from
right
side
operands to
left
side
operand.
C=A+B
will assign
value of A
+ B into C
Add
AND
assignment
operator, It
adds right
operand to
the
left
operand
and assign
the result to
left operand.
C += A is
equivalent
to C = C +
A
Subtract
AND
assignment
operator, It
C -= A is
equivalent
to C = C A
subtracts
right
operand
from the left
operand
and assign
the result to
left operand.
*=
/=
Multiply
AND
assignment
operator, It
multiplies
right
operand
with the left
operand
and assign
the result to
left operand.
C *= A is
equivalent
to C = C *
A
Divide AND
C /= A is
assignment
equivalent
operator, It
divides left to C = C /
operand
A
with
the
right
operand
and assign
the result to
left operand.
%=
<<=
Modulus
AND
assignment
operator, It
takes
modulus
using
two
operands
and assign
the result to
left operand.
C %= A is
equivalent
to C = C
%A
Left
shift
AND
assignment
operator.
C <<= 2 is
same as
C = C <<
2
>>=
Right shift
AND
assignment
operator.
C >>= 2 is
same as
C = C >>
2
&=
Bitwise AND C &= 2 is
assignment same as
operator.
C=C&2
^=
Bitwise
exclusive
C ^= 2 is
OR
and same as
assignment C = C ^ 2
operator.
|=
Bitwise
inclusive
C |= 2 is
OR
and same as
assignment C = C | 2
operator.
Misc Operators
The following table lists some other operators that
C++ supports.
Sr.No Operator
Description
1
&
sizeof
sizeof
operator returns
the size of a
variable.
For
example, sizeof(a),
where ‘a’ is integer,
and will return 4.
2
Condition? X : Y
Conditional
operator (?). If
Condition is true
then it returns value
of
X
otherwise
returns value of Y.
3
,
Comma
operator causes a
sequence
of
operations to be
performed.
The
value of the entire
comma expression
is the value of the
last expression of
the
commaseparated list.
4
. (dot)
(arrow)
and
->
Member
operators are used
to
reference
individual members
of
classes,
structures,
and
unions.
5
Cast
Casting
operators convert
one data type to
another.
For
example,
int(2.2000)
would
return 2.
6
&
Pointer operator
& returns
the
address
of
a
variable.
For
example &a; will
give actual address
of the variable.
7
*
Pointer operator
* is pointer to a
variable.
For
example *var; will
pointer to a variable
var.
Operators Precedence in C++
Operator precedence determines the grouping of
terms in an expression. This affects how an
expression is evaluated. Certain operators have
higher precedence than others; for example, the
multiplication operator has higher precedence than
the addition operator −
For example x = 7 + 3 * 2; here, x is assigned 13,
not 20 because operator * has higher precedence
than +, so it first gets multiplied with 3*2 and then
adds into 7.
Here, operators with the highest precedence
appear at the top of the table, those with the lowest
appear at the bottom. Within an expression, higher
precedence operators will be evaluated first.
Show Examples
Category
Operator Associativity
Postfix
() [] -> . ++ Left to right
--
Unary
+ - ! ~ ++ - Right to left
- (type)* &
sizeof
Multiplicative
*/%
Left to right
Additive
+-
Left to right
Shift
<< >>
Left to right
Relational
< <= > >= Left to right
Equality
== !=
Left to right
Bitwise AND
&
Left to right
Bitwise XOR
^
Left to right
Bitwise OR
|
Left to right
Logical AND
&&
Left to right
Logical OR
||
Left to right
Conditional
?:
Right to left
Assignment
= += -= *= Right to left
/= %=>>=
<<= &= ^=
|=
Comma
,
Left to right
(Statement in C++)
Decision making structures require that the
programmer specify one or more conditions to be
evaluated or tested by the program, along with a
statement or statements to be executed if the
condition is determined to be true, and optionally,
other statements to be executed if the condition is
determined to be false.
Following is the general form of a typical decision
making structure found in most of the programming
languages −
C++ programming language provides following
types of decision making statements.
Sr.No Statement
Description
1
&
if statement
An ‘if’ statement
consists
of
a
Boolean expression
followed by one or
more statements.
2
if...else statement
An ‘if’ statement can
be followed by an
optional
‘else’
statement,
which
executes when the
Boolean expression
is false.
3
switch statement
A ‘switch’ statement
allows a variable to
be
tested
for
equality against a
list of values.
4
nested
statements
if
You can use one ‘if’
or ‘else if’ statement
inside another ‘if’ or
‘else
if’
statement(s).
5
nested
switch
statements
You can use one
‘switch’ statement
inside
another
‘switch’
statement(s).
The? : Operator
We have covered conditional operator “? :” in
previous chapter which can be used to
replace if...else statements. It has the following
general form −
Exp1 ? Exp2 : Exp3;
Exp1, Exp2, and Exp3 are expressions. Notice the
use and placement of the colon.
The value of a ‘?’ expression is determined like this:
Exp1 is evaluated. If it is true, then Exp2 is
evaluated and becomes the value of the entire ‘?’
expression. If Exp1 is false, then Exp3 is evaluated
and its value becomes the value of the expression
LOOPING & DECISION
 Looping or Iteration structure are used for
executing a set of instruction for specific
number of times.
 For example :- process that involves
calculation on same set of variable, must be
coded using this structure – adding number
from 1 to 10, finding a factorial of number,
finding a sign or a cross series values, any
other series value such as factorial.





WHILE LOOP :C++ while loops statement allows to repeatedly
run the same block of code until a condition is
met……. While loop has one control condition,
and executes as long the condition is true. The
condition of the loop is tested before the body
of the loop is executed, hence it is called an
entry- controlled loop.
A while loop consists of a logical expression
and a set of instruction.
The set of instruction, that forms the body of the
while ‘true’. The body will be executed only if
the expression is true, thus it is positive.
Before execution of the body the condition is
checked thus while loop is pre-test loop.
The logical expression of the while loop
consists of a variable for which a value is
checked.
The
movement the control variable value exceeds
or is less of the value being checked, the
execution of the loop body terminate.
 Such a value is known as the base value of the
exit value.
SYNTAX :- while ( condition )
{
Statement;
}
FLOW CHART :False
Test
condition
exit
True
statement
EXAMPLE :- int n = 1, S = 0;
while ( n < = 100 )
{
S = S + n;
n++;
}
DO – WHILE : As discussed in the last tutorial about while
loop, a loop is used for repeating a block of
statements until the given loop condition return
false, In this tutorial we will see do- while. Do
while loop is similar to while loop, however
there is a difference between them: In the while
loop, condition is evaluate first and then the
statements inside loop body gets executed, on
the other hand in do – while loop, statements
inside do – while gets executed first and then
the condition is evaluated.
SYNTAX :- do
{
Body of loop
…………
} while ( test condition )
FLOW CHART :-
Body of loop
Test
condition
exit
true
EXAMPLE :- int n = 1, S = 0;
Do
{
S = S + n;
n++;
} while ( n <= 100 )
FOR LOOP : A loop is used for executing a block of
statements repeatedly until a particular
condition is satisfied. For example, when we
are displaying number from 1 to 100 we may
want set the value of variable to 1 and display it
100 times, increasing its value 1 on each loop
iteration.
SYNTAX :increment /
For ( initialization; condition;
decrement)
{
statement;
………………;
}
FLOW CHART :Initialization
false
Test
condition
exit
true
body of loop
FIRST STEP :- In for loop, initialization happens
first and only once, which means that the
initialization part of for loop only executes once.
SECOND STEP :- Condition in for loop is
evaluated on each loop iteration, if the condition
is true then the statements inside for the loop
body gets executed. Once the condition returns
false, the statements in for loop does not execute
and the control gets transferred to the next
statement in the program after for loop.
THIRD STEP :- After every execution for loop’s
body, the increment / decrement part of the loop
executes that updates the loop counter.
FOURTH STEP :- After third step, the control
jumps to second step and condition is reevaluated.
VARIABLE IN C++ FUNCTION
 A Variable is an object that may take an values
of an specified type that values whose values
always changes doing execution of the
program.
 Variables in C++ A variable is a name which is
associated with is associated with a value that
can be changed. For example when I write int
num = 20; here variable name is num which is
associated with value 20, int is a data type that
represents that this variable can hold integer
values.
 The variable must be declared by specifying
the data type and identifiers. The general
syntax of the variable declared are as follows
data type :SYNTAX :- 2d1, 2d2, 2d3-------------2dn;
EXAMPLE :- int x, y, z;
short int a;
char a;
CIN FUNCTION :- ( Console Input )
 Cin is a reading a function. Cin is used to read
a number, a character, or a string of character
from standard input devices normally the
keyboard extraction operator ( double greater
than >> ) is used to C in operator.
 The Cin is a predefined object of istream class.
It is connected with the standard input devices,
which is usually a keyboard. The Cin is used in
conjunction with stream extraction operator ( >>
) to read the input from a console.
DECLARATION : It is defined in <iostream> header file. The Cin
object is ensured to be initialized during or
before the first time an object of type
ios_base::\nit is constructed. After the cin object
is constructed, cin.tie() return & cout which
means that any formatted input operation on cin
forces a call to cout.flush() if any character are
pending for output
 The Cin object is used along with the extraction
operator ( >>) in order to receive a stream of
character.
SYNTAX :- cin >> variable name;
EXAMPLE :- int x, y,;
Cin >> x >>y;
COUT FUNCTION :- ( Console output )
 Cout function is a writing a function. Its stands
for console output. Cout is used to display an
object of output of the standard devices
normally insertion operators ( double less than
<< ). It is associated with the standard C output
stream stdout.
DECLARATION : It is defined in <iostream> header file. The cout
object is ensured to be initialized during or
before the first time an object of type
ios_base::lnit is constructed. After the cout
object is constructed, it is tied to cin which
means that any input operation on cin executes
cout.flush().
 The “C” in cout refers to “character” and ‘out’
means “output”, hence cout means “character
output”. The cout object is used along with the
insertion operator ( << ) in order to display a
stream of character.
SYNTAX :- Cout << variable name;
:- Cout << “some string”;
EXAMPLE :- int x, y;
Cin >> x >> y;
EXAMPLE OF COUT :# Include <iostream.h>
# Include <conio.h>
Void main ()
{
int i;
for ( i =1; i >=1; i++)
{
cout << “ value of variable i is :<< i ;
}
}
ENDL : This function is used to output manipulator. To
generate a character return or like field
character. The endl may be used several time
in cout “ sum of a = “<<.
 The “endl” is a library keyword which is used to
end a line in a C++ stream. Its usage is : cout
<< “This is a line of text” << std:: endl << “This
is another line of text”; This is a substitute for
putting a “ /r/n” at the end of our print statement.
VOID MAIN : The parenthesis of the following word main are
distinguish features of a function without
parenthesis complier. Whose think main refers
to a variable or to some other program
elements.
 The word void preceding of the main function
name indicate that this is the particular function
does not return the values.
 The ANSI standard says “no” to the ‘void main’
and thus using it can be considered wrong. One
should stop using the ‘void main’ if doing so. Int
main- ‘int main’ means that our function needs
to return some integers at the end of the
execution and we do so by returning 0 at the
end of the program.
 1.Write a program in c++ to find the addition,
subtraction, multiplication, division of a
given number.

 #include <iostream>
 #include<conio.h>
 void main()
 {

int num1, num2, add, subtract, multiply;

float divide;


cout << "Please enter two integer: ";

cin >> num1;

cin >> num2;


add
= num1 + num2;

subtract = num1 - num2;

multiply = num1 * num2;

divide = num1 / (float)num2;


cout << endl <<"Sum = " << add;

cout << endl <<"Difference = " << subtract;

cout << endl <<"Multiplication = " << multiply;

cout << endl <<"Division = " << divide;


getch();
 }
 2.Write a program in c++ to find the sum of
even numbers upto n terms


 #include<iostream.h>
 #include<conio.h>
 void main()
 {
clrscr();

int n,i,sum=0,res=0;

cout<<"enter no. of terms";

cin>>n;

for(i=0;i<n;i++)

{

res=res+sum;

sum=sum+2;

}

cout<<“sum
of
even
upto“<<n<<”terms”<<res;

getch();
 }




















 3.Write a program in c++ to find the sum of
odd numbers up to n terms.



 #include<iostream.h>
 #include<conio.h>
 void main()
 {
clrscr();

int n,i,sum=0,res=0;









 }
cout<<"enter no. of terms";
cin>>n;
for(i=0;i<n;i++)
{
res=res+sum;
sum=sum+2;
}
cout<<res;
getch();
 4.Write a program in c++ to print prime
numbers up to n.

 #include<iostream>
 #include<conio.h>
 void main()
 {

int num, i, j, isPrime, n;

cout << "Enter the value of N\n";

cin >> num;

for(i = 2; i <= num; i++){

isPrime = 0;


for(j = 2; j <= i/2; j++)

{

if(i % j == 0)

{

isPrime = 1;

break;

}

}

if(isPrime==0 && num!= 1)

cout << i << " ";

}
 getch();
 }


 5. Write a program in c++ to find the sum of
the square of even number up to n terms.

 #include<iostream.h>
 #include<conio.h>
 Void main()
 {

int n,i,sum=0,res=0;

cout<<"enter no. of terms";

cin>>n;

for(i=0;i<n;i++)

{

res=res+(sum*sum);

sum=sum+2;

}

cout<<”sum of square of even number upto
”<<n<<”terms”<<res;

getch();
 }

 6.Write a program in c++ to find the sum of
square of n natural numbers.

 #include<iostream>
 #include<conio.h>
 Void main()
 {

int n,i,sum=0,res=0;

cout<<"enter no. of terms:";

cin>>n;

for(i=0;i<n;i++)

{

res=res+(i*i);

}

cout<<"sum of square of even number upto
"<<n<<" terms is "<<res;
 getch();
 }
 7. Write a program in c++ to find the factorial
of given number.

 #include <iostream>
 #include<conio.h>
 void main()
 {

unsigned int n;

unsigned long long factorial = 1;

cout << "Enter a positive integer: ";

cin >> n;

for(int i = 1; i <=n; ++i)

{

factorial *= i;

}

cout << "Factorial of " << n << " = " <<
factorial;

getch();
 }
 8. Write a program in c++ to change
Fahrenheit in degree Celsius.

 #include <iostream.h>
 #include <conio.h>
 Void main()
 {

float f,c;

cout<<"enter faranhiet";

cin>>f;

c=(f-32)*5/9;

cout<<endl<<"temp in cel is"<<c;

getch();
 }
 9. Write a program in c++ to print the
Fibonacci series.

 #include <iostream,h>
 #include<conio.h>
 void main()
 {

int n, t1 = 0, t2 = 1, nextTerm = 0;

cout << "Enter the number of terms: ";

cin >> n;

cout << "Fibonacci Series: ";

for (int i = 1; i <= n; ++i)

{

if(i == 1)

{

cout << " " << t1;

continue;

}

if(i == 2)

{

cout <<" "<< t2 << " ";

continue;

}

nextTerm = t1 + t2;

t1 = t2;

t2 = nextTerm;


cout << nextTerm << " ";

}

getch();
 }
 10. Write a program in c++ to find the
greatest number among 3 numbers.

 #include <iostream>
 #include<conio.h>
 Void main()
 {

int n1, n2, n3;

cout << "Enter three numbers: ";

cin >> n1 >> n2 >> n3;

if(n1 >= n2 && n1 >= n3)

{

cout << "Largest number: " << n1;

}

if(n2 >= n1 && n2 >= n3)

{

cout << "Largest number: " << n2;

}

if(n3 >= n1 && n3 >= n2) {

cout << "Largest number: " << n3;

}

getch();
 }
 11. Write a program in c++ to print the
multiplication table of a given number.

 #include <iostream>
 #include<conio.h>
 void main()
 {

int n;

cout << "Enter a positive integer: ";

cin >> n;

for (int i = 1; i <= 10; ++i)
 {

cout << n << " * " << i << " = " << n * i <<
endl;

}

getch();
 }
 12.Write a program in c++ to find all roots of
a quadratic equation.

 #include <iostream>
 #include <math.h>
 #include<conio.h>
 void main()
 {

int a, b, c;

float x1, x2, d, real, imaginary;

cout << "Enter coefficients a, b and c: ";

cin >> a >> b >> c;

d = b*b - 4*a*c;


if (d > 0) {

x1 = (-b + sqrt(d)) / (2*a);

x2 = (-b - sqrt(d)) / (2*a);

cout << "Roots are real and different." <<
endl;

cout << "x1 = " << x1 << endl;

cout << "x2 = " << x2 << endl;

}


else if (d == 0) {

cout << "Roots are real and same." <<
endl;












x1 = (-b + sqrt(d)) / (2*a);
cout << "x1 = x2 =" << x1 << endl;
}
else {
real = -b/(2*a);
imaginary =sqrt(-d)/(2*a);
cout << "Roots are complex and different."
<< endl;
cout << "x1 = " << real << "+" << imaginary
<< "i" << endl;
cout << "x2 = " << real << "-" << imaginary
<< "i" << endl;
}
getch();
}
 13.Write a program c++ to find the area of a
circle;

 #include <iostream>
 #include <conio.h>
 void main()
 {
 float radius, area;
 cout << "Enter the radius of circle: ";
 cin >> radius;

 area = 3.14 * radius * radius;
 cout << "Area of circle: " << area << endl;

 getch();
 }
Download