cnotes full
TRANSCRIPT
-
8/20/2019 Cnotes Full
1/187
INTRODUCTION
COMPUTER: Computer is an electronic device, which has memory and performs arithmetic and logicaloperations.
Input: The data entered in the computer is called input. Ex:- Keyboard, mouse, oy stic! etc.,
Output: The resultant information obtained by the computer is called output. Ex:- Through "creen, #rinter etc.,
Program: $ se%uence of instructions that can be executed by the computer is called program.
Software: $ group of program to operate and control the computer is called software
Hardware: $ll the physical components or units of computer system connected to the computer circuit is calledhardware.
Operating System: &t is an interface between user and the computer. &n other words, operating system is acomplex set of programs, that manage the resources of a computer. 'esource include input, output, processoretc., "o operating system is a 'esource manager.
anguage: &t consists of a set of executable instructions.
Pa!"age: &t is designed by any other language with limited resources.
#arious steps in$o%$ed in program or &pp%i!ation
de$e%opment: The following steps are involved in program
development.(. #roblem definition). $nalysis and *esign+.$lgorithms. low Chart. Coding and &mplementation/. *ebugging 0 errors1 and testing2. *ocumentation(. Pro'%em Definition: #roblem definition phase is a clear
understanding of exactly what is needed for creating awor!able solution. 3e must !now exactly what we want to do
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
2/187
before we do a problem. &t involves three specificationsregarding a proper definition.
• &nput "pecification• 4utput "pecification• #rocessing
). &na%ysis and Design: 5efore going to ma!e a final solutionfor the problem, the problem must be analy6ed outline
solution is prepared in the case of simple problems. 5ut in thecase of complex problems, the main problem is divided intosub problems called modules. These modules can be handledand can be solved independently. 3hen the tas! is too big, it isalways better to analy6e the tas!, such that, it can be dividedinto number of modules and see! solution for each.
+. &%gorit(ms: 4nce the problem is divided into number of
modules, the logic for solving each module can be developed.The logic is expressed step by step. $ step by step procedureto solve the given problem is called algorithm. $n algorithm isdefined as a finite set of instructions which accomplish a particular tas!. $n algorithm can be described in a naturallanguage li!e English.
. )%ow C(art: $fter completion of algorithm, the program can be visuali6ed by drawing a flow chart. $ flow chart is nothing but a graphical or symbolic representation of how instructionswill be executed one after another.
. Coding and Imp%ementation: Coding is a process of converti
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
3/187
/. the algorithm solution of flow chart in computer program. &n t2. process each and every step of algorithm is converted
instructions of selected computer programming language. 5ef
selecting the programming language we must follow thconsiderations.
Nature of pro'%em
• Programming %anguage a$ai%a'%e*
• imitations of !omputer*
+* De'ugging and Testing:,
De'ugging: 5efore loading the program into the computer wemust correct all the errors. This process is called debugging.There are three types of errors.
-Synta. error
-Runtime error
-ogi!a% errorTesting:, &t is very important to test the program written toachieve a specific tas!. Testing is running the program with!nown data and !nown result.
2. Do!umentation:, &t is the most important aspect of programming. &t is a continuous process. To !eep the copy ofcontinuous process. To !eep the copy of all the phases0involving1 in parts, definition, analysis and designing,algorithm, flow chart, coding and implementation, debuggingand testing are the parts of the documentation. This phase
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
4/187
involves to produce written document for the user.
C%assifi!ation of Programming anguage
#rogramming language can be classified into ) types(. 7igh 8evel 8anguage and). 8ow 8evel 8anguage.
Hig( e$e% anguage:, Those are more English li!e language and hence the programmers foundthem very easy to learn to convert the programs in high level language to machine language!ompi%ers and interpreters are used.
ow e$e% anguage:, $ll low level language called assembly language is designed in the beginning. &t has some simple instructions. Those instructions are not binary codes. 5ut thecomputer can understand only the machine language. 7ence a converter or translator is developed totranslate the low level language programs into machine language. This translator is !nown as
$ssembler.
Trans%ators:, There are three types of translators available for the language.
/* &ssem'%er
0* Compi%er
1* Interpreter
(. &ssem'%er:, This translator is used to convert the programs written in low levelanguage0$ssembly1 into machine language.
). Compi%er:, Compiler is used to convert high level language into machine level language. &t
chec!s for error in the entire program and converts the program into machine language.+. Interpreter:, This is also used to convert high level language into machine language. &t chec!s
for errors statement by statement and converts the statement into machine level language.
There are )/ characters by the micro computer. These values 9 to). These can be divided operating system under.C(ara!ter type Num'er of C(ara!ter
Capital letteres )/ 0 $ to 1"mall 8etters )/ 0a to 61*igits (9 0 9 to ;1
"pecial symbols +)Control Characters +
-
8/20/2019 Cnotes Full
5/187
a to 6 ;2 to ())9 to ; = to 2Enter (+"pace +)Tab ;
5ac! "pace =
&%gorit(m:,$ step by step procedure to solve the given problem is !nown as algorithm./* To find t(e sum2 produ!t2 and di$ision of gi$en two num'ers*
Steps:,,
(. 'ead any two numbers a,b). sum>a?b+. product>a@b. division>aAb. print sum, product, division/. end
0* To find t(e ma.imum $a%ue of gi$en two num'ers:
Steps:,
(. 'ead any two values a,b). max>a+. if maxBb then max>b. print max. end
1* To find t(e ma.imum $a%ue of t(ree num'ers
Steps:,
(. 'ead any three values a,b,c). max>a+. if maxBb then max>b a>(9 b>)9 c>. if maxBc then max>c. print max/. end
3* To !(e!" w(et(er t(e gi$en num'er is e$en or odd
Steps:,
(. 'ead n). if n)> > 9,
print Dn is even else
print Dn is odd+. end
4* To disp%ay natura% num'ers from / to gi$en num'erSteps:,
(. 'ead n
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
6/187
). i>(+. print i. i>i?(. if iB>n then to to step+/. end
5*To disp%ay fa!tors of gi$en num'ers
Steps:,
(. 'ead n). i>(+. if ni> > 9 then print i. i>i?(. if iB>n then to to step +/. end
6* To disp%ay fa!toria% of gi$en num'er
Steps:,(. 'ead n). fact>(+. fact>fact@n. n>n-(. if nF>( then go to step+/. print fact2. end
+* To disp%ay (ow many digits in gi$en num'er
Steps:
(. 'ead n). nd>9+. nd>nd?(. n>nA(9. if nF9 then go to step+/. print nd2. end
7*To !a%!u%ate and disp%ay sum of gi$en digits in gi$en num'er
Steps:,
(. 'ead n )). "um>9+. "um>"um?0n(91 sum>?)>2. n>nA(9 )A(9>9. &f nF9 then go to step+/. #rint sum2. end
/8* To !(e!" w(et(er t(e gi$en num'er is Pa%indrome or not
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
7/187
Steps:,
(. 'ead n ))). rev>9,m>n m>))+. rev>rev@(9?0m(91 )@(9?0)(91>)). m>mA(9 ))A(9 >)
. if nF9 then go to step+/. if n> > rev then print Dn is palindromeelse
Dn is not palindrome2. end
)O9 CH&RT
Definition:
-
8/20/2019 Cnotes Full
8/187
1C &N;U&;E
C is a programming language it is designed by *EGG&" '&TC7&E in (;2) at $T H T I$mericanTelephone and telegraphsJ 5ell labs in "$. C is most popular general purpose language.
-
8/20/2019 Cnotes Full
9/187
(9. C has a richest of operators.((. Extensive verities of da x ta types such as pointers, structures, nions etc., available in C.(). 'ecursive function calls for algorithmic approach is possible with C.(+. Mainly we are using the C language to implement the system software those are compilers, text
editors, Getwor! drives, data base utilities and finally the operating systems.
(. C compiler combines the capabilities of assembly level language with the features of high levellanguage. "o it is called as middle level language.
Important Points
(. C is a case sensitive programming language. C statements are entered in lower case letters only.). Every C statement must be terminated by a semicolon 0 N 1 except preprocessor statements and
function definition.+. C is a function oriented language. $ny C program contains one or more functions. Mainly one
function is necessary by the name called main. 3ithout main we cannot execute C #rograms.. $ function is represented by a function name with a pair of parenthesis.
DI&;R&M O) E?ECUTION O) C PRO;R&ME
4nce the coding is completed, the program is feed into the computer using a compiler to producee%uivalent machine language code.
&n C compilation two parts are there namely (1 Compiler and )1 8in!er.Compiler receives C source file as input and converts the file into obect file. Then the lin!er receives
that obect file as input and lin!ing with C libraries after lin!ing it produces executable file. $fter creation ofexecutable file then start the program execution and loads the information of the program in the primarymemory through loading process after loading the information the processor processing the information andgives the output.
-
8/20/2019 Cnotes Full
10/187
8in! "ection.I
-
8/20/2019 Cnotes Full
11/187
4r 0format string1
The printf statement is used to print messages, the values contain in the variable on the screen.
stdio*(:, "tandard input and output header file.
!onio*(:,Consol input and output header file. Example: printf0D3elcome to C #rogramming1N
Program:
QincludeBstdio.hFQincudeBconio.hFSoid main01O printf0D3elcome to C #rogramming1NP
!%rs!r@A:, &t clears text mode window. Syntax:- void clrscr01N
get!(@A:, &t gets a character from console but does not echo to the screen. Syntax:- int getch01N
Program:
QincludeBstdio.hFQinculdeBconio.hFvoid main01O clrscr01N printf0D3E8C4ME T4 C #'4
-
8/20/2019 Cnotes Full
12/187
/*Identifier:, &dentifiers can be defined as name of the variables,arrays, functions and some other program elements using thecombination of the following characters.
lphabets# $ to a to 6
Digits# 9 to ;$nderscore# 0 &t is usually used as a lin! between twowords in long
&dentifiersNote:, (. The first character of an identifier will be an alphabetor underscore
). The default identifier length is +) characters.
0*>eywords:, Keywords are the words whose meaning has allready been explained to the compiler. That means at the time ofdesigning a language some words are reserved to do specific tas!s.
"uch words are called as !eywords or reserve words. C has only +)!eywords. They are
$uto brea! case char Const Continue default externloat ar for gotoif &nt long register
'eturn "hort signed si6eof "tatic "truct switch typedef nion nsigned void while*o *ouble else enum
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
13/187
Constants: Constants in C referred to fixed values that do notchange during the execution of program. C supports the followingconstants.
Constants
Gumeric Constants Character constants
&nteger constants, 'eal Constants "ingle CharacterConstants, "tring Constants
Operators:, &t is a symbol which perform a particular operation.
C has a rich set of operators.The following operators available in C are(. $rithmetic 4perator.). 8ogical 4perator.+. 'elational 4perator.. 5it-wise operator etc.,
Spe!ia% C(ara!ters:, $ll characters other than alphabet and digitsare treated as special symbols.
D&T& T=PES
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
14/187
The !ind of data that variables may hold in a programminglanguage is called data types. C data types can be classified intothree types namely..
#rimary data typesser defined data types*erived data types
PRIM&R= D&T& T=PES OR PRIMITI#E D&T& T=PES
$ll C compilers supports four fundamental data types namelyint% char%% float% double&.
int:, &t is a positive, negative and the whole values but not adecimal number.
!'ample# (9,9,-),-(+,9 etc.,
!(ar:, $ single character can be treated as character data type. &t isdefined between single %uotation 0UL1
!'ample# UaL,L7L, U@L,L=L etc.,
string:, $ string is a group of characters. 5ut in C language astring is also a character data type and is defined between 0D 1double %uotation.
!'ample# Dwelcome, D5*#", Dabc()+ etc.,
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
15/187
f%oat:, The number which are stored in the form of floating pointrepresentation is !nown as float data type. 0or1 The value withdecimal point.A
!'ample# ((.+,(+.)9),9.)+, etc.dou'%e:, The number which are stored in the form of double precision floating point representation is !nown as double datatype.
!'ample# (+).+2=;2, -))2.9;;=2, 9.;2)
USER DE)INED D&T& T=PES
The user defined data types enable a program to invent hisown data types and define what values it can ta!en on.
Thus this data types can help a programmer to reducing the programming errors. There are two types of user defined datatypes namely..
(. enum (enumerated data t"pe&). typedef (t"pe definition&
DERI#ED D&T& T=PES
The derived data types are created from the basic integers,characters and floating values. The examples of derived data typesare arrays, pointer, structures, unions, etc.,
Type Modifiers or Type ua%ifiers
@signed2 unsigned2 s(ort2 %ongA
$ type modifier alters the meaning of the base data type toyield a new type. Each of these modifiers can be applied to the
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
16/187
base type int. The modifiers signed and unsigned can also beapplied to the base type char. &n addition, long can applied todouble.
D&T& T=PES2 )ORM&T SPECI)IERS2 MEMOR= SIFE
&ND THEIR &CCESS &
-
8/20/2019 Cnotes Full
17/187
stringx 7exa *ecimal base o 4ctal base p Memory$ddress
#aria'%es:, $ %uantity which may vary during the execution of a program is !nown as variable.
De!%aration of a $aria'%e:, data type identifierN0or1
data type identifier-(, identifier-),RRRidentifier-nN !'amples# int nN
char ch Nfloat fNint a,b,cN
Initia%iGation:, $t the time of declaring a variable we can store some data into that variable is !nown as
initiali6ation.data type identifier>valueN
!'amples#) int n>(9Nchar ch>LsLNint a>(9,b>)9,c>+9Nint a>(99,b,c>(9N
!'ample#) int n>(99N(.printf0Dd,n1N ).printf0Dvalue of n>d,n1N
output>(99 output:- value of n>(99Program:
QincludeBstdio.hF
QincludeBconio.hFvoid main0 1O int n>(9N clrscr01N printf0DSalue of n>d,n1N getch01NPProgram: *rite a program to initiali+e , standard data t"pe variables and displa" initial data.
QincludeBstdio.hFQincludeBconio.hF
Soid main01O int n>(99N char ch>LhLN float f>(=.9;/)N double d>;+).)(N clrscr01N printf0Dn>d,n1N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
18/187
printf0DVn ch>c,ch1N printf0DVn f>f,f1N printf0DVn d>lf,d1N getch01NP
Note:(. &n C language all declarations will be done before the first execution of programs.). loating values displays / digits after the decimal point by defaultProgram:
QincludeBstdio.hFQincludeBconio.hFSoid main01O float f>(=.9;/)N clrscr01N printf0Df>f,f1N
printf0DVn f>.)f,f1N getch01NP
Es!ape seuen!e !(ara!ters
Vn > new line Vt > 7ori6ontal tab 0default = spaces1Vv > Sertical tab 0default ( line1 Vb > 5ac! spaceVr > Carriage return Va > $lert 05eep sound1Program:
QincludeBstdio.hFQinculdeBconio.hFvoid main01O clrscr01N printf0D5*#" "4T3$'E 8&M&TE*1N printf0DVnS&W$X$3$*$1N getch01NPConstants:, Constants in C referred to fixed values that do not change during the execution of a program.
Const:, &t is a !eyword and is used to define constants. Syntax:- const data type identifier>valueN
0or1Constdatatypeidentifier-(>value,identifier-)>value,RRidentifier-n>valueN
Program:
QincludeBstdio.hFQinculdeBconio.hFvoid main01O const int n>(99N clrscr01N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
19/187
printf0Dn>dVn,n1NA@n>)99N cannot modify@A printf0Dn>d,n1N getch01NP
Sym'o%i! Constants:, &t is a pre-processor statement and is used to define symbolic constants. Syntax:- Qdefine identifier value.Example:- Qdefine pi +.(
Qdefine g ;.=Program
QincludeBstdio.hFQinculdeBconio.hFQdefine pi +.(Qdefine g ;.=O clrscr01N
printf0Dpi>.)f,pi1N printf0DVn g>.lf,g1N getch01NPs!anf@A:, &t is a function, which is used to read data from standard input device.
S"nta'#) int scanf0Dformat0s1,address-(,address-),RR.,address-n1NE.amp%e:, int n N
$ddress of n>Hn scanf0Dd,Hn1N
Program
QincludeBstdio.hFQinculdeBconio.hFvoid main01O int nN clrscr01N prinf0DEnter the value of n:1N scanf0Dd,Hn1N printf0Dd,n1N getch01NPProgram
QincludeBstdio.hFQinculdeBconio.hFvoid main01O char chN int nN float fN
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
4utput:- n>(99n>(99
-
8/20/2019 Cnotes Full
20/187
double dN clrscr01N prinf0DEnter any charactaer:1N scanf0Dc,Hch1N prinf0DEnter any value:1N
scanf0Dd,Hn1N prinf0DEnter any float:1N scanf0Df,Hf1N prinf0DEnter any double:1N scanf0Dlf,Hd1N printf0Dc,ch1N printf0Dd,n1N printf0Df,f1N printf0Dlf,d1N getch01NP
Program
QincludeBstdio.hFQinculdeBconio.hFvoid main01O int nN char chN clrscr01N prinf0DEnter any integer:1N scanf0Dd,Hn1N prinf0DEnter any charactaer:1N scanf0Dc,Hch1N printf0Dd,n1N printf0Dc,ch1N getch01NP
&n the above program it cannot read character into a character variable, because the int reading statementcreates a new line char in the input stream that will assign to the character variable.
To avoid this problem we use Dfflush function 0or1 DVn character
ff%us(:, &t flushed specified stream Syntax:- int fflush 0stream-name1NExample:-fflush0stdin1N
f%us(a%%:, &t flushes all open streams. Syntax:- int flushall01N
Program:
QincludeBstdio.hFQinculdeBconio.hFvoid main01
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
21/187
O int nN char chN clrscr01N prinf0DEnter any integer:1N
scanf0Dd,Hn1N prinf0DEnter any charactaer:1N scanf0Dc,Hch1N fflush0stdin1 or flushall 0 1N printf0Dd,n1N printf0Dc,ch1N getch01NPProgram:
QincludeBstdio.hFQinculdeBconio.hF
void main0 1O int nN char chN clrscr01N prinf0DEnter any integer:1N scanf0Dd,Hn1N prinf0DEnter any charactaer:1N scanf0DVnc,Hch1N printf0Dd,n1N printf0Dc,ch1N getch01NPString:, $ group of characters define between double %uotation mar!s is a string.
&n C language a string is nothing but an array of characters and terminated by a null character 0V91. Syntax:- char identifierIsi6eJNExample:- char st0)91N
Initializatin:- char identifier> DstringNExample:- char stI)9J> D3E8C4MEN
Note:, 3hen the compiler assigns a character string to a character array it automatically supplies a nullcharacter0V91 at the end of the string.
Therefore the si6e should be e%ual to the maximum number of characters are string?(.Program
QincludeBstdio.hFQincludeBconio.hFvoid main01O char stI)9JN clrscr01N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
22/187
printf0YEnter any string:Y1N scanf0YsY,st1N printf0Y
-
8/20/2019 Cnotes Full
23/187
&rit(meti!a% Operators:,These are the basic operators in C language. They are useful for performingarithmetic calculations.
? addition A division- subtraction Modulus 0remainder1@ Multiplication
Program:,*rite a program to accept an" two numbers and test all arithmetic operatorsQincludeBstdio.hFQincludeBconio.hFvoid main01O int a,bN clrscr01N printf0YEnter any Two numbers:Y1N scanf0YddY,Ha,Hb1N printf0YVn$ddition of d and d is dY,a,b,a?b1N printf0YVn"ubtraction of d and d is dY,a,b,a-b1N
printf0YVnMultipliction of d and d is dY,a,b,a@b1N printf0YVn*ivision of d and d is dY,a,b,aAb1N printf0YVnModulus of d and d is dY,a,b,ab1N getch01N Pogi!a% operators:, These operators are used for combine the result of two or more expressions.
HH 8ogical $G* [[ 8ogical 4' \ 8ogical G4T
E/ E0 E/ E0 E/ E0
T T T TT T T T
&f E( > True then \E(> false &f E( > alse then \E( True
Re%ationa% operators:,These are used to test the relation between two values or two variables or expressions$ll c relational operators are binary operators and hence re%uires two operands.
B less thanF greater than
B> less than or e%ual to F> greater than or e%ual to > > e%ual to \> not e%ual to
&ssignment operators:, These are used to assign a value of an expression to an identifier.> $ssignment operator.
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
24/187
Compound assignment operators 0or1 "hort hand assignment operators:-?>, ->, @>, A>, > !'ample#)int a>(9N
a>a?)9N 0or1 a?>)9N
In!rement and De!rement operators:, These are used to control the loops in an effective method.
In!rement operators:, The symbol ]??] is used for incrementing by (. Syntax:- ??identifierN 0prefix increment1
identifier??N 0postfix increment1 !'ample#) int a>(9,bN
b>a?? here oAp : a>((,b>(9 b>??a here oAp : a>((,b>((
De!rement operators:, The symbol ]--] is used for decrementing by (. Syntax:- --identifierN 0prefix decrement1
identifier--N 0postfix decrement1 !'ample#) int a>(9,bN b>a-- here oAp : a>;,b>(9
b>--a here oAp : a>;,b>;
-
8/20/2019 Cnotes Full
25/187
printf0YVna[b>dY,a[b1N printf0YVnaZb>dY,aZb1N getch01N P
=,b,cN clrscr01N b>aBBN
c>aFF+N printf0YVn a>dY, a1N printf0YVn b>dY, b1N printf0YVn c>dY, c1N getch01N SPECI& OPER&TORS:
aA Ternary operator @orA Condition operator:-$ Ternary operator is represented as Y^:Y &t is used to constructa conditional expression. The general form of ternary operator is exp(^exp):exp+N here exp( , exp) , exp+ areexpressions.
&f exp( is true, then exp) is evaluated and its value becomes the value of expression. &f exp( is false,then the exp+ is evaluated and its value becomes the value of the expression.
Program:, *rite a program to find ma' and min of given two values b" using ternar" operaotrs
QincludeBstdio.hFQincludeBconio.hF !'ample# int a/0%b10%c%d2void main01 ca3b4a#b2O a/0% b10 and c10%d/0 int a,b,max,minN clrscr01N printf0YEnter values for a,b:Y1N scanf0YddY,Ha,Hb1N max>aFb ^ a:bN min>aBb ^ a:bN printf0YMaximum value is :dY,max1N printf0YVnMinimum value is :dY,min1N getch01N P
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
26/187
'AComma operator:,&t can be used to lin! the relative expressions together. The general form of a commaoperator is
Syntax: identifier >0identifier(>value,---------identifier n>value, expression1Program:,
QincludeBstdio.hF !'ample# int a%b%c2
QincludeBconio.hF c(a/0%b10%ab&2void main01 -herefore a/0% b10 % c0O (first assigns the value /0 to 7a8 ne't assigns int a,b,cN the value 10 to b and finall" assigns clrscr01N the value 0 to c& c>0a>(9,b>)9,a?b1N printf0Ya>dY,a1N printf0YVnb>dY,b1N printf0YVnc>dY,c1N getch01N P
Program:QincludeBstdio.hFQincludeBconio.hFvoid main01O int a,b,cN clrscr01N c>0a>(9,b>)9,0a?b1@)1N printf0YVn a>dY,a1N printf0YVn b>dY,b1N printf0YVn c>dY,c1N getch01N P!A siGeof operator:, &t is a compile name operator and is used to get the memory si6e of a variable , expressionand data type.
Syntax:- si6eof0variable0or1exp0or1datatype1NProgram:
QincludeBstdio.hFQincludeBconio.hFvoid main01O char chN int nN float ftN double dbN clrscr01N printf0YVn"i6e of char:d byteY,si6eof 0ch11N printf0YVn"i6e of int:d byteY,si6eof 0n11N printf0YVn"i6e of float:d byteY,si6eof 0ft11N printf0YVn"i6e of double:d byteY,si6eof 0db11N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
27/187
getch01N P
Program:, *rite a program to find ma'imum of given three values b" using ternar" operators.
QincludeBstdio.hF
QincludeBconio.hFvoid main01O int a,b,c,maxN clrscr01N printf0YEnter any three numbers:Y1N scanf0YdddY,Ha,Hb,Hc1N max>0aFbHHaFc1^a:0bFc^b:c1N printf0YMaximum value>dY,max1N getch01N P
Program:,*rite a program to find out the given value is even or Odd number b" using ternar" operators.
QincludeBstdio.hFQincludeBconio.hFvoid main01O int aN clrscr01N printf0YEnter any number:Y1N scanf0YdY,Ha1N a)> >9^ printf0YaN a>bN b>tN printf0Y$fter "wapping:Y1N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
28/187
printf0YVna>dY,Ha1N printf0YVnb>dY,Hb1N getch01N P
Program:, *rite same program without using third variable
QincludeBstdio.hFQincludeBconio.hFvoid main01O int a,bN clrscr01N printf0YEnter any two values into a and b:Y1N scanf0YddY,Ha,Hb1N printf0YdY,Ha1N printf0YVnb>dY,Hb1N A@ $TE' "3$##&Ga?bN b>a-bN a>a-bN printf0Y$fter "wapping:Y1N printf0YVna>dY,Ha1N printf0YVnb>dY,Hb1N getch01N P
PRECIDIENCE O) OPER&TORSS*No Category Operator 9(at it is or does( 7ighest 0 1
I Junction call$rray subscript
) nary \?-
??--H
si6eof
8ogical negationnary plusnary minus&ncrement operator *ecrement operator $ddress0returning si6e of
operand, in bytes1
+ Multiplicative @A
Mulitiply*ivision'emainder0modulus1
$dditive ?-
5inary #lus5inary minus
"hift BB 8eft shift operator
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
29/187
FF 'ight "hift operator / 'elational B
B>F
F>
8essthan8ess than or e%ual to\>HZ[
HH[[
E%ual to Got e%ual to5itwise $nd5itwise Exclusive 4r 5itwise or 8ogical and8ogical or
= Conditional ^: Ternary operator ; $ssignment >
@>A?
>?>->
"imple $ssignment$ssign #roduct$ssign _uotient
$ssignremainder0modulus1$ssign sum$ssign difference
(9 Comma , Evaluate
CONTRO ST&TEMENTS
@ORA
CONTRO STRUCTURES
C is consider as a structure programming language. 4ne of the reason for this is having various program
control structures.C process the decision ma!ing capabilities and supports the statements !nown as control statements.There are + types of control statements supported by CR
(. Condition control statements). n condition control statements+. 8oop control statements.
/* Condition !ontro% statements:, C supports five types of condition control statements. "imple if statement &f else statement Gested if statement
Else if ladder "witch statement
iA Simp%e if statement:, &t is a decision ma!ing statements and is used to control the flow of execution. Syntax:- if0expression1
O"tatementsN
alse P
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
Entry
Ex pr.
-
8/20/2019 Cnotes Full
30/187
True
&f expression is true then the statement bloc! will be executed and the statement bloc! will be executedand the control transfer to the next statement. 4therwise the expression is false then the control directly go tothe next statement.
Program: 3rite a program to find max of two values by using simple if statement^QincludeBstdio.hFQincludeBconio.hFvoid main01O int a,b,maxN clrscr01N printf0YEnter any two numbers:Y1N scanf0YddY,Ha,Hb1N max>aN if0maxBb1 O max>bN P printf0YMax value:dY,max1N getch01N PNote:, &n any control statement the statement bloc! contains only a single statement, braces are not necessary.
Program: 3rite a program to find the max value of given three numbers using simple if statements.QincludeBstdio.hFQincludeBconio.hFvoid main01O int a,b,c,maxN clrscr01N printf0YEnter any three numbers:Y1N
scanf0YdddY,Ha,Hb,Hc1N max>aN if0maxBb1 max>bN if0maxBc1 max>cN printf0YMax value:dY,max1N getch01N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
"tatements
Gext "tatements
-
8/20/2019 Cnotes Full
31/187
P
Program: 3rite a program to enter any date in date format and display given date.QincludeBstdio.hFQincludeBconio.hF
void main01O int d,m,yN clrscr01N printf0YEnter any date 0dd-mm-yyyy1:Y1N scanf0Yd-d-dY,Hd,Hm,Hy1N printf0Y
-
8/20/2019 Cnotes Full
32/187
P else O
"tatement-)N P
Entry
alse True
&f the expression is true then if bloc! statements are executed and else bloc! statements are ignored.4therwise the expression is false, then else bloc! statements are executed and if bloc! statements are ignored.Program:
QincludeBstdio.hFQincludeBconio.hFvoid main01O int a,b,maxN clrscr01N printf0YEnter any two numbers:Y1N scanf0YddY,Ha,Hb1N if0aBb1
max>aN else
max>bN printf0YMax value:dY,max1N getch01N P
Program: *rite a program to chec9 whether the given no. is even or odd.QincludeBstdio.hFQincludeBconio.hFvoid main01O int nN clrscr01N printf0YEnter any number:Y1N scanf0YdY,Hn1N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
Expr.
"tatement ) "tatement (
Gext "tatements
-
8/20/2019 Cnotes Full
33/187
if0n)>>91 printf0Y
-
8/20/2019 Cnotes Full
34/187
QincludeBconio.hFvoid main01O int a,b,c,maxN clrscr01N
printf0YEnter any three numbers:Y1N scanf0YdddY,Ha,Hb,Hc1N if0aFb1 O if0aFc1 max>aN else max>cN P else O
if0bFc1 max>bN else max>cN P printf0YMax Salue for given + numbers:dY,max1N getch01N P
Program: *rite a program to accept an" character and chec9 whether the given character is alphabet or digitor a special character.
QincludeBstdio.hFQincludeBconio.hFvoid main01O char chN clrscr01N printf0YEnter any character:Y1N scanf0YcY,Hch1N if00chF>/ HH chB>;91 [[ 0chF>;2 HH chB>())11 printf0Y= HH chB>21 printf0Y
-
8/20/2019 Cnotes Full
35/187
QincludeBstdio.hFQincludeBconio.hFvoid main01O char chN
clrscr01N printf0YEnter any character:Y1N scanf0YcY,Hch1N if00chF>/ HH chB>;91 [[ 0chF>;2 HH chB>())11 O if0ch>>]$] [[ ch>>]a] [[ ch>>]E][[ ch>>]e] [[ ch>>]&] [[ ch>>]i] [[ ch>>]4] [[ ch>>]o] [[ ch>>]] [[ ch>>]u]1 printf0Y
-
8/20/2019 Cnotes Full
36/187
printf0YVn$verage:dY,avg1N if0cF>9 HH cppF>9 HH unixF>91 O printf0YVn 'esult :#assY1N if0avgF>/91
printf0YVn *ivision :irstY1N else printf0YVn *ivision :"econdY1N P else printf0YVn 'esult :ailY1N getch01N Pi$A e%se if %adder:, &t is also used for a serried of decisions are involved.S"nta'#)if0expression-(1
O
stataement-(N Pelse if 0expression-)1 O
statements-)N P
-------------- -------------- --------------
else if 0expression-n1 O statements-nN P else O statementsN P
&n this statement expression are evaluated top to bottom. &f the condition true, the statements associatedthat bloc!s is executed and the control transfer to the next statement. 3hen all expressions are false, then thefinal else bloc! statements will be executed.
Program: *rite a program to find the ma'imum value of given no:s using nested if.QincludeBstdio.hFQincludeBconio.hFvoid main01O int a,b,c,maxN clrscr01N printf0YEnter any three numbers:Y1N scanf0YdddY,Ha,Hb,Hc1N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
37/187
if0aFb HH aFc1 max>aN else if0bFc1 max>bN else
max>cN printf0YMax Salue for given + numbers:dY,max1N getch01N P
Program: *rite a program to accept month and "ear. Calculate and disipla" number of da"s in the given
monthQincludeBstdio.hFQincludeBconio.hFvoid main01O
int m,y,ndN clrscr01N printf0YEnter any month number:Y1N scanf0YdY,Hm1N printf0YEnter any year number:Y1N scanf0YdY,Hy1N if0m>>)1 O
if0y>>91nd>);Nelsend>);N
P else if0m>>( [[ m>>+ [[ m>> [[ m>>2 [[ m>>= [[ m>>(9 [[ m>>()1 nd>+(N else nd>+9N printf0YGumber of days in the given month:dY,nd1N getch01N P
Program: 3rite a program to enter consumer number% name% present month reading and last month reading.Calculate and displa" total units and bill amount b" the following table=
Units Rate per Unit
9-9 's(.)(-(99 's ).(9(9(-)99 's ).=)9(-99 's. +.9$bove 99 's..9
QincludeBstdio.hF
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
38/187
QincludeBconio.hFvoid main01O int cno,pmr,lmr,tuN char cnameI)9JN
float billN clrscr01N printf0YEnter consumer number:Y1N scanf0YdY,Hcno1N printf0YEnter consumer name:Y1N fflush0stdin1N gets0cname1N printf0YEnter pmr and lmr:Y1N scanf0YddY,Hpmr,Hlmr1N tu>pmr-lmrN if 0tuB>91
bill>09@(.)1N if 0tuB>(991 bill>09@(.)1?00tu-91@).(91N
if 0tuB>)991 bill>09@(.)1?09@).(91?00tu-(991@).=1N
if 0tuB>991 bill>09@(.)1?09@).(91?0(99@).=1?00tu-)991@+.91N
else bill>09@(.)1?09@).(91?0(99@).=1?0)99@[email protected]
clrscr01N p rintf0YVnConsumer number :dY,cno1N printf0YVnConsumer name :sY,cname1N printf0YVn#resent month reading :dY,pmr1N printf0YVn8ast month reading :dY,lmr1N printf0YVnTotal units :dY,tu1N printf0YVn5ill amount :.)fY,bill1N getch01N P
-E.it:, &t terminated the program header file process.7 Syntax:- void exit0int status1N
-Status:, Typically a value of U9L indicates a normal exit, and a non 6ero value indicates some error.
Program: *rite a program to enter dealer number% dealer name% dealer region% number of liters petrol sold% no.
of liters diesel sold% no. of liters 9erosene sold. Dealer region are >) north% !)east% *)west% S)south
Calculate and displa" total region collection b" the following table.
Petrol Diesel ?erosene
>orth ,@.A5 1.65 /B.@5
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
39/187
South ,@.15 1./5 /B.15
!ast ,B.@5 /.A5 /A.50
*est ,B.5 0.B5 /A.5QincludeBstdio.hFQincludeBconio.hF
void main01Oint dno,nps,nds,n!sNchar dnameI)9J,dregionI)9JNfloat trgc,tcn,tcs,tce,tcwNclrscr01N printf0YEnter dealer number:Y1Nscanf0YdY,Hdno1N printf0YEnter dealer name:Y1Nfflush0stdin1Ngets0dname1N
printf0YEnter no of litres petrol sold:Y1Nscanf0YdY,Hnps1N printf0YEnter no of litres Kerosene sold:Y1Nscanf0YdY,Hn!s1N printf0YEnter no of litres diesel sold:Y1Nscanf0YdY,Hnds1Ntcn>nps@;.2?nds@+)./?n!s@(=.;Ntcs>nps@;.)?nds@+).(?n!s@(=.)Ntce>nps@=.;?nds@+(.2?n!s@(2.9Ntcw>nps@=.+?nds@+9.=?n!s@(2.+Ntrgc>tcn?tce?tcs?tcwNclrscr01N printf0YVntotal collection in north :.)fY,tcn1N printf0YVntotal collection in east :.)fY,tce1N printf0YVntotal collection in 3est :.)fY,tcw1N printf0YVntotal collection in "outh :.)fY,tcs1N printf0YVntotal region class :.)fY,trgc1Ngetch01N
P
II met(od:
QincludeBstdio.hFQincludeBconio.hFvoid main01O
int dno,np,nd,n!Nchar dnameI)9J,regNfloat pa,da,!a,taNclrscr01N printf0YEnter dealer number:Y1N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
40/187
scanf0YdY,Hdno1N printf0YEnter dealer name:Y1Nfflush0stdin1Ngets0dname1N printf0YEnter dealer region:Y1N
scanf0YcY,Hreg1Nif0reg\>]G] HH reg\>]"] HH reg\>]3] HH reg\>]E]1O
printf0Y&nvalid region:Y1Ngetch01Nexit091N
P printf0YEnter no of lts petrol sold:Y1Nscanf0YdY,Hnp1N printf0YEnter no of lts diesel sold:Y1Nscanf0YdY,Hnd1N
printf0YEnter no of lts !erosene sold:Y1Nscanf0YdY,Hn!1Nif 0reg>>]G]1O
pa>np@;.2Nda>nd@+)./N!a>n!@(=.;N
Pelse if 0reg>>]"]1O
pa>np@;.)Nda>nd@+).(N!a>n!@(=.)N
Pelse if 0reg>>]E]1O
pa>np@=.;Nda>nd@+(.2N!a>n!@(2.9N
PelseO
pa>np@=.+Nda>nd@+9.=N!a>n!@(2.+N
Pta>pa?da?!aNclrscr01N printf0YVn *ealer number :dY,dno1N printf0YVn *ealer name :sY,dname1N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
41/187
printf0YVn *ealer 'egion :cY,reg1N printf0YVn Go of lts petrol sold :dY,np1N printf0YVn Go of lts !erosene sold :dY,n!1N printf0YVn Go of lts diesel sold :dY,nd1N printf0YVn #etrol amount :.)fY,pa1N
printf0YVn Kerosene amount :.)fY,!a1N printf0YVn *esel sold :.)fY,da1N printf0YVn Total amount :.)fY,ta1Ngetch01N P
Program: *rite a program to enter emplo"ee number% emplo"ee name% emplo"ee grade%basic salar". Calculate
and displa" R% D% -% -% PE% FROSS SGRH >D >!- SGRH b" the following table.
))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))Frade Iaic R D - - PE
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
-
8/20/2019 Cnotes Full
42/187
O printf0Y&nvalid gradeY1Ngetch01Nexit091N
P
printf0YEnter basic salary:Y1Nscanf0YfY,Hbs1Nif0gr>>]$] [[ gr>>]a]1O
if0bsB>)9991O
hra>bs@=A(99Nda>bs@()A(99Nta>bs@)A(99Nit>9N pf>bs@)A(99N
Pelse if0bsB>9991O
hra>bs@(9A(99Nda>bs@(A(99Nta>bs@+A(99Nit>bs@)A(99N pf>bs@A(99N
Pelse if0bsB>9991O
hra>bs@()A(99Nda>bs@(A(99Nta>bs@A(99Nit>bs@A(99N pf>bs@/A(99N
PelseO
hra>bs@(A(99Nda>bs@(=A(99Nta>bs@A(99Nit>/A(99N pf>bs@=A(99N
PPelseO
if0bsB>)9991O
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
43/187
hra>bs@/A(99Nda>bs@=A(99Nta>bs@(A(99Nit>9N pf>bs@(A(99N
Pif0bsB>9991O
hra>bs@=A(99Nda>bs@(9A(99Nta>bs@+A(99Nit>bs@)A(99N pf>bs@)A(99N
PelseO
hra>bs@(9A(99Nda>bs@(A(99Nta>bs@A(99Nit>bs@+A(99N pf>bs@A(99N
PPgs>bs?hra?da?taNded>it?pfNns>gs-dedNclrscr01N printf0YVnEnter Employee numbe:dY,eno1N printf0YVnEnter Employee name :sY,ename1N printf0YVnEnter Employee grade:cY,gr1N printf0YVn5asic "alary :.)fY,bs1N printf0YVn7ra :.)fY,hra1N printf0YVn*a :.)fY,da1N printf0YVnta :.)fY,ta1N printf0YVnit :.)fY,it1N printf0YVnpf :.)fY,pf1N printf0YVn
-
8/20/2019 Cnotes Full
44/187
OCase value (N
"tatement(N5rea!N
----------------------
----------------------Case value nN"tatements nN5rea!.N
*efaultN*efault statementN
PThe switch statement tests the value of the given variable or expression against a list of case values and
when a match is found, a bloc! of statements associated that case is executed. 4therwise the default bloc!statements will be executed.
-
8/20/2019 Cnotes Full
45/187
brea!Ncase :
printf0YVnModulus d and d is dY,a,b,ab1N brea!N
default :
printf0YVn&nvalid 'egion:Y1N P getch01NP
Program: *rite a menu driven program to compute the electricit" bill for the domestic and commercial
purposes with the following data.
"# Dme$ti% pp$e:
units RateLunit
))))))))))))))))))))))))))
0)50 /.,5
5/)/00 1.B5/0/)100 .@5
10/)00 ,.5010/)00 5.00
))))))))))))))))))))))))))
e'tras# Rs./0L) Service charge and Rs.0.06 ps per unit power ta'% Subject to
minimum of Rs./0 single phase and Rs.10 for phase
"# Cmme#%ial pp$e
units RateL$nit )))))))))))))))))))))))))))))
0)/00 .@5
above /00 A.00))))))))))))))))))))))))))))
e'tras# Rs.10L) Service charge and Rs.0.06 ps per unit power ta'% Subject to
minimum of Rs.10 single phase and Rs.50 for phaseQincludeBstdio.hFQincludeBconio.hFvoid main01O
int cno, pmr,lmr,tu,phNchar cnameI)9J,typeNfloat bill,se, pt,tamtNclrscr01N printf0YVnVtVtMEGY1N printf0YVnVtVt---------------Y1N printf0YVnVtVtC4MME'C&$80CVc1Y1N printf0YVnVtVt*4ME"T&C0*Ad1Y1N printf0YVnVtVt---------------Y1N printf0YVnVtVtEnter your option:Y1N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
46/187
scanf0YcY,Htype1Nif0type\>]C] HH type\>]c] HH type\>]*] HH type\>]d]1O
printf0YVnVtVt&nvalid Type:Y1Ngetch01N
exit091NP printf0YVnVtEnter phase type 0( or +1:Y1Nscanf0YdY,Hph1Nif0ph\>( HH ph\>+1O
printf0YVnVt &nvalid phaseY1Ngetch01Nexit091N
Pclrscr01N
printf0YEnter consumer number:Y1Nscanf0YdY,Hcno1N printf0YEnter consumer name:Y1Nfflush0stdin1Ngets0cname1N printf0YEnter pmr and lmr:Y1Nscanf0YddY,Hpmr,Hlmr1Ntu>pmr-lmrNswitch0type1O
case ]d]:case ]*]:
if0tuB>91 bill>09@(.1Nelse if0tuB>(991 bill>09@(.1?0tu-91@).=Nelse if0tuB>)991 bill>09@(.1?09@).=1?0tu-(991@+.;Nelse if 0tuB>+991 bill>09@(.1?09@).=1?0(99@+.;1?0tu-)[email protected] bill>09@(.1?09@).=1?0(99@+.;1?0)[email protected][email protected]>(9.99N pt>[email protected]/Nif0ph>>(1O
if0ptB(9.991 pt>(9.99N
Pelse
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
47/187
Oif0ptB)9.991 pt>)9.99N
P brea!N
case ]c]:case ]C]:if0tuB>(991 bill>0(99@+.;1Nelse bill>0(99@+.;1?0tu-([email protected]>)9.99N pt>[email protected]/Nif0ph>>(1O
if0ptB)9.991
pt>)9.99NPelseO
if0ptB9.991 pt>9.99N
P brea!N
P tamt>bill?se?ptN clrscr01N printf0YVnConsumer Gumber :dY,cno1N printf0YVnConsumer name :sY,cname1N printf0YVnType0c-com,d-dom1 :cY,type1N printf0YVnphase :dY,ph1N printf0YVn#resent Month 'eading :dY,pmr1N printf0YVn8ast Month 'eading :dY,lmr1N printf0YVnTotal nits :dY,tu1N printf0YVn5ill $mount :.)fY,bill1N printf0YVn"ervice Charges :.)fY,se1N printf0YVn#ower tax :.)fY,pt1N printf0YVnTotal $mount :.)fY,tamt1N getch01N
PUNCONDITION& CONTRO ST&TEMENT
/*
-
8/20/2019 Cnotes Full
48/187
0* Continue @"ey wordA:, The continue statement causes control to pass to the end o the innermost enclosingwhile, do or for statement, at which point the loop continuation condition in re-evaluated.
Syntax:- continueN 0#asses control1
1* ;oto:, The UgotoL statement is an unconditional control statement which is used up the execution of program
se%uence by transfer of control to the some other part of the program. Syntax:- goto labelN3here label is C valid identifier used to the label of the destination such that the control could
transferred label. Syntax:- identifierNProgram: *rite a program to displa" natural numbers from one to given natural numbers using MgotoN
statement.
QincludeBstdio.hFQincludeBconio.hFint n,i>(Nclrscr01N printf0DEnter any number:1N
scanf0Dd,Hn1Nlb: printf0DGatural numbers from ( to d,n1N printf0DdVt,i1Ni??Nif0iB>n1goto lbNgetch01N
PProgram:
QincludeBstdio.hFQincludeBconio.hFint nNclrscr01N printf0DEnter any number:1Nscanf0Dd,Hn1Nif0n> >(1
gotot lb(Nif0n> >)1
goto lb)N printf0D3EC4MEVn1Nlb(: printf0D8abel (Vn1Nlb): printf0D8abel )Vn1Ngetch01N
PKDos*HL
De%ay: "uspends execution for interval 0milli seconds1 De%la#atin:- void delay 0unsigned milli seconds1N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
49/187
K!onio*(L
;oto.y:,&t is used to move the cursor to specify the location is text window. Syntax:- void gotoxy0intx, inty1N
Program:
QincludeBstdio.hFQinculdeBconio.hFvoid main01O clrscr01N gotoxy029,)1N printf0D3E8C4ME1N getch01NPProgram:
QincludeBstdio.hF
QincludeBconio.hFvoid main01O
int sno,c,cpp,ava,tot,sc>)+,ec>2,iNchar snameI)9J,chNfloat avgN setcursortype0)1NdoO
clrscr01Ngotoxy0),1N printf0Y---------------------------------Y1Ngotoxy0+9,/1N printf0Y5*#" "4T3$'E 8&M&TE*Y1Ngotoxy0+,21N printf0YS&W$X$3$*$Y1Ngotoxy0),=1N printf0Y---------------------------------Y1Ngotoxy0)+,1N printf0YcY,]`]1Ngotoxy02,1N printf0YcY,]]1Ngotoxy02,))1N printf0YcY,]]1Ngotoxy0)+,))1N printf0YcY,]]1Nfor0i>/NiB>)(Ni??1O
gotoxy0sc,i1N printf0YcY,]]1N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
50/187
Pfor0i>/NiB>)(Ni??1O
gotoxy0ec,i1N printf0YcY,]]1N
Pgotoxy0),;1N printf0Y"G4 :Y1N AA+(gotoxy0),((1N printf0Y"G$ME :Y1Ngotoxy0),(+1N printf0YM$'K" &G C :Y1N AA +;gotoxy0),(1N printf0YM$'K" &G C## :Y1Ngotoxy0),(21N printf0YM$'K" &G W$S$ :Y1N
gotoxy0++,(;1N printf0YT4T$8 :Y1Ngotoxy0++,)(1N printf0Y$SE'$ c ? cpp ? avaNavg > 0float1 totA+Ngotoxy0,(;1N printf0YdY,tot1Ngotoxy0,)(1N printf0Y.)fY,avg1Ngotoxy0),)+1N printf0Y*o you enter another record0yAn1:Y1Nfflush0stdin1Nscanf0YcY,Hch1N
Pwhile0ch\>]n]1N}
OOP CONTRO ST&TEMENTS
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
51/187
oop:, The process of repeated executing a bloc! of statements is called loop. C supports three types of loopingstatements. They areR
3hile 8oop do-while loop and for-loop
$ny loop has three things. They areR(. &nitiali6e the index). Test condition+. pdate the index
/* w(i%e %oop:, &t is a conditional control loop statement in C language. Syntax:- while0test condition1
O "tatementsN P
irst the test condition is evaluated and if it is true then the statement bloc! will be executed. $fter the
execution of statements the test condition is evaluated once again.Then if it is true the statement bloc! will be executed continuous until the test condition finally becomes false.
Program: *rite a program to displa" natural numbers from / to given number using
]3hile 8oop]QincludeBstdio.hFQincludeBconio.hFvoid main01O
int n,i>(Nclrscr01N
printf0YEnter no of elements:Y1Nscanf0YdY,Hn1N printf0YGatural Gumbers from ( to d:VnY,n1Nwhile 0iB>n1O
printf0YdVtY,i1Ni??N
Pgetch01N
}
Program: *rite a program to chec9 whether the given number is prime or not
QincludeBstdio.hFQincludeBconio.hFvoid main01O
int n,i>(,count>9Nclrscr01N printf0YEnter any number:Y1Nscanf0YdY,Hn1N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
Entry
True
TestConditi
on
"tatements
Gext "tatements
alse
-
8/20/2019 Cnotes Full
52/187
while0iB>n1Oif0ni>>91count??Ni??N
Pif0count>>)1 printf0Yrev@(9?0n(91Nn>nA(9N
P printf0Y'everse number>luY,rev1Ngetch01N
PProgram: *rite a program to accept an" number and chec9 whether the given number is palindrome number
QincludeBstdio.hFQincludeBconio.hFvoid main01O
int n,m,rev>9Nclrscr01N printf0YEnter any number:Y1Nscanf0YdY,Hn1Nm>nNwhile0mF91O
rev>rev@(9?0m(91Nm>mA(9N
Pif0n>>rev1
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
53/187
printf0Ysum?0n(91Nn>nA(9N AAn>()+,nF9,
P AA sum>;?(,s>(9,n>9 printf0Y"um of digits in the given number:dY,sum1Ngetch01N
}
Program:
QincludeBstdio.hFQincludeBconio.hFvoid main01O
int n,i>(Nclrscr01N printf0YEnter any number:Y1Nscanf0YdY,Hn1N printf0Yactors of d:Y,n1Nwhile0iB>n1O
if0ni>>91 printf0YdVtY,i1Ni??N
P
getch01NPProgram:
QincludeBstdio.hFQincludeBconio.hFvoid main01O
int n,i>(N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
54/187
unsigned long f>(Nclrscr01N printf0YEnter any number:Y1Nscanf0YdY,Hn1N printf0Yactorial of d is lu:Y,n,f1N
while 0iB>n1O f>f@iN i??N printf0YdVtY,f1NPgetch01N
}
Program: *rite a program to displa" even numbers and odd numbers from / to given number QincludeBstdio.hF QincludeBconio.hF void main01 O
int n,i>(Nclrscr01N printf0YEnter any number:Y1Nscanf0YdY,Hn1N printf0YEven numbers from ( to d:Y,n1Nwhile 0iB>n1O
if0i)>>91 printf0YdVtY,i1Ni??N
Pi>(N printf0YVn4dd numbers from ( to d:Y,n1Nwhile0iB>n1O
if0i)>>(1 printf0YdVtY,i1Ni??N
Pgetch01N
}
Program: *rite a program to count no of digits in the given number QincludeBstdio.hFQincludeBconio.hFvoid main01O
unsigned long nNint nd>9Nclrscr01N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
55/187
printf0YEnter any number:Y1Nscanf0YluY,Hn1Nwhile0nF91Ond??N
n>nA(9NP printf0YGo of digits in the given number is dY,nd1Ngetch01N
P
Program:
QincludeBstdio.hFQincludeBconio.hFvoid main01O
int i>9Nclrscr01Nwhile0iB(991O
i??Nif0i>>9 [[ i>>9 [[ i>>/91continueN printf0YdVtY,i1N
Pgetch01N
P
Program:
QincludeBstdio.hFQincludeBconio.hFvoid main01O
int n,m,sum>9,rNclrscr01N printf0YEnter any number:Y1N AAn>(+scanf0YdY,Hn1Nm>nNwhile0mF91O
r>m(9Nsum>sum?0r@r@r1Nm>mA(9N
Pif0n>>sum1 printf0Y
-
8/20/2019 Cnotes Full
56/187
else printf0YaBb^a:bNfor0i>(NiB>mNi??1O
if0ai>>9 HH bi>>91g>iN
P
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
57/187
printf0YaFb^a:bNwhile0(1O
if0lcma>>9 HH lcmb>>91
brea!Nlcm??NP printf0Ylcm of d and d is dY,lcm1Ngetch01N
}
Program: Method (QincludeBstdio.hFQincludeBconio.hFvoid main01O
unsigned long nNint sum>9Nclrscr01N printf0YEnter any number:Y1Nscanf0YluY,Hn1Nwhile0nF91O
sum>sum?0n(91Nn>nA(9Nif0n>>91O if0sumF;1
O printf0YdVnY,sum1Nn>sumNsum>9N
PP
P printf0Yinal sum is:dY,sum1N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
58/187
getch01NPMet(od,0
QincludeBstdio.hFQincludeBconio.hF
void main01Ounsigned long nNint sum>9Nclrscr01N printf0YEnter any number:Y1Nscanf0YdY,Hn1Nsum>n;Nif0sum>>91 printf0Y"um is dY,;1Nelse
printf0Y"um is dY,sum1Ngetch01N}
Program: *rite a program to accept an" decimal number and displa"s binar" number QincludeBstdio.hFQincludeBconio.hFvoid main01O
int nNunsigned long bin>9,b>(Nclrscr01N printf0YEnter any number:Y1Nscanf0YdY,Hn1Nwhile0nF91O
bin>bin?0n)1@bNn>nA)N b>b@(9N
P printf0Y5inary number of given decimal number:luY,bin1Ngetch01N
PProgram: *rite a program to accept an" binar" number and displa" decimal number
QincludeBstdio.hFQincludeBconio.hFvoid main01O
int n>9,b>(,rNunsigned long biaNclrscr01N printf0YEnter any binary number:Y1N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
59/187
scanf0YluY,Hbia1Nwhile0biaF91O
r>bia(9Nif0r\>( HH r\>91
O printf0Y&nvalid binary number:Y1Ngetch01Nexit091N
Pn>n?r@bN bia>biaA(9N b>b@)N
P printf0Y*ecimal number of given binary number :dY,n1Ngetch01N
}
0* do,w(i%e %oop:, &t is an alternative form of while loop. The only difference between while and do while isthe minimum number of execution of UwhileL is U9L. or minimum no of execution of Udo-whileL is U(.
Syntax:- do O "tatementsN P 3hile0test condition1N
)%ow!(art
irst the statement bloc! will be executed and then test condition will be evaluated. &f the condition istrue then the statement bloc! will be executed once again. This process of repeated executed continuous untithe test condition finally becomes falseProgram: *rite a program to accept and displa" natural numbers% from one to given number using do)while
loop
QincludeBstdio.hFQincludeBconio.hF
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
Entry
True
"tatements
Testconditon
Gext "tatements
-
8/20/2019 Cnotes Full
60/187
void main01O
int n,i>(Nclrscr01N printf0YEnter any number:Y1N
scanf0YdY,Hn1N printf0YGatural numbers form ( to d:VnY,n1NdoO
printf0YdVtY,i1Ni??N
Pwhile0iB>n1Ngetch01N
P
1* for %oop:, &t is the most commonly used loop statement in C language. &t consisting of three expressions. Syntax:- for0exp(N exp)N exp+1 O
"tatementsNP
The first expression is used to the initiali6e the index, second expression is used to chec! whether theloop is to be continued or not, third expression is used to change the index for further iteration 0increment ordecrement1.
Program: *rite a program to accept an" number and chec9 whether the given number is perfect or not.
sum of factors e'cept that number is called perfect number
QincludeBstdio.hFQincludeBconio.hF
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
Entry
alse
True
&nitiali6e the &ndex
TestConditionExpression
)
"tatements Gext "tatements
-
8/20/2019 Cnotes Full
61/187
void main01O
int n,sum>9,iNclrscr01N printf0YEnter any number:Y1N
scanf0YdY,Hn1Nfor0i>(NiB>nNi??1O
if0ni>>91sum>sum?iN
Pif0n>>sum1
printf0Y(NiB>nNi??1O
m>iNrev>9Nwhile0mF91O
rev>rev@(9?0m(91N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
62/187
m>mA(9NPif0rev>>i1 printf0YdVtY,i1N
P
getch01NPProgram: *rite a program to displa" prime numbers from / to given numbers
QincludeBstdio.hFQincludeBconio.hFvoid main01O
int n,i,,cNclrscr01N printf0YEnter any number:Y1Nscanf0YdY,Hn1N
printf0Y#rime Gumbers from ( to d:Y,n1Nfor0i>(NiB>nNi??1O
c>9Nfor0>(NB>iN??1O
if0i>>91c>c??N
Pif0c>>)1 printf0YdVtY,i1N
Pgetch01N
PProgram: *rite a program to displa" perfect numbers from / to given numbers.
QincludeBstdio.hFQincludeBconio.hFvoid main01O
int n,,i,sumNclrscr01N printf0YEnter any number:Y1Nscanf0YdY,Hn1N printf0Y#erfect number from ( to d:VtY,n1Nfor0i>(NiB>nNi??1O
sum>9Nfor0>(NB>iA)N??1O
if0i>>91
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
63/187
sum>sum?NPif0sum>>i1 printf0YdVtY,i1N
P
getch01NPProgram: *rite a program to displa" rmstrong numbers from / to given numbers.
QincludeBstdio.hFQincludeBconio.hFvoid main01O
int n,m,i,r,sumNclrscr01N printf0YEnter any number:Y1Nscanf0YdY,Hn1N
printf0Y$rmstrong numbers from ( to d:Y,n1Nfor0i>(NiB>nNi??1O
m>iNsum>9Nwhile0mF91O
r>m(9Nsum>sum?0r@r@r1Nm>mA(9N
Pif0sum>>i1 printf0YdVtY,i1N
Pgetch01N
PProgram: *rite a program to accept an" number calculate and displa" sQuare root of a given number withoutusing librar" functions.
QincludeBstdio.hFQincludeBconio.hFvoid main01O
int n,iNfloat Nclrscr01N printf0YEnter any number:Y1Nscanf0YdY,Hn1Ni>(Nwhile0i@i B n1O
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
64/187
i??NPif0i@i >> n1 printf0Y"%uare root of given number is dY,i1Nelse
O >i-(Nwhile0@Bn1O
>?9.9999(NP printf0Y"%uare root of given number:.+fY,1N
Pgetch01N
PProgram: *rite a program to enter base and power calculate and displa" e'ponent ional value.
QincludeBstdio.hFQincludeBconio.hFvoid main01O
int b,p,iNfloat e>(Nclrscr01N printf0YEnter base and power:Y1Nscanf0YddY,Hb,Hp1Nif0pF91O
for0i>(NiB>pNi??1O
e>e@bNP
Pif0pB91O
for0i>pNiB9Ni??1O
e>e@(AbNP
P printf0YExponential of d and d is :.+fY,b,p,e1Ngetch01N
}
)ormatting Out put
Int:,
Int n/88
#rintf0Dd,n1 or printf0Dd,n1N or printf0D-/d,n1N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
65/187
oAp:(99 oAp:--(99 0)spaces1 oAp:(99--- 0+ spaces1C(ar:,
C(ar !(H
#rintf0Dc,ch1N printf0D+c,ch1N printf0D-c,ch1N
oAp:7 oAp:--7 0)spaces1 oAp: 7---- 0 spaces1
)%oat:,
f%oat ft/01*613
#rintf0Df,ft1N printf0D()f,ft1N printf0D.)f,ft1NoAp:()+.2+999 oAp:--()+.2+999 0)spaces1 oAp: ()+.2+
printf0D=.)f,ft1N oAp:--()+.2+
String:,
string st 9ECOME
printf0Ds,st1N printf0D;s,st1N printf0D.+s,st1NoAp3E8C4ME oAp--3E8C4ME0)spaces1 oAp3E8
int n>+ pirntf0D.@s,n,st1NoAp:3E8
Program: 3rite a program to generate the following pattern 33E3E8
3E8C3E8C43E8C4M3E8C4ME
QincludeBstdio.hFQincludeBconio.hFvoid main01O
char stI(9J>Y3E8C4MEYNint iNclrscr01N
for0i>(NiB>2Ni??1O printf0YVn.@sY,i,st1NPgetch01N
PProgram:
QincludeBstdio.hF
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
66/187
QincludeBconio.hFvoid main01O
int n,iNclrscr01N
printf0YEnter any number:Y1Nscanf0YdY,Hn1Nfor0i>(NiB>)9Ni??1O printf0YVnd+c+d+cdY,n,]@],i,]>],n@i1NPgetch01N
PProgram: 3rite a program to enter any number and display mathematical table from one to givennumbers0each screen contains + tables1QincludeBstdio.hF
QincludeBconio.hFvoid main01O
int i,,n,r>(,c>(Nclrscr01N printf0YEnter any number:Y1Nscanf0YdY,Hn1Nclrscr01Nfor0i>(NiB>nNi??1Ofor0>(NB>)9N??1Ogotoxy0c,r1N printf0Yd+c+d+cdY,i,]@],,]>],i@1Nr??NPif0rF)91O r>(Nc>c?)=NPif0i+>>91O printf0YVnVn#ress any !ey to continue....Y1Ngetch01Nclrscr01Nc>(Nr>(NPP
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
67/187
getch01NPProgram: 3rite a program to generate the following pattern @ @ @ @ @
@ @ @ @ @@ @ @ @ @
@ @ @ @ @QincludeBstdio.hF @ @ @ @ @QincludeBconio.hFvoid main01O int n,i,N clrscr01N printf0YEnter any number:Y1N scanf0YdY,Hn1N for0i>(NiB>nNi??1 O
for0>(NB>nN??1O printf0Y+cY,]@]1NP printf0YVnVnY1N
P getch01NPProgram: 3rite a program to generate the following pattern (
) )+ + +
QincludeBstdio.hFQincludeBconio.hFvoid main01O
int n,i,Nclrscr01N printf0YEnter any number:Y1Nscanf0YdY,Hn1Nfor0i>(NiB>nNi??1Ofor0>(NB>iN??1O printf0Y+dY,i1NP printf0YVnVnY1NPgetch01N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
68/187
PProgram: 3rite a program to generate there following pattern(()()+
()+()+QincludeBstdio.hFQincludeBconio.hFvoid main01O
int n,i,Nclrscr01N printf0YEnter any number:Y1Nscanf0YdY,Hn1Nfor0i>(NiB>nNi??1
O for0>(NB>iN??1O
printf0Y+dY,1NP printf0YVnVnY1N
Pgetch01N
PProgram: 3rite a program to generate the following pattern
@ @ @ @ @ @
@ @ @ @ @ @ @ @ @QincludeBstdio.hFQincludeBconio.hFvoid main01O
int n,i,,sNclrscr01N printf0YEnter any number:Y1Nscanf0YdY,Hn1Ns>n@)Nfor0i>(NiB>nNi??1O
printf0YcY,s,+)1Nfor0>(NB>iN??1O
printf0YcY,]@]1N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
69/187
P
printf0YVnY1Ns>s-)N
Pgetch01N
PProgram: 3rite a program to generate the #ascal triangle of n rows using functionsQincludeBstdio.hFQincludeBconio.hFvoid main01O
unsigned long fact0int1Nint n,ncr,i,,sN
clrscr01N printf0YEnter any value into n:Y1Nscanf0YdY,Hn1Ns>n@+Nfor0i>9NiB>nNi??1O
printf0Y@cY,s,+)1Nfor0>9NB>iN??1O
ncr>fact0i1Afact01@fact0i-1N printf0Y/dY,ncr1N
P printf0YVnY1Ns>s-+N
Pgetch01N
Punsigned long fact0int x1O
unsigned long f>(Nint iNfor0i>(NiB>xNi??1O
f>f@iNPreturn fN
PProgram:
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
70/187
QincludeBstdio.hFQincludeBconio.hFvoid main01O
int n,m,i,,!,s>)N
clrscr01N printf0YEnter any number into n:Y1Nscanf0YdY,Hn1Nclrscr01Nfor0i>nNiF>(Ni--1O
!>iN printf0Y@cY,s,+)1Nfor0>(NB)@iN??1O
printf0Y)dY,!1N
if0Bi1!--Nelse!??N
P printf0YVnY1Ns>s?)N
Pgetch01N
P
Program:
QincludeBstdio.hFQincludeBconio.hFvoid main01O
int i,,!>(Nclrscr01N
printf0YEnter any number:Y1Nscanf0YdY,Hn1Nfor0i>(NiB>nNi??1O
for0>(NB>nN??1O
printf0Y+dY,!1Nif0Bn1O
if0i)>>91!--Nelse
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
71/187
PP
PP
Program:QincludeBstdio.hFQincludeBconio.hFvoid main01O
int n,m,i,,!,sNclrscr01N printf0YEnter any value into n:Y1Nscanf0YdY,Hn1Ns>)@nNm>(N
for0i>(NiB)@nNi??1O!>(N printf0Y@cY,s,+)1Nfor0>(NB)@mN??1OP
P}
Program:
QincludeBstdio.hFQincludeBconio.hFvoid main01O
int n,m,i,,!,s>)Nclrscr01N printf0YEnter any vlaue of n:Y1Nscanf0YdY,Hn1Nm>nNfor0i>(NiB)@nNi??1O
!>mN printf0Y@cY,s,+)1N
for0>(NB)@mN??1O
printf0Y)dY,!1Nif0Bm1!--Nelse!??N
P
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
72/187
printf0YVnY1Nif0iBn1O
s>s?)Nm--N
PelseO
s>s-)Nm??N
PP
}
Program:
QincludeBstdio.hFQincludeBconio.hFvoid main01O
int i,,p,x>2,y>2Nclrscr01Ntextcolor0(?58&GK1Nfor0i>(NiB>(+Ni??1O
p>/Nfor0>(NB>(+N??1O
if0iF( HH 0F>x HH B>y11cprintf0Y)cY,]@]1Nelse printf0Y)cY,p1Nif0B21 p??Nelse p--N
P printf0YVnY1Nif0iF(1O
if0iB21
Ox--Ny??N
PelseO
x??Ny--N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
73/187
PP
Pgetch01N
P
Program:
QincludeBstdio.hFQincludeBconio.hFvoid main01O
int i,,p,x>2,y>2Nclrscr01Nfor0i>(NiB>2Ni??1O
p>/Nfor0>(NB>(+N??1O
if0iF( HH 0F>x HH B>y11 printf0Y)cY,+)1Nelse printf0Y)cY,p1Nif0B21 p??Nelse p--N
P printf0YVnY1Nif0iF(1O
x--Ny??N
PPgetch01N
P
Te.t Co%or:, &t selects a new character color in textmode. Declaration#) void textcolor0int new color1N
Te.t
-
8/20/2019 Cnotes Full
74/187
( 58$CK 9 XE" XE") 58E ( XE" XE"+
-
8/20/2019 Cnotes Full
75/187
getch01NP
KDOS*HL
De%ay: "uspends execution for interval 0milli seconds1 Declaration# void delay 0unsigned milliseconds1N
"'(it:, &t chec!s for currently available !eystro!es. Declaration# int !bhit01N
DI;IT& COC>
QincludeBstdio.hFQincludeBconio.hFQincludeBdos.hFvoid main01O
int h,m,sNclrscr01N
printf0YEnter any time0hh:mm:ss1:Y1Nscanf0Yd:d:dY,Hh,Hm,Hs1Ntextmode091Ntextcolor0'E*1N setcursortype0G4C'"4'1Nwhile0\!bhit011Oclrscr01Ngotoxy0(,()1N cprintf0Y.)d:.)d:.)dY,h,m,s1Ndelay0(991Ns??Nif0sF;1Om??Ns>(NPif0mF;1Oh??Nm>9NPif0hF()1h>(NP
P
Program:
QincludeBstdio.hFQincludeBconio.hFQincludeBdos.hF
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
76/187
void main01O
int c>(,c(>2,r>(Ntextmode0)1N setcursortype091N
textcolor0'E*1Nwhile0\!bhit011O
clrscr01Ngotoxy0+2,r1Ncprintf0Y84SE"Y1Ngotoxy0c,()1Ncprintf0Y#'&X$Y1Ngotoxy0c(,()1Ncprintf0YM$*7Y1Nc>c?)N
c(>c(-)Nr>r?(Nif0cF21O c>(N c(>2NPif0rF)1r>(Ndelay0=91N
PPProgram: 3rite a program to get the following outputQincludeBstdio.hFQincludeBconio.hFvoid main01O
int n,i,Nclrscr01N printf0YEnter any value:Y1Nscanf0YdY,Hn1Nfor0i>(NiB>nNi??1O
for0>(NB>iN??1O
printf0Y+cY,]@]1NP printf0YVnY1N
Pgetch01N
P
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
- -
- - -
- - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
-
8/20/2019 Cnotes Full
77/187
Program: 3rite a program to generate the following patternQincludeBstdio.hFQincludeBconio.hFvoid main01O
int n,i,N clrscr01N printf0YEnter any number:Y1N scanf0YdY,Hn1N for0i>(NiB>nNi??1Ofor0>(NB>nN??1O printf0Y+cY,]@]1NP printf0YVnVnY1N
P getch01NPProgram: 3rite a program to generate the following patternQincludeBstdio.hFQincludeBconio.hFvoid main01Oint n,i,Nclrscr01N printf0YEnter any number:Y1Nscanf0YdY,Hn1Nfor0i>(NiB>nNi??1Ofor0>(NB>iN??1O printf0Y+dY,i1NP printf0YVnVnY1NPgetch01N}
)UNCTIONS
)un!tion:, &t is a self contained bloc! of statements and it can be used at several multiple times in a program but defined only once.
i'rary fun!tion or Predefined fun!tions:, The functions which are in built with the C-compiler is called aslibrary functions. Ex:- printf, scanf, getch01, clrscr01N etc.,
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
() )+ + +
-
8/20/2019 Cnotes Full
78/187
User defined fun!tion:, ser can defined functions to do a tas! relevant to their programs. "uch functions arecalled as user defined functions.
$ny function has three things. They areR(. unction declaration
). unction definition+. unction calling.
&n case of pre,defined fun!tions the function declaration is in header files, definition is in C Gibrariesand calling is in your source program.
5ut in case of user defined fun!tions all the three things are in your source program.
)un!tion de!%aration:,
Syntax:- 'eturntype funcname0I$rg 8istJ1N
Example:-Soid test01N&nt sum0int , int 1N
)un!tion definition:,
Syntax:- returntype funcname0I$rg 8istJ1 O 5odyN P
)un!tion !a%%ing:,
Syntax:- funcname0I$rg 8istJ1N
The arguments which we given at the time of function declaration or definition are called arguments orforma% arguments*
The arguments which are given at the time of function calling are called a!tua% arguments orparameters.
void:- 0Empty data type1QincludeBstdio.hFQincludeBconio.hFvoid main01O void test0 1N A@declaration@A clrscr01N test0 1N A@calling@A getch01NPvoid test01 A@definition@AO printf0Dwelcome to c functions:1N P
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
79/187
R&le$ '# %#eatin( an) a%%e$$in( &$e# )e'ine) '&n%tin$:,
(. $ function can be called by any number of times.). $ function may or may not receive arguments.+. $ function may or may not return a value.
. &f a function does not return any value the function return data type will be specified as Dvoid. &f a function returns a value only one value can be returned./. 3e cannot specify any return data type, the function returns on integer value by default.2. The returning value must be return with a statement Dreturn.=. &f a function returns a value, the execution of return statement should be last;. &f a function returns a value, the returning value should match with the function return data type.(9. $ function is executed when a function is call by its name.((. 5efore a function call, function declaration or definition must and should(). $ function definition may be placed before or after the main function.(+. &f a function call, function definition can be specified any where in the program(. &f a function definition is specified before the function called then the function declaration is not
necessary.(. The function definition should not be terminated with semicolon0 N 1
Return:, Exits immediately from the currently execution of function to the calling rotated optionally returninga value.
Syntax:- return IBexpressionFJNProgram:
QincludeBstdio.hFQincludeBconio.hFvoid main01O int sum0int,int1N int a,b,nN clrscr01N printf0YEnter any two values:Y1N scanf0YddY,Ha,Hb1N n>sum0a,b1N printf0Y"um is dY,n1N getch01N P int sum0int x,int y1 O
return x?yN P
"&n%tin P#ttype$ an) %ate(#y ' '&n%tin$#
$ unction depending and whether arguments are present or not and a value is returned or not, may belong to one of the following categories.
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
80/187
Category (: unction with no arguments and no return valueCategory ): unction with arguments and no return valueCategory +: unction with arguments and return value.Category : unction with no arguments and return value.*+ "&n%tin it n a#(&ment$ an) n #etn .al&e:- 3hen a function has no arguments it does not receive
any data from the calling function. "imilarly when it does not return a value the calling function does notreceive any data from the called function. &n affect there is no data transfer between the calling function and thecalled function.Program:
QincludeBstdio.hFQincludeBconio.hFvoid main01O void sum01N A@declarationN@A clrscr01N sum01N A@callingN@A
getch01N P void sum01 A@ definition@A O
int a,bN printf0YEnter any two values:Y1Nscanf0YddY,Ha,Hb1N printf0Y"um is dVnY,a?b1N
P
!+"&n%tin it a#(&ment$ an) n #etn .al&e:- &n this type the function has some arguments, it receive datafrom the calling function but it does not return any value. The calling function does not receive data from thecalled function. "o there is one way data communication calling function and the called function.
Program:
QincludeBstdio.hFQincludeBconio.hFvoid main01O void sum0int, int1N A@declarationN@Aint a,bNclrscr01N printf0YEnter any two values:Y1Nscanf0YddY,Ha,Hb1Nsum0a,b1N A@callingN@Agetch01N P void sum0int x, int y1 A@ definition@A O
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
81/187
printf0Y"um is dVnY,x?y1N P
/+"&n%tin it a#(&ment$ an) #etn .al&e#) &n this type the function has some arguments. &t receives datafrom the calling function. "imilarly it returns a value. The calling function receive data from the called
function. "o it is a two way data communication between the calling function and the called function.Program:QincludeBstdio.hFQincludeBconio.hFvoid main01O int sum0int,int1N int a,b,nN clrscr01N printf0YEnter any two values:Y1N scanf0YddY,Ha,Hb1N
n>sum0a,b1N printf0Y"um is dY,n1N getch01N P int sum0int x,int y1 O
return x?yN P
0+ "&n%tin it n a#(&ment$ an) #etn .al&e:, &n this type the function has no arguments. &t does notreceive data from the calling function. 5ut it returns a value the calling function receive data from the calledfunction. "o it is a one way data communication between called function and the calling function.Program:
QincludeBstdio.hFQincludeBconio.hFvoid main01O int sum01N A@declarationN@A int sN clrscr01N s>sum01N A@callingN@A printf0Y"um is dY,s1N getch01N P int sum01 A@ definition@A O
int a,bN printf0YEnter any two values:Y1Nscanf0YddY,Ha,Hb1Nreturn a?bN
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
82/187
P
Program: *rite a program to displa" natural numbers from one to given number b" using user defined functions
QincludeBstdio.hF
QincludeBconio.hFvoid main01O void disp0int1N int nN clrscr01N printf0YEnter any number:Y1N scanf0YdY,Hn1N disp0n1N getch01N P
void disp0int x1O int iN for0i>(NiB>xNi??1 printf0YdVtY,i1N PProgram: *rite a program to displa" factors of given number QincludeBstdio.hFQincludeBconio.hFvoid main01O void disp0int1N int nN clrscr01N printf0YEnter any number:Y1N scanf0YdY,Hn1N disp0n1N getch01N P void disp0int x1O int iN for0i>(NiB>xNi??1 O if0xi>>91 printf0YdVtY,i1N PPProgram: *rite a program to calculate and displa" reverse number of given number .QincludeBstdio.hF
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
83/187
QincludeBconio.hFvoid main01O unsigned long revnum0int1N int nN
unsigned long rN clrscr01N printf0YEnter any number:Y1N scanf0YdY,Hn1N r>revnum0n1N printf0Yreverse number is:luY,r1N getch01N P unsigned long revnum0int x1while0xF91O
rev>rev@(9?0x(91N x>xA(9N P return revN P
Program: *rite a program to calculate factorial of given number QincludeBstdio.hFQincludeBconio.hFvoid main01O unsigned long fact0int1N int nN unsigned long fN clrscr01N printf0YEnter any number:Y1N scanf0YdY,Hn1N f>fact0n1N printf0Yactorial of given number:luY,f1N getch01N P unsigned long fact0int x1O unsigned long f>(N while0xF>(1 O f>f@xN x--N P return fN
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
84/187
P
Program: *rite a program to accept an" number and displa" sum of natural numbers using user defined function
QincludeBstdio.hF
QincludeBconio.hFvoid main01O void sum0int1N int nN unsigned long fN clrscr01N printf0YEnter any number:Y1N scanf0YdY,Hn1N sum0n1N getch01N
P void sum0int x1O int i>9,sum>9N while0iB>x1 O sum>sum?iN i??N P printf0Y"um is dY,sum1N P
Program: *rite a program to displa" the Eibonacci series of n terms using functionsQincludeBstdio.hFQincludeBconio.hFvoid main01O
void fibo01N AAfun declarationNclrscr01Nfibo01Ngetch01N AAfun callingN
Pvoid fibo01 AAfun definitionO
int n,t,t(>9,t)>(,iNclrscr01N printf0YEnter no of terms:Y1Nscanf0YdY,Hn1N printf0Yibonacci series:Y1N printf0YdVtdY,t(,t)1N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
85/187
for0i>(NiB>n-)Ni??1O
t>t(?t)N printf0YVtdY,t1Nt(>t)N
t)>tNP
P
Storage C%asses
The scope and lifetime of variables in functions:-Sariables in C different behavior from those in most other
language. or example in a basic program a variable returnsits value through out the program. &t is not always the casein UCL. &t always depends on the storage classes the variablemay assume.
$ variable in C can have any one of the four storage
classes.
(.$utomatic variables)."tatic Sariables+.External Sariables or
-
8/20/2019 Cnotes Full
86/187
"torage Main Memory*efault value
-
8/20/2019 Cnotes Full
87/187
"cope 8ocal to the bloc! in which it is defined.8ife $ Salue of static variable consists
between differentfunction calls. 0Go Changes1. That means itcannot reali6ed between different functioncalls.
Keyword static
Program:QincludeBstdio.hFQincludeBconio.hFvoid main01O static int a,bN clrscr01N printf0YVn a>dY,a1N printf0YVn b>dY,b1N getch01N P
+. Exte#nal 1a#ia2le$ # Gl2al 1a#ia2le$:- Thevariables both alive and active throughout entire
program is !nown as external variable."torage Main Memory*efault value ero
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
88/187
"cope global8ife as long as the program execution does
not come to endKeyword extern
Program:/
QincludeBstdio.hFQincludeBconio.hF
int a,bNvoid main01O clrscr01N printf0YVn a>dY,a1N printf0YVn b>dY,b1N getch01N P
Program:0
QincludeBstdio.hFQincludeBconio.hFint a>(99Nvoid main01O extern int aN clrscr01N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
89/187
printf0YVn a>dY,a1N getch01N P
. Re(i$te# 1a#ia2le$:- 3e can use register variables forfre%uently used variables the fast execution of a
program. These variables are declared inside thefunction bloc!.
"torage C#, 'egister *efault value garbage value"cope local to the bloc! in which it is defined8ife the control remains within the bloc! in
which it is defined
Keyword
'egister
Program:
QincludeBstdio.hFQincludeBconio.hF
void main01O register int a,bN clrscr01N printf0YVn a>dY,a1N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
90/187
printf0YVn b>dY,b1N getch01N PNote:, &f there is no storage classes specifier before thefunction declaration of variables inside the function bloc! bydefault it ta!es auto storage class.
Re!ursi$e fun!tion:, Calling a function with the same namefunction definition is !nown as recursive function. &f youwant to wor! with recursive function we must follow twoconditions.
(.Calling itself ).Termination condition
Program: 3rite a program to display natural numbers from
( to given number using recursive functionQincludeBstdio.hFQincludeBconio.hFvoid main01O void disp0int1N
int nN clrscr01N printf0YEnter any number:Y1N scanf0YdY,Hn1N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
91/187
disp0n1N getch01N P void disp0int x1O if0xF(1 disp0x-(1N printf0YdVtY,x1N P
Program: 3rite a program to display natural numbers fromone to given number using recursive function callingmain with in main.
QincludeBstdio.hFQincludeBconio.hF
void main01O
"tatic int i>(NnN&f0i>>(1O
Clrscr01N
#rintf0DEnter any number:1N"canf0Dd,Hn1N#rintf0DGatural numbers from ( to d:,n1N
P
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
92/187
#rintf0DdVt,i1Ni??Nif0iB>n1main01Ngetch01Nexit091N
PProgram: 3rite a program to calculate and display factorial of given number QincludeBstdio.hFQincludeBconio.hFvoid main01O unsigned long fact0int1N int nN unsigned long fN clrscr01N printf0YEnter any number:Y1N scanf0YdY,Hn1N f>fact0n1N printf0Yactorial is:luY,f1N getch01N P unsigned long fact0int x1O if0xB>(1 return (N else return x@fact0x-(1N PProgram: 3rite a program to display reverse number of given number QincludeBstdio.hFQincludeBconio.hFvoid main01O unsigned long revnum0int1N int nN unsigned long rN clrscr01N printf0YEnter any number:Y1N scanf0YdY,Hn1N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
93/187
r>revnum0n1N printf0Yreverse number is:luY,r1N getch01N P unsigned long revnum0int x1
O static unsigned long rev>9N rev>rev@(9?0x(91N x>xA(9N if0xF91 revnum0x1N return revN P
M&TH*H )UNCTIONS
a's@A:, abs 0a macro1 gets the absolute value of an integer Syntax:- int abs0int x1N
Program:QincludeBstdio.hFQincludeBconio.hFQincludeBmath.hFvoid main01O
int n,aNclrscr01N printf0YEnter any two numbers:Y1Nscanf0YdY,Hn1Na>abs0n1N printf0Y$bsolute value:dY,a1Ngetch01N
Psrt@A:,&t calculates the s%uare root of the given number.
Syntax:- double s%rt0double x1NProgram:
QincludeBstdio.hFQincludeBconio.hFQincludeBmath.hFvoid main01O
int nNdouble sNclrscr01N printf0YEnter any number:Y1Nscanf0YdY,Hn1Ns>s%rt0n1N printf0Y"%uare root of given number:.)lfY,s1Ngetch01N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
94/187
Ppow@A:, &t calculates the exponential value of given base and power.
Syntax:- double pow0double x, double y1NProgram:
QincludeBstdio.hF
QincludeBconio.hFQincludeBmath.hFvoid main01O
int b,pNdouble eNclrscr01N printf0YEnter any two value into b and p is:Y1Nscanf0YddY,Hb,Hp1Ne>pow0b,p1N printf0YExponential value of d and d is .)lfY,b,p,e1N
getch01NPf%oor@A:, &t rounds down the given value. Syntax:- double floor0double x1N!ei%@A:, &t rounds up the given value. Syntax:- double ceil0double x1NProgram:
QincludeBstdio.hFQincludeBconio.hFQincludeBmath.hFvoid main01O
double n,ce,flNclrscr01N printf0YEnter any value:Y1Nscanf0YlfY,Hn1Nce>ceil0n1Nfl>floor0n1N printf0YVnCeil value:.)lfY,ce1N printf0YVnloor value:.)lfY,fl1Ngetch01N
PProgram:-
QincludeBstdio.hFQincludeBconio.hFQincludeBmath.hFvoid main01O
int a,b,c,d,max,minNclrscr01N printf0YEnter any two values:Y1Nscanf0YddY,Ha,Hb1N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
95/187
c>a?bNd>abs0a-b1Nmax>0c?d1A)Nmin>0c-d1A)N printf0YVnMaximum value:dY,max1N
printf0YVnMinimum value:dY,min1Ngetch01NPProgram: *rite a program to calculate and displa" area of triangle with the given three sides
Note:,$ny two angles greater than e%ual to third angle Got form:- +,,2 because +?>2
QincludeBstdio.hFQincludeBconio.hFQincludeBmath.hFQincludeBprocess.hFvoid main01
O float a,b,c,s,areaNclrscr01N printf0YEnter three sides of triangle:Y1Nscanf0YfffY,Ha,Hb,Hc1Nif00a?b1B>c [[ 0b?c1B>a [[ 0c?a1B>b1O
printf0Ynable to form triangleY1Ngetch01Nexit091N
Ps>0a?b?c1A)Narea>s%rt0s@0s-a1@0s-b1@0s-c11N printf0Y$rea > .+fs%unitsY,area1Ngetch01N
PProgram: *rite a program to enter principle amount% time% and rate of interest calculate and
displa" total amount% with compound interest
t>p@0(?rA(91power of nQincludeBstdio.hFQincludeBconio.hFQincludeBmath.hFvoid main01O
int m,y,nNfloat pr,ta,rNclrscr01N printf0YEnter principle amount:Y1Nscanf0YfY,Hpr1N printf0YEnter rate of interest:Y1N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
96/187
scanf0YfY,Hr1N printf0YEnter time 0Xear H Month1:Y1Nscanf0YddY,Hy,Hm1Nn>y@()?mNta>pr@pow0(?rA(99,n1N
printf0YTotal amount with compound interest:.)fY,ta1Ngetch01NPProgram: *rite a program to compute the following series
(?(A)?(A+?----------?(AnQincludeBstdio.hFQincludeBconio.hFQincludeBmath.hFvoid main01O
int n,iN
float sum>9Nclrscr01N printf0YEnter the value of n:Y1Nscanf0YdY,Hn1Nfor0i>(NiB>nNi??1O
sum>sum?0float1(AiN AAtype castingP printf0Y"um > .)fY,sum1Ngetch01N
PProgram: *rite a program to compute the following e'ponential series
cpower of x>(?(Ax?xs%uareA)\?x%ubeA+\?---?xpowerof nAn\QincludeBstdio.hFQincludeBconio.hFQincludeBmath.hFunsigned long fact0int x1O
if0xB>(return (N
elsereturn x@fact0x-(1N
Pvoid main01O
int n,x,iNfloat sum>9Nclrscr01N printf0YEnter x and n values:Y1Nscanf0YddY,Hx,Hn1N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
97/187
for0i>9NiB>nNi??1O
sum>sum?pow0x,i1Afact0i1NP printf0Y"um > .)fY,sum1N
getch01NPProgram: Sin. Q Series .,.!u'e1.power44,.power66,,,,,,,,,
QincludeBstdio.hFQincludeBconio.hFQincludeBmath.hFunsigned long fact0int x1O
if0xF>(1return (N
else
return x@fact0x-(1NPvoid main01O
int i,x,n,p>(Nfloat sum>9Nclrscr01N printf0YEnter x and n values:Y1Nscanf0YddY,Hx,Hn1Nfor0i>(NiB>nNi??1O
if0i)>>91sum>sum-pow0x,p1Afact0p1N
elsesum>sum?pow0x,p1Afact0p1N
p>p?)NP printf0Y"um > .)fY,sum1Ngetch01N
PProgram: !os. Q series !os./,.suare0.power33,.power55,,,,,,,,,
QincludeBstdio.hFQincludeBconio.hFQincludeBmath.hFunsigned long fact0int x1O
if0xF>(1return (N
elsereturn x@fact0x-(1N
ARISE, AWAKE AND STOP TILL THE GOAL IS NOT REACHED
-
8/20/2019 Cnotes Full
98/187
Pvoid main01O
int i,n,x,p>9Nfloat sum>9N
clrscr01N printf0YEnter n H p values:Y1Nscanf0YddY,Hn,Hp1Nfor0i>(NiB>nNi??1O
if0i)>>91sum>sum>pow0x,p1Afact0p1N
elsesum>sum?pow0x,p1fact0p1N p>p?)N
P
printf0Y"um > .)fY,sum1Ngetch01N}
&RR&=S
Definition:,$ group of data items 0variables1 of same data types stored in a continuous memory location is!nown as arrays.
That means a group of related data items that shape location name that particular is indicated orsubscript in s%uare braces I J or array name. C "upports + types of arrays. They areR(. "ingle *imension arrays or 4ne dimensional). *ouble *imension arrays or Two dimensional+. Multidimensional arrays
/* Sing%e Dimensiona% array: $ group