You are on page 1of 15

1.

C++ Symbols:
# ; ( ) < > { } << >> // []
2. C++ Keyword:
1. include
(statement to include the library files in the codin)
synta!" #include <library file>
#. cout
(statement to dis$lay strin on the out$ut screen)
synta!"
cout << %your strin% ;
cout << endl;
cout <<&test&<<endl<<&test#&;
cout <<&test'ntest#&;
(. cin
(statement to recei)e in$ut entered by user on the out$ut screen)
synta!" cin >> )ariablename ;
*. clrscr();
+ function to clear the screen before dis$layin anythin on the out$ut screen
,. endl - end line.
cout << endl;
.. 'n - ne/ line.
cout << 0'n&;
1. return
+ sto$/terminate e!ecution and return bac2 to function
3. usin names$ace std;
+ settin a lobal class
4. system (0cls&);
+ to clear screen
3. Library files:
1. iostream
+ include this file if you /ant to use cout and cin statement
#. cstdlib
+ /hen you use system (0cls&);
4. C++ Structure:
//
#include <library files
usin! names"ace std#
int main$%
&
'
()ercise 1a:
5ello 6orld7
()ercise 1b:
5ello
6orld7
()ercise 1c:
5ello
6orld7
8ody of your main function
9nythin that you code here /ill be e!ecuted
return :;
()ercise 1d:
5ello 6orld7
;his is <== >rorammin
()ercise 1e:
????????????????????????
5ello 6orld7
????????????????????????
()ercise 1f:
????????????????????????
5ello 6orld7
????????????????????????
()ercise 1!:
5ello 6orld7
;his is <== >rorammin
@y name is 0your name&
*. +ariable declaration:
datatype )ariablename;
Aata ty$e can be"
char used for any character (al$habetsB numbersB symbol)
int only for inteer
lon int only for inteer /ith bi )alue
float for real number
double for real number
lon double for real number
C!am$les"
char name; //only one character
char name[1:]; //an array
strin name; //a strin of characters
int num1; //only /hole )alue is acce$ted
float num#; //decimal numbers are acce$ted to 1 diits $recision
double num(; //decimal numbers are acce$ted to 1, diits $recision
lon double num*; //decimal numbers are acce$ted to 14 diits $recision
()ercise 2:
Cnter a number" ,
;he number youD)e entered is ,
()ercise 3:
6hat is your nameE
> Fue
GhB hello Fue7 6elcome to <== >rorammin.
()ercise 4:
6hat is your nameE
> Fue
GhB hello Fue7 6elcome to <== >rorammin
5o/ old are youE
> 11
9re you sure you are 11B FueE ;+)
,,,,,,-utorial2
()ercise *:
;he $rice is H@(.,: each. 5o/ many do you /antE
> (
;he total $rice is H@1:.,:
()ercise .a:
Cnter first number" (
Cnter second number" *
( = * ? 1
()ercise .b:
Cnter first number" (
Cnter second number" *
( = * ? 1
( I * ? 1#
( + * ? +1
( / * ? :.1,
()ercise /:
Cnter the heiht"
> (
Cnter the /idth"
> *
;he area /hen heiht is 3 and /idth is 4 is 12
;he $erimeter /hen heiht is 3 and /idth is 4 is 14
()ercise 0:
>roram to <alculate the Jncome >ayment for <om$any KLM
;he hourly $ay rate is H@#,.::
Cnter ho/ many hours /or2ed this /ee2" 3*
5ours /or2ed" 3*
Jncome $ayment this /ee2" H@0/*.11
;han2 you.
.. 2ecision Lo!ic Structure: if 3 else statement
Jf thereDs only 1 sinle statement for each alternati)e"
if (logical expression)
statement_if_true1;
else
statement_if_false;
Nested if
if (logical expression)
{
statement_if_true;
}
else if (logical expression)
{
statement_if_true;
}
else if (logical expression)
{
statement_if_true;
}
else
{
statement_if_false;
}
Jn you loical e!$ressionB use these symbols only"
>
<
>?
<?
??
7?
Jf thereDre more than 1 statement for each alternati)e"
if (logical expression)
{
statement_if_true1;
statement_if_true2;
statement_if_true_n;
}
else
{
statement_if_false1;
statement_if_false2;
statement_if_false_n;
}
Nested if
if (logical expression)
{
statement_if_true1;
statement_if_true2;
statement_if_true_n;
}
else if (logical expression)
{
statement_if_true1;
statement_if_true2;
statement_if_true_n;
}
else if (logical expression)
{
statement_if_true1;
statement_if_true2;
statement_if_true_n;
}
else
{
statement_if_false1;
statement_if_false2;
statement_if_false_n;
}
()ercise 4:
Cnter your mar2"
> .1
Lou $ass.
5un t6e "ro!ram a!ain7
Cnter your mar2"
> 44
Lou fail.
()ercise 11:
Condition:
If user enter A, display Thats correct!
If user enter other than A, display Thats wrong!
>ress 97
>8
Lou entered 8. ;hatDs correct7
Hun the $roram aainB
<ondition"
@ar2 >?,: >ass
@ar2 <,: Oail
>ress 97
>9
Lou entered 9. ;hatDs /ron7
()ercise 11:
>roram to <alculate the 9rea or the >erimeter of a Hectanle
Cnter the /idth" 4
Cnter the heiht" 3
>lease ty$e 9 to find out the 9rea or 8 to find out the >erimeter" 8
;he area is" 12
;han2 you.
>roram to <alculate the 9rea or the >erimeter of a Hectanle
Cnter the /idth" 4
Cnter the heiht" 3
>lease ty$e 9 to find out the 9rea or 8 to find out the >erimeter" 9
;he $erimeter is" 14
;han2 you.
>roram to <alculate the 9rea or the >erimeter of a Hectanle
Cnter the /idth" 4
Cnter the heiht" 3
>lease ty$e 9 to find out the 9rea or 8 to find out the >erimeter" :
6ron selection. >roram /ill be terminated.
;han2 you.
()ercise 12:
>roram to <alculate the Jncome >ayment for <om$any KLM
;he hourly $ay rate is H@#,.::
Cnter ho/ many hours /or2ed this /ee2" 3*
5ours /or2ed" 3*
Jncome $ayment this /ee2" H@0/*.11
;han2 you.
()ercise 13:
Condition:
Maxiu hours wor!ed per wee! is "# hours$
%o the axiu payent should &e 'M(###$##
)ut if the eployee wor!ed for ore than "# hours per wee!, the extra hour he*she
wor!ed will &e paid 'M+#$## per hour$
>roram to <alculate the Jncome >ayment for <om$any KLM
;he hourly $ay rate is H@#,.::
Cnter ho/ many hours /or2ed this /ee2" *1
Lou ha)e /or2ed e!tra 11 hours.
5ours /or2ed" *1
Jncome $ayment this /ee2" H@1*11.11
;han2 you.
-6e 8;2 o"erator <<
if ( (com$arison1) PP (com$arisonQ#) )
statement;
else
statement;
-6e =5 o"erator >>
if ( (com$arison1) RR (com$arisonQ#) )
statement;
else
statement;
()ercise 14:
Create an ?@= c6art and t6en code t6e "ro!ram.
>lease enter the mar2" /1
Lour rade is 9
>lease enter the mar2" 11.
:arA is inBalid
>lease enter the mar2" ,/
:arA is inBalid
<ondition"
3: <? 9 <? 1::
1: <? 8 < 3:
.: <? < < 1:
,: <? A < .:
: <? O < ,:
()ercise 1*:
??????????????????????????????
9 mini calculator by Lour Name
??????????????????????????????
>lease enter the first number" 4
>lease enter the second number" *
Felect the o$eration that you /ant to do"
9. 9dd
A. Ai)ide
@. @ulti$ly
F" Fubtract
Cnter your selection (9B AB @ or F)" 8
4 = * ? 4
/. Loo" Lo!ic Structure:
?ncrement and decrement o"erator
Jncrement o$erator ==
Aecrement o$erator ++
countdo/n ==
countdo/n ? countdo/n=1
countdo/n ? countdo/n + 1;
countdo/n ++;
/.1 while loo"
while (logical_expression)
{
statement_1;
statement_2;

statement_n;
}
while (logical_expression)
statement_1;
/.2 for loo"
for (initial value; logical statement; operator)
{
statement_1;
statement_2;

statement_n;
}
for (initial value; logical statement; operator)
statement_1;
/.3 do-while loo"
do
{
statement_1;
statement_2;

statement_n;
} while (logical_expression);
0. Cunctions
Ounction must be declared before you can use it. ;
6hen you /ant to use the functionB you call it. ;
;he instruction on /hat the function can do is in the function $rototy$e.
Ftructure"
#include statement
lobal )ariable declaration
function declaration
main()
{
local )ariable is declared here (if any)
function is called here..
}
function $rotoyt$e
{
local )ariable is declared here (if any)
}
function $rotoyt$e
{
local )ariable is declared here (if any)
}
5o/ to declare the function"
returnDBalueDdataty"e nameDofDfunction $ar!umentsE"arameter%
C!am$le"
Jf the function must return the )alue /hen it is calledB then you must $ut in the
dataty$e of the )alue that /ill be returned.
Jf the )alue to be returned is of ty$e charB then the function declaration is"
char functionQname (aruments/$arameters);
;he function can also ha)e no return )alue. Jn such casesB the function declaration
is"
)oid functionQname (aruments/$arameters);
Cunction ar!umentsE"arameters
;he aruments/$arameters are the $assin )alue. Fometimes you need it;
sometimes you donDtB de$endin on your $roram. Jf your $roram reSuires you to
$ass the )alueB then you must declare the )ariable in the ( ).
C!am$le"
Jf the function is oin to as2 the user to enter a numberB the function can be"
)oid etQ)alue (int a);
GH
int etQ)alue (int a);
GH
int etQ)alue (intP a);
Jf the function does not need any $assin )alues or $arameter or arumentsB you
can lea)e a blan2 in the brac2et.
C!am$le" int etQ)alue();
Jn your main functionB Tust call the other functions.
;he other functions are"
1. Ounction to as2 the user to enter data (Head @odule)
#. Ounction to calculate the ans/er (<alc @odule)
(. Ounction to dis$lay the ans/er (>rint @odule)
#include <iostream.h>
#include <conio.h>
)oid et (intP a B intP b );
int calc (int aB int b);
)oid dis$lay (int sum);
int sum;
Function Declaration
Function call for get.
x and y are actually a and b that come from
function prototype get.
main()
{
int !B y;
clrscr();
et(!By);
sum ? calc(!B y);
dis$lay (sum);
etche();
}
)oid et (intP aB intP b)
{
cout << %9" %;
cin >>a;
cout << %8" %;
cin >> b;
}
int calc (int aB int b)
{
sum ? a=b;
return sum;
}
)oid dis$lay (int sum)
{
cout <<%Fum is %<< sum ;
}
Function prototype for function get
No return value
Variables need to be passed to other
modules
Function prototype for function calc
Theres return value
Variables need to be taken from other
modules
Function prototype for function display
No return value
Variables need to be taken from other
modules
Function call for calc.
x and y are actually a and b that come from function prototype
get that have been passed to calc module to be calculated.
Function call for display.
sum is actually the return value that comes from
calc module.

You might also like