Você está na página 1de 30

C++ Program Structure:

Let us look at a simple code that would print the words Hello World.
#include <iostream>
// main() is where program execution begins.
int main()

cout << !Hello World!" // prints Hello World


return #"
$
Comments in C++
%&& supports single line and multi'line comments. (ll characters a)ailable inside an* comment are ignored b*
%&& compiler.
%&& comments start with /+ and end with +/.
,or example-
// .his is a single line comment
/+ %&& comments can also
+ span multiple lines
+/
#include <iostream>
using namespace std"
main()

/+ %&& comments
.his /rogram prints Hello World
+/
cout << !Hello World!" // prints Hello World
return #"
$
Data Types in C++
.hree t*pes-
0uilt in data t*pe
1eri)ed data t*pe
2ser'de3ined data t*pe
1ata t*pe is one o3 the 3undamental properties o3 a )ariable.
1. Built in Data Types:
4. char a single b*te that can hold one character.
5. int an integer
6. 3loat a single precision 3loating point number.
7. double a double precision 3loating point number.
Qualifiers:-
short8 long8 signed8 unsigned are called 9uali3iers.
i. :hort8 long si;e 9uali3iers
ii. :igned8 unsigned sign 9uali3iers
:i;e 9uali3iers can not be applied to char8 3loat8 data t*pes.
:ign 9uali3iers can not be applied to 3loat8 double < long double.
1
=ame 1escription :i;e+ >ange+
char
%haracter or small integer. ? b*te
signed- '?4@ to ?4A
unsigned- # to 477
character- a to ; ( to B < :pecial
%haracters
short int
:hort Cnteger. 4 b*tes
signed- '54AD@ to 54ADA
unsigned- # to D7757
int
Cnteger. 6 b*tes
signed- '4?6A6@5D6@ to 4?6A6@5D6A
unsigned- # to 64E6EDA4E7
long int
Long integer. 6 b*tes
signed- '4?6A6@5D6@ to 4?6A6@5D6A
unsigned- # to 64E6EDA4E7
bool
0oolean )alue. Ct can take one
o3 two )alues- true or 3alse.
? b*te true or 3alse
float
,loating point number. 6 b*tes &/' 5.6e &/' 5@ (FA digits)
double
1ouble precision 3loating
point number.
@ b*tes &/' ?.Ae &/' 5#@ (F?7 digits)
long
double
Long double precision
3loating point number.
@ b*tes &/' ?.Ae &/' 5#@ (F?7 digits)
2.Derive Datatypes:
(rra*s
/ointers
,unction
>e3erence
!."ser-Define Data Types:
:truct
2nion
%lass
Gnumeration
%&& Hperators-
(n operator is a s*mbol that tells the compiler to per3orm speci3ic mathematical or logical manipulations.
%&& is rich in built'in operators and pro)ides 3ollowing t*pe o3 operators-
(rithmetic Hperators ( &8 '8 I8 +8 &&8 '')
>elational Hperators (JJ8 KJ8 >. <8 >J8 <J)
Logical Hperators (<<8 LL8 K )
0itwise Hperators (< L8 M8 F8 <<8 >>)
(ssignment Hperators (J8 &J8 'J8 +J8 /J8 NJ8 <<J8 >>J8 <J8 MJ8 LJ)
Oisc Hperators ( si;eo38 < cast8 comma8 conditional etc.)
Types of operators:
?. (ssignment operators
4. (rithmetic operators
5. >elational operators
6. Logical operators
7. 0itwise operators
D. %ompound (ssignment Hperators
A. Cncrement < decrement operators
2
@. %onditional operators
E. :pecial operator
1.#ssignment $perators %&'
Peneral s*ntax- varia(le&e)pression*
2.#rit+metic $perators:-
Operator Meaning
& (ddition or unar* plus.
' :ubtraction or unar* minus.
+ Oultiplication.
/ 1i)ision.
N Oodulo di)ision.
!.,elational $perators:-
Ct is used to make comparisons between two expressions. (ll these operators are binar* and re9uire two
operands.
Operator Meaning
< Less than
> Preater than
<J Less than e9ual to
>J Preater than e9ual to
J J G9ual to
KJ =ot e9ual to
-..ogical $perators:
Logical operators are use3ul in combining one or more conditions. (n* expression that
e)aluates to ;ero denotes a ,(L:G condition and that e)aluating to non';ero )alue denotes a .>2G condition.
Operator Meaning Type
<< Logical (=1 0inar*
L L Logical H>
K Logical =H. 2nar*
/.Bit0ise $perator:
Operator Meaning
< 0itwise (=1
L 0itwise H>
M 0itwise GQ'H>
F 0itwise complement
<< :hi3t le3t
>> :hi3t right
1.Compoun #ssignment $perators:

!
Operator Usage Effect
& J a & J exp" a J a & (exp)"
' J a ' J exp" a J a ' (exp)"
+ J a + J exp" a J a + (exp)"
/ J a / J exp" a J a / (exp)"
N J a N J exp" a J a N (exp)"
2. 3ncrement an Decrement $perator:-
Operator Syntax
&&
)ariable name &&"
&&)ariable name"
''
)ariable name''"
'' )ariable name"
@.Conitional $perator%T4,5#,6 $P4,#T$,' 7:
Peneral s*ntax- e)p17 True : 8alse
2.Special $perators:
:i;eo3 J returns the si;e o3 data t*pe or )ariable in terms o3 b*tes occupied in memor*.
delete J Oemor* release operator
new J Oemor* allocation operator
+ J Cndirection operator.
< J address operator.
8 J comma operator.
-- J :cope >esolution operator
C++ 3entifiers:
( %&& identi3ier is a name used to identi3* a )ariable8 3unction8 class8 module8 or an* other user'de3ined item.
(n identi3ier starts with a letter ( to B or a to ; or an underscore (R) 3ollowed b* ;ero or more letters8
underscores8 and digits (# to E).
%&& does not allow punctuation characters such as S8 T8 and N within identi3iers. %&& is a case'sensiti)e
programming language. .hus8 Oanpower and manpower are two di33erent identi3iers in %&&.
C++ 9ey0ors:
.he 3ollowing list shows the reser)ed words in %&&. .hese reser)ed words ma* not be used as constant or
)ariable or an* other identi3ier names.
(sm else new this
(uto enum operator throw
0ool explicit pri)ate true
0reak export protected tr*
%ase extern public t*pede3
%atch 3alse register t*peid
%har 3loat reinterpretRcast t*pename
-
%lass 3or return union
%onst 3riend short unsigned
constRcast goto signed using
continue C3 si;eo3 )irtual
1e3ault inline :tatic )oid
1elete int staticRcast )olatile
1o long :truct wcharRt
1ouble mutable :witch while
d*namicRcast namespace .emplate
C$5T,$. ST#T4:45T:
Conitional Statement
.ooping Statement
Brea;ing Statement
C$5D3T3$5#. ST#T4:45T:
C3 :tatement
C3Uelse :tatement
:witch case :tatement
S65T#<:
if(condition)

//C3 condition true execute this


statement
$
if(condition)

//.rue statement
$
else

//,alse statement
$
s0itc+()ariable)

case ()alue?)-

$
(rea;*
case ()alue4)-

$
(rea;*
efault:
(rea;*
$

.$$P35= ST#T4:45T
for loop
0+ile loop
o 0+ile loop
S65T#<:
for (intialise"condition"&&/'')

// :tatement
$
0+ile (condition)

// :tatement
$
o

// :tatement
$0+ile(condition)"
Brea; statement-
( break construct terminates the execution o3 loop and the control is trans3erred to the statement
immediatel* 3ollowing to the loop.
.he term VbreakW re3ers to the act o3 breaking out o3 a block o3 code.
/
,ig-break statements in loops"
3or(initiali;e"exp?"exp4)
RRRRRRR"
i3(condition) .rue
break"
RRRRRRRRR"
$
statement"
8unction:
8unction Prototyping:
returnt*pe 3unctionname(argumentlist)"
Gx-
int add(int a8 intb)"
8unction Definition:
returnt*pe 3unctionname(argumentlist)

// :tatements
$
Gx-
int add(int a8 int b)

return(a&b)"
$
8unction Call:
3unctionname(argument)alue)"
Gx-
add(486)"
Call (y >alue
int add(int a8int b)" // %all b* Xalue
Call (y ,eferrence
int add(int <a8 int <b)" // %all b* >e3errence
int add(int +a8 int +b)" // %all b* >e3errence
C.#SS4S #5D $B?4CTS
Class Declaration
class classname

pri)ate-
//1ata member
//Oember ,unction
public-
//1ata member
//Oember ,unction
$"
$(@ect Declaration
:et+o 1:
1
Xoid main()

classname obY?8obY4"
$
:et+o 2:
class classname

//1ata member
//Oember ,unction
$obY?8obY4"
#ccessing Class :em(ers
,or 1ata member
HbYectname.datamember"
,or Oember ,unction
HbYectname.member3unction(argumentlist)"
Defining :em(er 8unction
3nsie t+e Class
class classname

returnt*pe 3unctionname(argumentlist)

// :tatements
$
$"
$utsie t+e Class
class classname

returntype functionname%argumentlist'*
$"
returnt*pe classname::3unctionname(argumentlist)

// :tatements
$
Pointers
Declaration: atatype ApointerBvaria(le*
,etrieve aress: pointerBvaria(le & C varia(le*
C++ P,$=,#:
?. %&& />HP>(O .H H2./2. (= C=.GPG>8 ( ,LH(.C=P /HC=. =2O0G> (=1 (
%H(>(%.G>
#include <iostream.h>
#include <conio.h>
)oid main()

2
clrscr()"
int x J ?#"
3loat * J ?#.?"
char ; J ZaZ"
cout << !x J ! << x << endl"
cout << !* J ! << * << endl"
cout << !; J ! << ; << endl"
getch()"
$
2. C++ PROGRAM TO FIND THE SUM, DIFFERENCE, PRODUCT AND QUOTIENT OF
TWO INTEGERS
#include <iostream.h>
#include <conio.h>
)oid main()

clrscr()"
int x J ?#"
int * J 4"
int sum8 di33erence8 product8 9uotient"
sum J x & *"
di33erence J x ' *"
product J x + *"
9uotient J x / *"
cout << !.he sum o3 ! << x << ! < ! << * << ! is ! << sum << !.! << endl"
cout << !.he di33erence o3 ! << x << ! < ! << !* << is ! << di33erence << !.! << endl"
cout << !.he product o3 ! << x << ! < ! << * << ! is ! << product << !.! << endl"
cout << !.he 9uotient o3 ! << x << ! < ! << * << ! is ! << 9uotient << !.! << endl"
getch()"
$
3. C++ PROGRAM TO ENTER TWO INTEGERS AND FIND THEIR SUM AND AVERAGE
#include <iostream.h>
#include <iostream.h>
#include <conio.h>
)oid main()

clrscr()"
int x8*8sum"
3loat a)erage"
cout << !Gnter 4 integers - ! << endl"
cin>>x>>*"
sumJx&*"
a)erageJsum/4"
cout << !.he sum o3 ! << x << ! and ! << * << ! is ! << sum << !.! << endl"
cout << !.he a)erage o3 ! << x << ! and ! << * << ! is ! << a)erage << !.! << endl"
getch()"
D
$
4. PROGRAM TO ENTER YOUR AGE AND PRINT IF YOU SHOULD BE IN GRADE 10
#include <iostream.h>
#include <conio.h>
)oid main()

clrscr()"
int age"
cout << !Gnter *our present age - ! << endl"
cin>>age"
i3(ageJJ?D)

cout << ![our present age is ! << age << ! *ears.! << endl"
cout << ![ou are o3 the right age 3or Yoining grade ?# K! << endl"
$
else

cout << ![our present age is ! << age << ! *ears.! << endl"
cout << ![ou are not o3 the right age 3or Yoining grade ?# K! << endl"
$
getch()"
$
. PROGRAM TO ENTER AN INTEGER AND PRINT IF IT IS GREATER OR LESS THAN
100
#include <iostream.h>
#include <conio.h>
)oid main()
clrscr()"
int x"
cout << !Gnter an integer - ! << endl"
cin>>x"
i3(x>?##)

cout << x << ! is greater than ?##.! << endl"


$
else

cout << x << ! is less than ?##.! << endl"


$
getch()"
$
!. PROGRAM TO SWITCH BETWEEN DIFFERENT CASES
#include <iostream.h>
#include <conio.h>
int main()

clrscr()"
E
int choice"
cout << !?. .alk! << endl"
cout << !4. Gat! << endl"
cout << !5. /la*! << endl"
cout << !6. :leep! << endl"
cout << !Gnter *our choice - ! << endl"
cin>>choice"
switch(choice)

case ? - cout << ![ou chose to talk...talking too much is a bad habit.! << endl"
break"
case 4 - cout << ![ou chose to eat...eating health* 3oodstu33 is good.! << endl"
break"
case 5 - cout << ![ou chose to pla*...pla*ing too much e)er*da* is bad.! << endl"
break"
case 6 - cout << ![ou chose to sleep...sleeping enough is a good habit.! << endl"
break"
de3ault - cout << ![ou did not choose an*thing...so exit this program.! << endl"
$
getch()"
$
". PROGRAM TO ENTER AN INTEGER AND PRINT IF IT IS PRIME OR COMPOSITE
#include <iostream.h>
#include <conio.h>
#include <process.h>
)oid main()

clrscr()"
int num?8x"
cout << !Gnter an integer - ! << endl"
cin>>num?"
3or(xJ4"x<num?"x&&)

i3(num?NxJJ#)

cout << num? << ! is a composite number.! << endl"


getch()"
exit(#)"
$
else

cout << num? << ! is a prime number.! << endl"


getch()"
exit(#)"
$
$
$
#. PROGRAM TO ENTER AN INTEGER AND OUTPUT THE CUBE OF THAT INTEGER
#include <iostream.h>
#include <conio.h>
1F
int cube(int x)" //.he protot*pe.
)oid main()

int a"
cout << !Gnter an integer - !"
cin>>a"
cout << !.he cube o3 ! << a << ! is - ! << cube(a) << endl" //%all the 3unction cube(a).
getch()"
$
int cube(int x) //1e3ining the 3unction.

int *"
*Jx+x+x"
return(*)"
$
$. PROGRAM TO ENTER TWO INTEGERS AND PRINT THE QUOTIENT AND
REMAINDER
#include <iostream.h>
#include <conio.h>
int main()

clrscr()"
int x8*89uotient8remainder"
cout << !Gnter 4 integers greater than # - !"
cin>>x>>*"
9uotientJx/*"
remainderJx'(9uotient+*)"
cout << !\uotient o3 ! << x << ! < ! << * << ! J ! << 9uotient << !In!"
cout << !>emainder! << ! J ! << remainder << !In!"
getch()"
return #"
$
10. PROGRAM TO ENTER THREE INTEGERS AND OUTPUT THE BIGGEST INTEGER
#include <iostream.h>
#include <conio.h>
int main()

clrscr()"
int x8*8;8biggest"
cout << !Gnter 5 integers - !"
cin>>x>>*>>;"
biggestJx>*](x>;]x-;)-(*>;]*-;)"
cout << !.he biggest integer out o3 the 5 integers *ou t*ped !"
cout << x << !8 ! << * << ! < ! << ; << ! is - ! << !In! << biggest << !In!"
getch()"
return #"
$
11. PROGRAM TO ENTER THREE INTEGERS AND OUTPUT THE BIGGEST INTEGER
USING IF
#include <iostream.h>
11
#include <conio.h>
int main()

clrscr()"
int x8*8;8biggest"
cout << !Gnter 5 integers - !"
cin>>x>>*>>;"
biggestJx"
i3(*>biggest)
biggestJ*"
i3(;>biggest)
biggestJ;"
cout << !.he biggest integer out o3 the 5 integers *ou t*ped !"
cout << x << !8 ! << * << ! < ! << ; << ! is - ! << !In! << biggest << !In!"
getch()"
return #"
$
12. PROGRAM TO FIND THE ROOTS OF A QUADRATIC EQUATION
#include <iostream.h>
#include <conio.h>
#include <math.h>
int main()

clrscr()"
3loat a8b8c8d8root?8root4"
cout << !Gnter the 5 coe33icients a8 b8 c - ! << endl"
cin>>a>>b>>c"
i3(Ka)
i3(Kb)
cout << !0oth a and b cannot be # in axM4 & bx & c J #! << !In!"
else

dJ'c/b"
cout << !.he solution o3 the linear e9uation is - ! << d << endl"
$
$
else

dJb+b'6+a+c"
i3(d>#)
root?J('b&s9rt(d))/(4+a)"
root4J('b's9rt(d))/(4+a)"
cout << !.he 3irst root J ! << root? << endl"
cout << !.he second root J ! << root4 << endl"
$
getch()"
return #"
$
13. PROGRAM TO ENTER AN INTEGER AND OUTPUT IT IN THE REVERSED FORM
12
#include <iostream.h>
#include <conio.h>
)oid main()

clrscr()"
long int num?8num48rnumJ#"
cout << !Gnter an integer - ! << endl"
cin>>num?"
num4Jnum?"
do

rnumJrnum+?#"
int digitJnum?N?#"
rnum&Jdigit"
num?/J?#"
$
while(num?)"
cout << !.he integer *ou t*ped is ! << num4 << !.! << endl"
cout << !.he re)ersed integer is ! << rnum << !.! << endl"
getch()"
$
14. PROGRAM TO CONVERT DAYS INTO YEARS AND WEE%S
#include <iostream.h>
#include <conio.h>
)oid main()

clrscr()"
int da*s8*ears8weeks8num?"
cout << !Gnter the number o3 da*s - ! << endl"
cin>>da*s"
*earsJda*s/5D7"
num?Jda*s'(*ears+5D7)"
weeksJda*s/A"
num?Jda*s'(weeks+A)"
cout << da*s << ! da*s J ! << endl"
cout << weeks << ! weeks H> ! << endl"
cout << *ears << ! *ears.! << endl"
getch()"
$
1. PROGRAM TO COMPUTE THE FIBONACCI SERIES
#include <iostream.h>
#include <conio.h>
)oid main()

clrscr()"
int a8b8x8*8num?8ct"
aJ#"
bJ?"
1!
cout << !Gnter the number o3 terms (less than 47) - ! << endl"
cin>>num?"
cout << a << endl"
cout << b << endl"
3or(ctJ?"ct<Jnum?'4"ct&&)

xJa&b"
cout << x << endl"
*Ja"
aJb"
bJx"
$
getch()"
$
1!. PROGRAM TO ENTER A CHARACTER AND OUTPUT ITS ASCII CODE
#include <iostream.h>
#include <conio.h>
)oid main()

clrscr()"
char charac"
cout << !Gnter the character - ! << endl"
cin>>charac"
int num?Jcharac"
cout << !.he (:%CC code 3or ! << charac << ! is ! << num? << !.! << endl"
getch()"
$
1". PROGRAM TO PRINT THE FIRST 10 LINES OF PASCAL&S TRIANGLE
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
long triangle(int x8int *)"
int main()

clrscr()"
const linesJ?#"
3or (int iJ#"i<lines"i&&)
3or (int YJ?"Y<lines'i"Y&&)
cout << setw(4) << ! !"
3or (int YJ#"Y<Ji"Y&&)
cout << setw(6) << triangle(i8Y)"
cout << endl"
getch()"
$
long triangle(int x8int *)

1-
i3(x<#LL*<#LL*>x)
return #"
long cJ?"
3or (int iJ?"i<J*"i&&8x'')
cJc+x/i"
return c"
$
1#. PROGRAM TO CONVERT TEMPERATURES FROM CELSIUS TO FAHRENHEIT AND
VICE VERSA
#include <iostream.h>
#include <conio.h>
)oid main()

clrscr()"
int choice"
3loat ctemp83temp"
cout << !?.%elsius to ,ahrenheit! << endl"
cout << !4.,ahrenheit to %elsius! << endl"
cout << !%hoose between ? < 4 - ! << endl"
cin>>choice"
i3 (choiceJJ?)

cout << !Gnter the temperature in %elsius - ! << endl"


cin>>ctemp"
3tempJ(?.@+ctemp)&54"
cout << !.emperature in ,ahrenheit J ! << 3temp << endl"
$
else

cout << !Gnter the temperature in ,ahrenheit - ! << endl"


cin>>3temp"
ctempJ(3temp'54)/?.@"
cout << !.emperature in %elsius J ! << ctemp << endl"
$
getch()"
$
1$. PROGRAM TO FIND THE SUM OF EITHER OF THE DIAGONALS OF A 4 ' 4 MATRI'
#include <iostream.h>
#include <conio.h>
)oid main()

clrscr()"
int x"
int (^6_^6_8sumJ#" //>eading the matrix.
cout << !Gnter the elements o3 the matrix - ! << endl"
3or(int *J#"*<6"*&&)
3or (int xJ#"x<6"x&&)

cout << !Glement ! << x&? << !8 ! << *&? << ! - !"
cin>>(^x_^*_"
1/
$
//:um o3 either o3 the diagonal elements.
3or(xJ#"x<6"x&&)
3or(*J#"*<6"*&&)
i3(xJJ*)
sum&J(^x_^*_"
else i3(*JJ6'(?&?))"
sum&J(^x_^*_"
cout << !:um o3 either o3 the diagonal elements is - ! << sum"
getch()"
$
20. PROGRAM TO ENTER THREE INTEGERS AND OUTPUT THE SMALLEST INTEGER
USING IF
#include <iostream.h>
#include <conio.h>
int main()

clrscr()"
int x8*8;8smallest"
cout << !Gnter 5 integers - !"
cin>>x>>*>>;"
smallestJx"
i3(*<smallest)
smallestJ*"
i3(;<smallest)
smallestJ;"
cout << !.he smallest integer out o3 the 5 integers *ou t*ped !"
cout << x << !8 ! << * << ! < ! << ; << ! is - ! << !In! << smallest << !In!"
getch()"
return #"
$
4?. />HP>(O .H ,C=1 :2O H, GLGOG=.: H, (>>([ 2:C=P ,2=%.CH=
#include<iostream.h>
#include<conio.h>
)oid sum(int a^_8int n)"
)oid main()

clrscr()"
int n8a^?#_"
cout<<!enter the number o3 elements in arra*!"
cin>>n"
cout<<!enter )alue o3 elements o3 arra*!<<!In!"
3or(int iJ#"i<n"i&&)

cin>>a^i_"
$
cout<<!entered )alue o3 arra* element is!<<!In!"
3or(iJ#"i<n"i&&)

cout<<!a^!<<i<<!_J!<<a^i_<<!In!"
11
$
sum(a8n)"
getch()"
$
)oid sum(int a^_8int n)

int sumJ#"
3or(int iJ#"i<n"i&&)

sumJsum&a^i_"
$
cout<<!sum o3 elements J!<<sum"
$
44. />HP>(O .H %(L%2L(.G (XG>(PG H, ZnZ GLGOG=.: 2:C=P (>>([
#include<iostream.h>
#include<conio.h>
)oid main()

clrscr()"
int i8n8sumJ#8a^4#_"
3loat a)g"
cout<<!enter number o3 elements in arra*!"
cin>>n"
cout<<!enter )alue o3 elements o3 arra*!<<!In!"
3or(iJ#"i<n"i&&)

cin>>a^i_"
$
cout<<!sum o3 elements o3 arra* are!<<!In!"
3or(iJ#"i<n"i&&)

sumJsum&a^i_"
$
cout<<!sum J!<<sum<<!In!"
a)gJsum/n"
cout<<!a)erage J!<<a)g"
getch()"
$
45. />HP>(O .H ,C=1 .HG .>(=:/H:G H, PCXG= O(.>CQ
#include<iostream.h>
#include<conio.h>
)oid main()

clrscr()"
int i8Y8m8n8a^?#_^?#_8b^?#_^?#_"
cout<<!enter the number o3 rows and column o3 matrix!<<!In!"
cin>>m>>n"
cout<<!enter the )alue o3 elements o3 matrix!"
3or(iJ#"i<m"i&&)

12
3or(YJ#"Y<n"Y&&)

cin>>a^i_^Y_"
$
$
cout<<!entered matrix is!<<!In!"
3or(iJ#"i<m"i&&)

cout<<!In!"
3or(YJ#"Y<n"Y&&)

cout<<a^i_^Y_<<!It!"
$
$
cout<<!In transpose o3 matrix is!<<!In!"
3or(iJ#"i<n"i&&)

3or(YJ#"Y<m"Y&&)

b^i_^Y_Ja^Y_^i_"
$
$
3or(iJ#"i<n"i&&)

cout<<!In!"
3or(YJ#"Y<m"Y&&)

cout<<b^i_^Y_<<!It!"
$
$
getch()"
$
46. />HP>(O H, ,2=%.CH= WC.H 1G,(2L. (>P2OG=.:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
)oid )olume(int lJ?#8int wJ?#8int hJ?#)"
)oid main()

clrscr()"
)olume()" //e9ui)alent to (?#8?#8?#)//
)olume(7)" //e9ui)alent to (78?#8?#)//
)olume(78D)" //e9ui)alent to (78D8?#)//
)olume(D8687)" //e9ui)alent to(D8687)//
getch()"
$
)oid )olume(int l8int w8int h)

cout<<!)olume is!<<l+w+h<<!In!"
$
1D
47. />HP>(O .H %(L%2L(.G .HG :\2(>G>HH. H, .HG PCXG= =2O0G>
#include<iostream.h>
#include<conio.h>
#include<math.h>
)oid main()

clrscr()"
int a"
3loat s9uareroot8cuberoot"
cout<<!enter the number!"
cin>>a"
cout<<!In!<<!s9uare root o3 the number is! "
s9uarerootJs9rt(a)"
cout<<s9uareroot"
getch()"
$
4D. />HP>(O .H ,C=1 .HG :2O 2:C=P ,2=%.CH=
#include<iostream.h>
#include<conio.h>
)oid main()

clrscr()"
int a8b"
)oid sum(int8int)"
cout<<!enter two numbers!"
cin>>a>>b"
sum(a8b)"
getch()"
$
)oid sum(int x8int *)

int ;"
;Jx&*"
cout<<!sumJ!<<;"
$
4A. />HP>(O ,H> :WC.%H :.(.GOG=.
#include<iostream.h>
#include<conio.h>
)oid main()

clrscr()"
int a8b8c"
cout<<!enter the )alue o3 two numbers!"
cin>>a>>b"
cout<<!?.addition!<<!In!"
cout<<!4.multiplication!<<!In!"
cout<<!5.di)ision!<<!In!"
cout<<!6.subtraction!<<!In!"
int choice"
cout<<!enter *our choice!"
1E
cin>>choice"
switch(choice)

case ?-cJa&b"
cout<<!sum is!<<c"
break"
case 4-cJa+b"
cout<<!multiplication is!<<c"
break"
case 5-cJa/b"
cout<<!di)ision is!<<c"
break"
case 6-cJa'b"
cout<<!subtraction is!<<c"
break"
de3ault-cout<<!in)alid choice-!"
$
getch()"
$
4@. />HP>(O .H %(L%2L(.G :\2(>G (=1 %20G H, ( =2O0G>
#include<iostream.h>
#include<conio.h>
)oid main()

clrscr()"
int n8i8s9uare8cube"
cout<<!enter the )alue o3 n!"
cin>>n"
cout<<!cube and s9uare o3 number is!<<!In!"
3or(iJ?"i<Jn"i&&)

s9uareJi+i"
cubeJi+i+i"
cout<<!ItIt!<<i<<!ItIt!<<s9uare<<!ItIt!<<cube<<!In!"
$
getch()"
$
4E. />HP>(O .H %(L%2L(.G .HG :CO/LG C=.G>G:.
#include<iostream.h>
#include<conio.h>
)oid main()

clrscr()"
3loat p8r8t8si"
cout<<!enter principle8rate8interest!"
cin>>p>>r>>t"
siJ(p+r+t)/?##"
cout<<!simple interest is!<<si"
getch()"
$
2F
5#. />HP>(O .H ,C=1 .HG :2O H, .WH =2O0G>
#include<iostream.h>
#include<conio.h>
)oid main()

int a8b8c"
cout<<!enter the )alue o3 two numbers!"
cin>>a>>b"
cJa&b"
cout<<!sum o3 two number is!<<c"
getch()"
$
5?. />HP>(O .H O2L.C/L[ .WH O(.>C%G:
#include<iostream.h>
#include<conio.h>
)oid main()

clrscr()"
int i8Y8m8n8p898a^?#_^?#_8b^?#_^?#_8c^?#_^?#_"
cout<<!enter row and column o3 matrix (!"
cin>>m>>n"
cout<<!In enter the row and column o3 matrix 0!"
cin>>p>>9"
i3(nKJp)

cout<<!multiplication not possible!"


$
else

cout<<!enter )alue o3 elements o3 matrix (!<<!In!"


3or(iJ#"i<m"i&&)

3or(YJ#"Y<n"Y&&)

cin>>a^i_^Y_"
$
$
cout<<!enter )alues o3 elements o3 matrix 0!<<!In!"
3or(iJ#"i<p"i&&)

3or(YJ#"Y<9"Y&&)

cin>>b^i_^Y_"
$
$
cout<<!)alues o3 elements o3 matrix ( are!<<!In!"
3or(iJ#"i<m"i&&)

3or(YJ#"Y<n"Y&&)

21
cout<<!It!"
cout<<a^i_^Y_"
$
cout<<!In!"
$
cout<<!)alues o3 elements o3 matrix 0 are!<<!In!"
3or(iJ#"i<p"i&&)

3or(YJ#"Y<9"Y&&)

cout<<!It!"
cout<<b^i_^Y_"
$
cout<<!In!"
$
cout<<!multiplication o3 two matrix is-!"
3or(iJ#"i<m"i&&)

cout<<!In!"
3or(YJ#"Y<9"Y&&)

c^i_^Y_J#"
3or(int kJ#"k<p"k&&)

c^i_^Y_Jc^i_^Y_&a^i_^k_+b^k_^Y_"
$
cout<<c^i_^Y_<<!It!"
$
$
$
getch()"
$
54. />HP>(O .H :20.>(%. .WH O(.>CQ
#include<iostream.h>
#include<conio.h>
)oid main()

clrscr()"
int i8Y8n8a^?#_^?#_8b^?#_^?#_8c^?#_^?#_"
cout<<!enter the order o3 matrix (!"
cin>>n"
cout<<!enter )alue o3 elements o3 matrix (!<<!In!"
3or(iJ#"i<n"i&&)

3or(YJ#"Y<n"Y&&)

cin>>a^i_^Y_"
$
$
cout<<!enter the order o3 matrix 0!"
22
cin>>n"
cout<<!enter )alues o3 elements o3 matrix 0!<<!In!"
3or(iJ#"i<n"i&&)

3or(YJ#"Y<n"Y&&)

cin>>b^i_^Y_"
$
$
cout<<!)alues o3 elements o3 matrix ( are!<<!In!"
3or(iJ#"i<n"i&&)

3or(YJ#"Y<n"Y&&)

cout<<!It!"
cout<<a^i_^Y_"
$
cout<<!In!"
$
cout<<!)alues o3 elements o3 matrix 0 are!<<!In!"
3or(iJ#"i<n"i&&)

3or(YJ#"Y<n"Y&&)

cout<<!It!"
cout<<b^i_^Y_"
$
cout<<!In!"
$
cout<<!subtraction o3 matrix ( and 0 is!<<!In!"
3or(iJ#"i<n"i&&)

cout<<!In!"
3or(YJ#"Y<n"Y&&)

c^i_^Y_Ja^i_^Y_'b^i_^Y_"
cout<<c^i_^Y_<<!It!"
$
cout<<!In!"
$
getch()"
$
55. />HP>(O .H (11 .WH O(.>CQ
#include<iostream.h>
#include<conio.h>
)oid main()

clrscr()"
int i8Y8n8a^?#_^?#_8b^?#_^?#_8c^?#_^?#_"
cout<<!enter the order o3 matrix (!"
2!
cin>>n"
cout<<!enter )alue o3 elements o3 matrix (!<<!In!"
3or(iJ#"i<n"i&&)

3or(YJ#"Y<n"Y&&)

cin>>a^i_^Y_"
$
$
cout<<!enter the order o3 matrix 0!"
cin>>n"
cout<<!enter )alues o3 elements o3 matrix 0!<<!In!"
3or(iJ#"i<n"i&&)

3or(YJ#"Y<n"Y&&)

cin>>b^i_^Y_"
$
$
cout<<!)alues o3 elements o3 matrix ( are!<<!In!"
3or(iJ#"i<n"i&&)

3or(YJ#"Y<n"Y&&)

cout<<!It!"
cout<<a^i_^Y_"
$
cout<<!In!"
$
cout<<!)alues o3 elements o3 matrix 0 are!<<!In!"
3or(iJ#"i<n"i&&)

3or(YJ#"Y<n"Y&&)

cout<<!It!"
cout<<b^i_^Y_"
$
cout<<!In!"
$
cout<<!addition o3 matrix ( and 0 is!<<!In!"
3or(iJ#"i<n"i&&)

cout<<!In!"
3or(YJ#"Y<n"Y&&)

c^i_^Y_Ja^i_^Y_&b^i_^Y_"
cout<<c^i_^Y_<<!It!"
$
cout<<!In!"
$
2-
getch()"
$
56. />HP>(O .H %HG%` WHG.HG> .HG G=.G>G1 =2O0G> C: GXG= H> H11
#include<iostream.h>
#include<conio.h>
)oid main()

clrscr()"
int n"
cout<<!enter an* number!"
cin>>n"
i3(nN4JJ#)

cout<<!number is e)en!"
$
else

cout<<!number is odd!"
$
getch()"
$
57. />HP>(O .H %HG%` WHG.HG> .HG PCXG= =2O0G> C: (>O:.>H=P H> =H.
#include<iostream.h>
#include<conio.h>
)oid main()

clrscr()"
int i8n8m8rem8sumJ#"
cout<<!enter the re9uired number!"
cin>>n"
mJn"
while(m>#)

remJmN?#"
mJm/?#"
sumJsum&rem+rem+rem"
$
i3(sumJJn)

cout<<!number is armstrong!"
$
else

cout<<!number is not armstrong!"


$
getch()"
$
5D. />COG =2O0G> />HP>(O
#include<iostream.h>
#include<conio.h>
2/
#include<process.h>
)oid main()

clrscr()"
int n"
char ch"
do

cout<<!enter the number!"


cin>>n"
3or (int iJ4"i<Jn/4"i&&)

i3(nNiJJ#)

cout<<!no is not prime!"


goto start"
$
$
cout<<!no is prime!"
start-
cout<<!u waana 3urther continue([/*)!"
cin>>ch"
$
while(chJJZ*ZLLchJJZ[Z)"
getch()"
$
5A. />HP>(O .H %HG%` WHG.HG> .HG G=.G>G1 =2O0G> C: /(LC=1>HOG H> =H.
#include<iostream.h>
#include<conio.h>
)oid main()

clrscr()"
int n8m8i8rem8re)J#"
cout<<!enter the re9uired number!"
cin>>n"
mJn"
while(m>#)

remJmN?#"
mJm/?#"
re)Jre)+?#&rem"
$
cout<<!re)erse o3 number is!<<re)<<!In!"
i3(re)JJn)

cout<<!number is palindrome!"
$
else

cout<<!number is not palindrome!"


21
$
getch()"
$
5@. />HP>(O .H ,C=1 LG(/ [G(>
#include<iostream.h>
#include<conio.h>
)oid main()

int n"
cout<<!enter the *ear!"
cin>>n"
i3(nN6JJ#)

cout<<!entered *ear is leap *ear!"


$
else

cout<<!the entered *ear is not leap *ear!"


$
getch()"
$
5E. />HP>(O .H C=.G>%H(=PG .WH =2O0G>:
#include<iostream.h>
#include<conio.h>
)oid main()

clrscr()"
int a8b"
cout<<!enter the )alue o3 a and b!"
cin>>a>>b"
aJa&b"
bJa'b"
aJa'b"
cout<<!In!<<!a3ter interchanging )alue o3!<<!aJ!<<a<<!In!<<!bJ!<<b"
getch()"
$
6#. P>G(.G> 0G.WGG= .H>GG =2O0G>: 2:C=P .G>=(>[ H/G>(.H>
#include<iostream.h>
#include<conio.h>
)oid main()

clrscr()"
int a8b8c8d"
cout<<!enter three no.!"
cin>>a>>b>>c"
dJ((a>b<<a>c)]a-(b>a<<b>c)]b-c)"
cout<<!greater no is!<<d"
getch()"
$
6?. ,C=1: .HG ,(%.H>C(L H, .HG PCXG= =2O0G>
22
#include<iostream.h>
#include<conio.h>
)oid main()

clrscr()"
int i8n83actJ?"
cout<<!enter the no. whose 3actorial is to be 3ind!"
cin>>n"
3or(iJ?"i<Jn"i&&)

3actJ3act+i"
$
cout<<!3actorial o3 number is!<<3act"
getch()"
$
64. />HP>(O H, ,2=%.CH= HXG>LH(1C=P
#include<iostream.h>
#include<conio.h>
#include<math.h>
)oid area(int)"
)oid area(int8int)"
)oid area(int8int8int)"
)oid main()

clrscr()"
3loat a8b8c8le8br8side"
cout<<!enter the side o3 s9uare!"
cin>>side"
cout<<!enter length and breadth o3 rectangle!"
cin>>le>>br"
cout<<!enter parameter a8b8c o3 traingle!"
cin>>a>>b>>c"
area(side)"
area(le8br)"
area(a8b8c)"
getch()"
$
)oid area(int x)

int a"
aJx+x"
cout<<!area o3 s9uare is!<<a<<!In!"
$
)oid area(int x8int *)

int b"
bJx+*"
cout<<!area o3 rectangle is!<<b<<!In!"
$
)oid area(int x8int *8int ;)
2D

3loat sJ(x&*&;)/4"
3loat ar"
arJs9rt(s+(s'x)+(s'*)+(s';))"
cout<<!area o3 triangle using HG>H 3ormula is!<<ar"
$
65. />HP>(O .H ,C=1 .HG ,(%.H>C(L 2:C=P .HG ,2=%.CH=
#include<iostream.h>
#include<conio.h>
)oid main()

clrscr()"
int 3act(int)"
int n8i8k"
cout<<!enter an* number!"
cin>>n"
kJ3act(n)"
cout<<!3actorial o3 number isJ !<<k"
getch()"
$
int 3act(int x)

int 3actJ?"
i3(xJJ#LLxJJ?)

cout<<!3actorial o3 number is!<<3act"


$
else

3or(int iJ4"i<Jx"i&&)

3actJ3act+i"
$
$
return(3act)"
$
66. %&& />HP>(O (>>(=PG .HG GLGOG=.: H, (= (>>([ C= (:%G=1C=P H>1G>.
#include<iostream.h>
#include<conio.h>
)oid sort(int a^_8int n)"
)oid main()

clrscr()"
int n8a^?#_"
cout<<!enter number o3 elements in arra*!"
cin>>n"
cout<<!In enter the )alue o3 elements o3 arra*!"
3or(int iJ#"i<n"i&&)

cin>>a^i_"
2E
$
cout<<!enter )alue o3 elements o3 arra* are!<<!In!"
3or(iJ#"i<n"i&&)

cout<<!a^!<<i<<!_J!<<a^i_<<!In!"
$
cout<<!elements in ascending order are!<<!In!"
sort(a8n)"
getch()"
$
)oid sort(int x^_8int *)

3or(int iJ#"i<*"i&&)

3or(int YJ#"Y<*"Y&&)

i3(x^i_<x^Y_)

int tempJx^i_"
x^i_Jx^Y_"
x^Y_Jtemp"
$
$
$
3or(iJ#"i<*"i&&)

cout<<x^i_<<!In!"
$
$
!F

Você também pode gostar