A typical programming task can be divided into two phases: Problem solving phase o produce an ordered sequence of steps that describe solution of problem o this sequence of steps is called an algorithm Implementation phase o implement the program in some programming language Pseudocode is an artificial and informal language that helps programmers develop algorithms. Pseudocode is very similar to everyday English. Write an Pseudocode & A!orit"# to deter#ine a student$s %ina !rade and indicate &"et"er it is 'assin! or %aiin!( T"e %ina !rade is cacuated as t"e a)era!e o% %our #ar*s( Pseudocode+ Input a set of 4 marks Calculate their average by summing and dividing by 4 if average is below 50 Print FI!" else Print P##" Detaied A!orit"# Step 1: nput !1"!#"!$"!% Step #: &'A(E )!1*!#*!$*!%+,% Step $: if )&'A(E - ./+ then Print 01A23 else Print 0PASS3 endif
E4ample PRINT ,PASS- Step 1: nput !1"!#"!$"!% Step #: &'A(E )!1*!#*!$*!%+,% Step $: if )&'A(E -./+ then Print 01A23 else Print 0PASS3 endif START In'ut M1.M/.M0.M1 GRAD23M14M/4M04M1561 IS GRAD2789 PRINT ,FAIL- STOP : N Dra& a %o&c"art to %ind t"e su# o% %irst 89 natura nu#;ers( Dra& a %o&c"art to %ind t"e ar!est o% t"ree nu#;ers A.B. and C( 3Correct t"e ;eo& one5 Write an a!orit"# and dra& a %o&c"art to con)ert t"e en!t" in %eet to centi#eter Pseudocode+ Input the length in feet (Lft) Calculate the length in cm (Lcm) by multiplying LFT with 30 Print length in cm (LCM) A!orit"# Step 1: nput 2ft Step #: 2cm 2ft 4 $/ Step $: Print 2cm START In'ut L%t Lc# L%t < 09 Print Lc# STOP Fo&c"art Write an a!orit"# and dra& a %o&c"art t"at &i read t"e t&o sides o% a rectan!e and cacuate its area( Pseudocode Input the width (W) and Length (L) f a rectangle Calculate the area (!) by multiplying L with W Print ! A!orit"# Ste' 1+ In'ut W.L Ste' /+ A L < W Ste' 0+ Print A FLOW CHART Write an a!orit"# and dra& a %o&c"art t"at &i cacuate t"e roots o% a =uadratic e=uation Hint+ d > s=rt 3 5. and t"e roots are+ "1 > 3?b 4 d56/a and "/ > 3? b ? d56/a Pseudocode+ #$ Input the cefficient% (a& b& c) f the 'uadratic e'uatin /( Calculate d 0( Calculate "1 1( Calculate </ 8( Print <# and </ A!orit"#+ Ste' 1+ In'ut a. ;. c Ste' /+ d s=rt 3 5 Ste' 0+ "1 3?b 4 d5 6 3/ < a5 Ste' 1+ "/ 3?b ? d5 6 3/ < a5 Ste' 8+ Print "1. "/ START In'ut W. L A L < W Print A STOP 2 4 b ac Dra& a %o&c"art %or co#'utin! %actoria N 3N@5 5here 67 8 1 9 # 9 $ 9 :: 6 . START In'ut a. ;. c d s=rt3b " b ? 1 < a < c5 Print " 1 ." /
STOP " 1 3?b 4 d5 6 3/ < a5 ( / 3?b ? d5 6 3/ < a5 Assi!n#ent + A!orit"# and %o&c"art to %ind t"e roots o% =uadratic e=uation a</4;<4c>9 %or a cases A!orit"# and %o&c"art %or %indin! #a<i#u# and #ini#u# o% !i)en n nu#;ers A!orit"# and %o&c"art to !enerate Fi;onacci nu#;ers u' to n STRACTAR2 OF A C PROGRAM F2ATAR2S OF C+ PR2S2NT AND ABS2NT IF?TH2N?2LS2 STRACTAR2 ;he algorithm for the flowchart is as follows: If !)* then print ! el%e print * endif is ABB Print B Print A : N Write an a!orit"# t"at reads t&o )aues. deter#ines t"e ar!est )aue and 'rints t"e ar!est )aue &it" an identi%Cin! #essa!e( ALGORITHM Ste' 1+ Input DALA21. DALA2/ Ste' /+ if (DALA21 B DALA2/5 then MAE DALA21 el%e MAE DALA2/ endif Ste' 0+ Print +The large%t ,alue i%-& M!( Write an a!orit"# t"at reads t"ree nu#;ers and 'rints t"e )aue o% t"e ar!est nu#;er( ALGORITHM Ste' 1+ Input N1. N/. N0 Ste' /+ if (N1BN/5 then if (N1BN05 then MAE N1 FN1BN/. N1BN0G el%e MAE N0 FN0BN1BN/G endif el%e if (N/BN05 then MAE DALA21 Print +The large%t ,alue i%-& M!( STOP : N START In'ut DALA21.DALA2/ MAE DALA2/ is DALA21BDALA2/ MAE N/ FN/BN1. N/BN0G el%e MAE N0 FN0BN/BN1G endif endif Ste' 0+ Print +The large%t number i%-& M!( IF?TH2N?2LS2 STRACTAR2 ;he algorithm for the flowchart is as follows: If !)* then print ! el%e print * endif is ABB Print B Print A : N Dra& %o& c"art to t"e a;o)e e<a#'e FOR ASSIGNM2NT Write and a!orit"# and dra& a %o&c"art to a5 read an e#'oCee na#e 3NAM25. o)erti#e "ours &or*ed 3OD2RTIM25. "ours a;sent 3ABS2NT5 and ;5 deter#ine t"e ;onus 'aC#ent 3PA:M2NT5( Bonus Sc"edue OD2RTIM2 ? 3/605HABS2NT Bonus Paid B19 "ours B09 ;ut 19 "ours B/9 ;ut 09 "ours B19 ;ut /9 "ours 19 "ours I89 I19 I09 I/9 I19 Ste' 1+ Input NAM2.OD2RTIM2.ABS2NT Ste' /+ if 3OD2RTIM2?3/605HABS2NT B 195 then PA:M2NT 89 el%e if (OD2RTIM2?3/605HABS2NT B 095 then PA:M2NT 19 el%e if (OD2RTIM2?3/605HABS2NT B /95 then PA:M2NT 09 el%e if (OD2RTIM2?3/605HABS2NT B 195 then PA:M2NT /9 el%e PA:M2NT 19 endif Ste' 0+ Print ,Bonus %or-. NAM2 ,is I-. PA:M2NT Dra& %o& c"art to t"e a;o)e e<a#'e FOR ASSIGNM2NT 6H "eo(c JJ T"e #ost %a#ous 'ro!ra# o% t"e# a (( H6 Kincude 7stdio("B int #ain3)oid5 L 'rint%3MHeo Word@NnM5O 66 return 9O P OR Kincude7stdio("B #ain35 L 'rint%3,Heo. &ord-5O P TC'e and O;ser)e t"e code int #ain3)oid5 L 'rint%3QQHeo &ord@Nn$$5O return /O < $include %stdio&h' main() * printf(+,ello&&&-n&&oh my-n&&&when do i stop.-n+)/ 0 >>>>>>>>>>>>>>>>>>>>>>>>> $include %stdio&h' main() * int sum/ sum 1 500 2 35/ printf(+4he sum of 500 and 35 is 5d-n+6 sum)/ 0 Sa#'e Pro!ra# Out'ut ;he sum of .// and 1. is .1. >>>>>>>>>>>>>>>>>>>>>>>>>> $include %stdio&h' main() * int sum6 value/ sum 1 30/ value 1 35/ printf(+5d-t5d-n+6 sum6 value)/ 0 Pro!ra# out'ut oo*s i*e 1/ 1. JJJJJJJJJJJJJJJJJJJJJJJJJJJJJ $include %stdio&h' main() * int value36 value76 sum/ value3 1 85/ value7 1 39/ sum 1 value3 2 value7/ printf(+4he sum of 5d and 5d is 5d-n+6 value36 value76 sum)/ 0 JJJJJJJJJJJJJJJJJJJJJJJJJ $include %stdio&h' main() * int count/ char letter/ printf(+Count 1 5d-n+6 count)/ printf(+!etter 1 5c-n+6 letter)/ 0 Sa#'e 'ro!ra# out'ut =ount 8 #>%?% 2etter 8 f t can be seen from the sample output that the values which each of the variables take on at declaration time are noJRero. n =" this is common" and programmers must ensure that variables are assigned values before using them. f the program was run again" the output could well have different values for each of the variables. 5e can never assume that variables declare in the manner above will take on a specific value. JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ include %stdio&h' $define 4:;<4= 0&30 main() * float balance/ float ta>/ balance 1 ?7&30/ ta> 1 balance @ 4:;<4=/ printf(+4he ta> on 5&7f is 5&7f-n+6 balance6 ta> )/ 0 JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ 6H Printin! Lar!e Boc* Letters H6 Kincude 7stdio("B 66)oid ;oc*!3)oid5O 6HPrototC'e %or ;oc*! %unction H6 int #ain 3)oid5 L 'rint%3MNnM5O ;oc*!35O 'rint%3MNnM5O P 6H Print out t"e Boc* etter ! H6 )oid ;oc*!3)oid5 L 'rint%3M!!!!!!NnM5O 'rint%3M! !NnM5O 'rint%3M!NnM5O 'rint%3M! !!!NnM5O 'rint%3M! !NnM5O 'rint%3M!!!!!!NnM5O P
6H It 'rints out+ !!!!!! ! ! ! ! !!! ! ! !!!!!! H6 JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ Write a 'ro!ra# to 'rint su# and a)era!e o% t"ree nu#;ers@S @include -stdio.hA int main)+ ,, no need to have int if no return B float a"b"c"sum"avgC printf)DPlease enter $ numbers and press enterD+C scanf)DEfEfEfD"Fa"Fb"Fc+ sum 8 a * b * cC avg 8 sum,$C printf)DSum: Ef Avg: EfD"sum"avg+C return /C < OR
@include -stdio.hA main)+ B int a"b"c"sum"avgC a8 1//C b81#/C c81G/C sum 8 a * b * cC avg 8 sum,$C or avg8)a*b*c+,$C printf)DAverage of three numbersEd"Ed"and Ed8Ed,n3"a"b"c"avgD+C < 6H 'o&er/(c JJ Print out 'o&ers o% /+ 1. /. 1. T. (( u' to /UN H6 Kincude 7stdio("B Kde%ine N 1V int #ain3)oid5 L int nO 6H T"e current e<'onent H6 int )a > 1O 6H T"e current 'o&er o% / H6 'rint%3MNt n Nt /UnNnM5O 'rint%3MNt>>>>>>>>>>>>>>>>NnM5O %or 3n>9O n7>NO n445 L 'rint%3MNtW0d Nt WVdNnM. n. )a5O )a > /H)aO P return 9O P 6H It 'rints out + n /Un >>>>>>>>>>>>>>>> 9 1 1 / / 1 0 T 1 1V 8 0/ V V1 X 1/T T /8V Y 81/ 19 19/1 11 /91T 1/ 19YV 10 T1Y/ 11 1V0T1 18 0/XVT 1V V880V H6 6H add/(c JJ Add t&o nu#;ers and 'rint t"e# out to!et"er &it" t"eir su# AATHOR+ ZNTAC2H DAT2+ 1/6906/919 H6 Kincude 7stdio("B int #ain3)oid5 L int %irst. secondO 'rint%3M2nter t&o inte!ers B M5O scan%3MWd WdM. &%irst. &second5O 'rint%3MT"e t&o nu#;ers are+ Wd WdNnM. %irst. second5O 'rint%3MT"eir su# is WdNnM. %irst4second5O P 6H addn(c JJ Read a 'ositi)e nu#;er N( T"en read N inte!ers and H 'rint t"e# out to!et"er &it" t"eir su#( H6 Kincude 7stdio("B int #ain3)oid5 L int nO 6H T"e nu#;er o% nu#;ers to ;e read H6 int su#O 6H T"e su# o% nu#;ers areadC read H6 int currentO 6H T"e nu#;er [ust read H6 int c)O 6H Loo' contro )aria;e. it counts t"e nu#;er o% nu#;ers areadC read H6 'rint%3M2nter a 'ositi)e nu#;er n B M5O scan%3MWdM.&n5O 6H We s"oud c"ec* t"at n is reaC 'ositi)eH6 su# > 9O %or 3c)>9O c) 7 nO c)445 L 'rint%3MNn2nter an inte!er B M5O scan%3MWdM.¤t5O 6H 'rint%3MNnT"e nu#;er &as WdNnM. current5O H6 su# > su# 4 currentO P 'rint%3MT"e su# is WdNnM. su#5O return 9O P Assi!n#ent+ Na#e )arious "eader %ies 'resent in C W"at are 're'rocessor co##ands and t"eir use \eC&ords in C Loca )s Go;a )aria;es in C Data tC'es in C 3in detai5 ,H addn.c II 'ead a positive number 6. ;hen read 6 integers and H print them out together with their sum. H, @include -stdio.hA int main)void+ B int nC ,H ;he number of numbers to be read H, int sumC ,H ;he sum of numbers already read H, int currentC ,H ;he number Just read H, int lcvC ,H 2oop control variable" it counts the number of numbers already read H, printf)DEnter a positive number n A D+C scanf)DEdD"Fn+C ,H 5e should check that n is really positiveH, sum 8 /C for )lcv8/C lcv - nC lcv**+ B printf)DKnEnter an integer A D+C scanf)DEdD"Fcurrent+C ,H printf)DKn;he number was EdKnD" current+C H, sum 8 sum * currentC < printf)D;he sum is EdKnD" sum+C return /C < 6H %actoria(c JJ It co#'utes re'eatedC t"e %actoria o% an inte!er entered ;C t"e user( It ter#inates &"en t"e inte!er entered is not H 'ositi)e( H6 @include -stdio.hA int fact)int n+C int main)void+ B int currentC printf)DEnter a positive integer Lto terminate enter nonIpositiveM A D+C scanf)DEdD" Fcurrent+C while )current A /+ B printf)D;he factorial of Ed is EdKnD" current" fact)current++C printf)DEnter a positive integer Lto terminate enter nonIpositiveM A D+C scanf)DEdD" Fcurrent+C < < ,H n is a positive integer. ;he function returns its factorial H, int fact)int n+ B int lcvC ,H loop control variable H, int pC ,H set to the product of the first lcv positive integers H, for)p81" lcv8#C lcv -8 nC p8pHlcv" lcv**+C return pC < ,H fibo.c II t prints out the first 6 1ibonacci H numbers. H, @include -stdio.hA int main)void+ B int nC ,H ;he number of fibonacci numbers we will print H, int iC ,H ;he inde4 of fibonacci number to be printed ne4t H, int currentC ,H ;he value of the )i+th fibonacci number H, int ne4tC ,H ;he value of the )i*1+th fibonacci number H, int twoawayC ,H ;he value of the )i*#+th fibonacci number H, printf)DNow many 1ibonacci numbers do you want to computeO D+C scanf)DEdD" Fn+C if )n-8/+ printf)D;he number should be positive.KnD+C else B printf)DKnKnKt Kt 1ibonacci)+ KnKt888888888888888888888KnD+C ne4t 8 current 8 1C for )i81C i-8nC i**+ B printf)DKtEd Kt EdKnD" i" current+C twoaway 8 current*ne4tC current 8 ne4tC ne4t 8 twoawayC < < < ,H ;he output from a run of this program was: Now many 1ibonacci numbers do you want to computeO ? 1ibonacci)+ 888888888888888888888 1 1 # 1 $ # % $ . . > G P 1$ G #1 ? $% H, >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 5rite a program to find the largest of three numbers. 1. @include-stdio.hA 2. @include-conio.hA $. 4. )oid main)+ .. >. B P. 8. int a"b"cC ?. 1/. clrscr)+C 11. 1#. printf)DEnter the value of a:D+C 1$. 1%. scanf)DEdD"Fa+C 1.. 1>. printf)DKnEnter the value of b:D+C 1P. 1G. scanf)DEdD"Fb+C 1?. #/. printf)DKnEnter the value of c:D+C #1. ##. scanf)DEdD"Fc+C #$. 24. i%)aAb+ #.. #>. Bi%)aAc+ #P. #G. printf)Da is largestD+C #?. 30. ese $1. $#. printf)Dc is largestD+C< $$. 34. ese $.. 36. i%)bAc+ $P. $G. printf)Db is largestD+C $?. 40. ese %1. %#. printf)Dc is largestD+C %$. %%. getch)+C %.. %>. < >>>>>>>>>>>>>>>>>>>>>>>>>>>>> La; J/ KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK Pro!ra# %or Su# o% 19 nu#;ers 6H add(c H a si#'e C 'ro!ra# H6
Kincude 7stdio("B Kde%ine LAST 19
int #ain35 L int i. su# > 9O
%or 3 i > 1O i 7> LASTO i44 5 L su# 4> iO P 6HJ%orJH6 'rint%3Msu# > WdNnM. su#5O return 9O P Kincude directi)es+ incudes decarations o% %unctions %ro# "eader %ies %or standard i;raries( For e<a#'e. Kincude 7stdio("B incudes decarations o% %unctions %or t"e standard i;rarC t"at are use%u %or in'ut6out'ut( Note t"e distinct a;sence o% se#icoon Kde%ine directi)es+ Per%or#s te<tua re'ace#ent. &"ic" is use%u to de%ine constants( For e<a#'e. t"is is e!a C+ Kincude 7stdio("B Kde%ine B2GIN L Kde%ine 2ND P int #ain35 B2GIN 'rint%3MHeo Word@NnM5O return 9O 2ND 2nu# e<a#'e in C Kincude 7stdio("B int #ain35 L enu# DaCsLSundaC.MondaC.TuesdaC.WednesdaC.T"ursdaC.FridaC.SaturdaCPO DaCs T"eDaCO int [ > 9O 'rint%3MPease enter t"e daC o% t"e &ee* 39 to V5NnM5O scan%3MWdM.&[5O T"eDaC > DaCs3[5O i%3T"eDaC >> SundaC ]] T"eDaC >> SaturdaC5 'rint%3MHurraC it is t"e &ee*endNnM5O ese 'rint%3MCurses sti at &or*NnM5O return 9O P ,HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH H H 2nu#eration e<a#'e( Pro!ra# &i return t"e #ont" in a Cear( H I(2( It returns Y %or Se'te#;er( H H, main)+ B ,H H (efine a list of aliases H, enum months BQan81" 1eb" !ar" Apr" !ay" Qun" Qul" Aug" Sep" Rct" 6ov" (ec<C
enum months monthC ,H define SmonthS variable of type SmonthsS H, printf)DEdKnD" month8Sep+C ,H Assign integer value via an alias H ;his will return a ? H, < ,HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH H H 2nu#eration e<a#'e( Pro!ra# &i return t"e nu#;er o% daCs in a #ont" H I(2( returns /T %or Fe;urarC( H6 main)+ B enum days BQan8$1" 1eb8#G" !ar8$1" Apr8$/" !ay8$1" Qun8$/" Qul8$1" Aug8$1" Sep8$/" Rct8$1" 6ov8$/" (ec8$1<C enum days monthC ,H define SmonthS variable of type SmonthsS H, printf)DEdKnD" month81eb+C ,H Assign integer value via an alias H ;his will return #G H, < 6HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH H 2nu#eration e<a#'e+ T"is 'ro!ra# &i %ai to co#'ie ;ecause H ^Ae<^ is in ;ot" enu# ists( H6 main)+ B enum People1 BAle48/" ;racy" Tristian< &irlsC enum People# B5illiam8/" !artin" Ale4< UoysC ,H switch )Uoys+ B case 5illiam: puts)D5illiamD+C breakC case !artin: puts)D!artinD+C breakC case Ale4: puts)DAle4D+C breakC default: breakC < < HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH H H 2nu#eration e<a#'e+ T"is 'ro!ra# &i %ai to co#'ie ;ecause t"e H 're'rocessor &i c"an!e t"e FALS2 to 1 on t"e enu# state#ent(((( H, @define 1A2SE 1 main)+ B enum UoolianVt B1A2SE8/" ;'WE< UoolianC
printf)D1alse has a value of EdD" 1A2SE+C printf)D ;rue has a value of EdD" ;'WE+C < ,H 2nu#eration e<a#'e+ T"is 'ro!ra# &i co#'ie ;ut t"e Kde%ine state#ent &i cause FALS2 and TRA2 to "a)e a )aue o% 1 H6 enum UoolianVt B1A2SE8/" ;'WE< UoolianC @define 1A2SE 1 main)+ B enum UoolianVt UoolianC
printf)D1alse has a value of EdKnD" 1A2SE+C printf)D ;rue has a value of EdKnD" ;'WE+C < 'esults:
H 1alse has a value of 1 H ;rue has a value of 1 Don^t trC to dis'aC a deci#a nu#;er usin! t"e inte!er %or#at s'eci%ier. Wd. as t"is dis'aCs une<'ected )aues@ Si#iarC. don^t use W% %or dis'aCin! inte!ers( Mi<in! Wd &it" c"ar )aria;es. or Wc &it" int )aria;es is a ri!"t. as s"o&n in t"is e<a#'e+ @include -stdio.hA int main)+ B int a 8 P#C char b 8 SASC printf)Da equals Ed KnD" a+C printf)Da equals Ec KnD" a+C printf)Db equals Ed KnD" b+C printf)Db equals Ec KnD" b+C < a e=uas X/ a e=uas H 66 'rints ASCII )aue %or X/ ; e=uas V8 66 'rints ASCII Daue %or A ; e=uas A T"e reason &"C t"is &or*s is ;ecause a c"aracter constant is [ust an inte!er %ro# 9 to /88( e<a#'es o% 'rint% %or#atted out'ut+ @include-stdio.hA main)+ B int a"bC float c"dC a 8 1.C b 8 a , #C printf)DEdKnD"b+C printf)DE$dKnD"b+C printf)DE/$dKnD"b+C c 8 1..$C d 8 c , $C printf)DE$.#fKnD"d+C < Rutput of the source above: P P //P ..1/ @include-stdio.hA main)+ B int 1ahrenheitC for )1ahrenheit 8 /C 1ahrenheit -8 $//C 1ahrenheit 8 1ahrenheit * #/+ printf)DE$d E/>.$fKnD" 1ahrenheit" )../,?./+H)1ahrenheitI$#++C < ;he output of the e4ample above: / I1P.PPG #/ I>.>>P %/ /%.%%% >/ 1....> G/ #>.>>P 1// $P.PPG 1#/ %G.GG? 1%/ >/./// 1>/ P1.111 1G/ G#.### #// ?$.$$$ ##/ 1/%.%%% #%/ 11....> #>/ 1#>.>>P #G/ 1$P.PPG $// 1%G.GG? For#attin! ot"er TC'es @include-stdio.hA main)+ B printf)D;he color: EsKnD" DblueD+C printf)D1irst number: EdKnD" 1#$%.+C printf)DSecond number: E/%dKnD" #.+C printf)D;hird number: EiKnD" 1#$%+C printf)D1loat number: E$.#fKnD" $.1%1.?+C printf)DNe4adecimal: E4KnD" #..+C printf)DRctal: EoKnD" #..+C printf)DWnsigned value: EuKnD" 1./+C printf)DQust print the percentage sign EEKnD" 1/+C < Note+ In t"e ast 'rint% state#ent onC t"e 'ercenta!e si!n is 'rinted( T"e nu#;er 19 in t"is state#ent doesn$t #atterO it$s not used in t"e out'ut( So i% Cou &ant to 'rint a 'ercenta!e nu#;er Cou &oud use so#et"in! i*e t"is+ 'rint%3,W/dWWNn-. 195O 3T"e out'ut &i ;e 19W5 For#attin! Strin!s @include-stdio.hA main)+ B printf)D:Es:KnD" DNello" world7D+C printf)D:E1.s:KnD" DNello" world7D+C printf)D:E.1/s:KnD" DNello" world7D+C printf)D:EI1/s:KnD" DNello" world7D+C printf)D:EI1.s:KnD" DNello" world7D+C printf)D:E.1.s:KnD" DNello" world7D+C printf)D:E1..1/s:KnD" DNello" world7D+C printf)D:EI1..1/s:KnD" DNello" world7D+C < ;he output of the e4ample above: :Nello" world7: : Nello" world7: :Nello" wor: :Nello" world7: :Nello" world7 : :Nello" world7: : Nello" wor: :Nello" wor : As Cou can see. t"e strin! %or#at con)ersion reacts )erC di%%erent %ro# nu#;er %or#at con)ersions( T"e 'rint%3,+Ws+Nn-. ,Heo. &ord@-5O state#ent 'rints t"e strin! 3not"in! s'ecia "a''ens(5 T"e 'rint%3,+W18s+Nn-. ,Heo. &ord@-5O state#ent 'rints t"e strin!. ;ut 'rint 18 c"aracters( I% t"e strin! is s#aer t"e ,e#'tC- 'ositions &i ;e %ied &it" ,&"ites'ace(- T"e 'rint%3,+W(19s+Nn-. ,Heo. &ord@-5O state#ent 'rints t"e strin!. ;ut 'rint onC 19 c"aracters o% t"e strin!( T"e 'rint%3,+WJ19s+Nn-. ,Heo. &ord@-5O state#ent 'rints t"e strin!. ;ut 'rints at east 19 c"aracters( I% t"e strin! is s#aer ,&"ites'ace- is added at t"e end( 3See ne<t e<a#'e(5 T"e 'rint%3,+WJ18s+Nn-. ,Heo. &ord@-5O state#ent 'rints t"e strin!. ;ut 'rints at east 18 c"aracters( T"e strin! in t"is case is s"orter t"an t"e de%ined 18 c"aracter. t"us ,&"ites'ace- is added at t"e end 3de%ined ;C t"e #inus si!n(5 T"e 'rint%3,+W(18s+Nn-. ,Heo. &ord@-5O state#ent 'rints t"e strin!. ;ut 'rint onC 18 c"aracters o% t"e strin!( In t"is case t"e strin! is s"orter t"an 18. t"us t"e &"oe strin! is 'rinted( T"e 'rint%3,+W18(19s+Nn-. ,Heo. &ord@-5O state#ent 'rints t"e strin!. ;ut 'rint 18 c"aracters( I% t"e strin! is s#aer t"e ,e#'tC- 'ositions &i ;e %ied &it" ,&"ites'ace(- But it &i onC 'rint a #a<i#u# o% 19 c"aracters. t"us onC 'art o% ne& strin! 3od strin! 'us t"e &"ites'ace 'ositions5 is 'rinted( T"e 'rint%3,+WJ18(19s+Nn-. ,Heo. &ord@-5O state#ent 'rints t"e strin!. ;ut it does t"e e<act sa#e t"in! as t"e 're)ious state#ent. acce't t"e ,&"ites'ace- is added at t"e end( T"e scan% %unction+ read in%or#ation %ro# a standard in'ut de)ice 3*eC;oard5( @include -stdio.hA main)+ B int i 8 /C int k"J81/C printf)Dnput three integers and press enter to confirm.D+C i8scanf)DEdEdEdD"FJ"Fk"Fi+C printf)Dtotal values inputted EdKnD"i+C printf)D;he input values Ed EdKnD"J"k+C < In'ut t"ree inte!ers and 'ress enter to con%ir#(1 / 0 total values inputted $ ;he input values 1 # Asin! Scan% to Recei)e In'ut %ro# a Aser @include -stdio.hA main)+B int iRperand1 8 /C int iRperand# 8 /C printf)DKnEnter first operand: D+C scanf)DEdD" FiRperand1+C printf)DEnter second operand: D+C scanf)DEdD" FiRperand#+C printf)D;he result is EdKnD" iRperand1*iRperand#+C < Enter first operand: 1 Enter second operand:# ;he result is $ @include -stdio.hA main)+B int i"J"kC printf)Dnput two integers and press Enter to comfirm:D+C scanf)DEdEdD"Fi"FJ+C k 8 i * JC printf)Dsum of two numbers is Ed KnD"k+C < nput two integers and press Enter to comfirm:##$ # sum of two numbers is ##. 66 Addition O% T&o Nu#;ers W"ic" r Ta*en Fro# Aser@ @include-stdio.hA @include-conio.hA void main)+ B float n1"n#"ansC printf)DEnter the 1irst numberKnD+C scanf)DEfD"Fn1+C printf)DEnter the Second numberKnD+C scanf)DEfD"Fn#+C ans8n1 * n#C printf)DXour Answer is I: E.$fD"Fans+C < C 'ro!ra# to add t&o !i)en nu#;ers Kincude7stdio("B 66 standard in'ut out'ut "eader %ie Kincude7conio("B #ain35 L int a.;.cO crscr35O 'rint%3Menter anC nu#;erM5O scan%3MWdM.&a5O 'rint%3Menter anC nu#;erM5O scan%3MWdM.&;5O c>a4;O 'rint%3MWdM.c5O !etc"35O66getch)+ statement is a get character statement. t pause the output until user presses a key from the keyboard. P c 'ro!ra# to !enerate nu#;er ;et&een 1 to 199 @include-stdio.hA main)+ B int a81C clrscr)+C while )a-81//+ B printf)DKnEdD"a+C a8a*1C < getch)+C < C 'ro!ra# to !enerate e)en nu#;ers @include-stdio.hA main)+ B int a8/C clrscr)+C while )a-#/+ B a8a*#C printf)DKnEdD"a+C < getch)+C < = program to generate odd numbers @include-stdio.hA main)+ B int a81C clrscr)+C while )a-#/+ B printf)DKnEdD"a+C a8a*#C < getch)+C < C 'ro!ra# to !enerate a ta;e o% anC !i)en nu#;er @include-stdio.hA main)+ B int a"b"cC a81C clrscr)+C printf)DEnter Any 6o.D+C scanf)DEdD"Fb+C while )a-81/+ B c8aHbC printf)DKnEdD"c+C a8a*1C < getch)+C < C 'ro!ra# to !enerate a 'Cra#id o% stars @include-stdio.hA main)+ B int a"b"cC a81C clrscr)+C for )a81Ca-8.Ca**+ B for )b81Cb-8aCb**+ B printf)DHD+C < printf)DKnD+C < getch)+C < ,,output would be H HH HHH HHHH HHHHH C 'ro!ra# to !enerate a u'side do&n 'Cra#id @include-stdio.hA main)+ B int a"b"cC a81C clrscr)+C for )a8.CaA81CaII+ B for )b8aCbA81CbII+ B printf)DHD+C < printf)DKnD+C < getch)+C < ,,output would be HHHHH HHHH HHH HH H C 'ro!ra# to %ind s=uare o% an inte!er @include-stdio.hA @include-conio.hA void main)+ B long int m"nC printf)DPlease enter the numberD+C scanf)DEldD "Fn+C m8nC n8nHnC printf)DSquare of entered number Eld 8 Eld D"m"n+C < Or @include-stdio.hA @include-conio.hA void main)+ B long int nC printf)DPlease enter the numberD+C scanf)DEldD "Fn+C n8nHnC printf)DSquare of entered number 8 Eld D"n+C < but if you want to show the entered number e.g.Square of the entered number G8>% then above one is the code. PROGRAM TO FIND S_AAR2 ROOT OF A NAMB2R Kincude 7#at"("B Kincude 7stdio("B int #ain3)oid5 L dou;e < > 1(9. resutO resut > s=rt3<5O 'rint%3MT"e s=uare root o% W% is W%NnM. <. resut5O return 9O P %ind 'o&er o% anC inte!er )aue((( @include-stdio.hA int po)int 4" int y+C void main)+ B int a" b" resC printf)DKn Enter the number and powerD+C scanf)DEd EdD" Fa" Fb+C res 8 po)a"b+C printf)DKn 'esult is 8 EdD" res+C < int po)int 4" int y+ B int f81" iC for )i81C i-8yC i**+ f8fH4C return)f+C < !enerate 'ri#e nu#;ers u'to a !i)en s'eci%ic nu#;er @include-stdio.hA @include-conio.hA void main)+ B int i"J"nC printf)D Enter the number upto which we have to find the prime number: D+C scanf)DEdD"Fn+C printf)DKnD+C for)i8#Ci-8nCi**+ B for)J8#CJ-8iI1CJ**+ if)iEJ88/+ breakC ,,6umber is divisble by some other number. So break out if)i88J+ printf)DKtEdD"i+C ,,6umber was divisible by itself )that is" i was same as J+ <,,=ontinue loop upto nth number getch)+C < Or main)+ B int a"c8/"i"nC printf)Denter the number to be checkedD+C scanf)DEdD"Fn+C for)i81Ci-8nCi**+ B a8nEiC if)a8/+ B c8c*1C < < if )c8#+ B printf)Dthe given number is primeD+C < else printf)Dthe given number is not primeD+C < or T"is 'ro!ra# is to c"ec* a !i)en nu#;er is 'ri#e or not 3or5 ist t"e 'ri#e nu#;ers in a !i)en no( main)+ B int i"J8#"ch8/C clrscr)+C printf)DKnE6;E' A6X 6W!UE'D+C scanf)DEdD"Fi+C while)J-8i,#+ B if)iEJ88/+ B printf)DEd S 6R; P'!ED"i+C ch81C breakC < else B J**C < < if)ch88/+ B printf)DEd S P'!ED"i+C < < %unction 'ro!ra# t"at %inds a 'ri#e nu#;er ;et&een 1 to 1999( Kincude M#at"("M Kincude Mstdio("M int IsPri#e3const int5O int #ain3)oid5 L int nu#;erO %or3nu#;er > 1O nu#;er 7> 1999O nu#;er445 L i%3IsPri#e3nu#;er55 L 'rint%3,WdNn-.nu#;er5O P P return 9O P int IsPri#e3const int di)idend5 L int root > int3s=rt3di)idend55O int di)isiorO int resut > 1O %or3di)isor > /O di)isor 7> rootO di)isor445 L i% 3di)idend W di)isor >> 95 L resut > 9O ;rea*O P P return resutO P Ho& do Cou &rite a 'ro!ra# in C to s&a' t&o )aria;es &it"out usin! t"e t"ird oneS include.%tdi$h) include-conio.hA main)+ B int a"bC clrscr)+C printf)Denter the value for a F bKnD+C scanf)DdD"Fa"Fb+C printf)Dvalue of a is Ed F b is Ed before swappingKnD"a"b+C a8a*bC b8aIbC a8aIbC printf)Dvalue of a is Ed F b is Ed after swappingKnD"a"b+C getch)+C < 3T"is 'ro!ra##e &i &or* %or onC inte!er not %or %oatin!5 s&a' )aues o% t&o )aria;es usin! c an!ua!e &it"out usin! arit"#etic o'eratorsS !ain)/ B int a"b"tempC printf)Denter the values of a F bD+C ,,could be done in diff. two lines for the input,, scanf)DEd EdD"Fa"Fb+C printf)Dthe values of a8Ed F b8Ed before swappingD"a"b+C 66 s&a''in! &it"out usin! arit"#etic o'erators((((( te#'>aO a>;O ;>te#'O printf)Dthe values of a8Ed F b8Ed after swappingD"a"b+C getch)+C < write a program to find out whether a given number is prime or not. main)+ B int a"c8/"i"nC printf)Denter the number to be checkedD+C scanf)DEdD"Fn+C for)i81Ci-8nCi**+ B a8nEiC if)a8/+ B c8c*1C < < if )c8#+ B printf)Dthe given number is primeD+C < else printf)Dthe given number is not primeD+C < or @include-stdio.hA @include-conio.hA void main)+ B int no"iC clrscr)+C printf)D Enter the 6umber W want to check:D+C scanf)DEdD"Fno+C i8#C while)i-8noI1+ B if)noEi88/+ B printf)D Ed is not Prime numberD"no +C breakC < i8i*1C < if)no88i+ printf)DEd is a prime 6umberD"no+C getch)+C < c"ec* !i)en nu#;er is 'ri#e nu#;er or not usin! c 'ro!ra# void main)+ B int num"i"count8/C clrscr)+C printf)DKnEnter a number:D+C scanf)DEdD"Fnum+C for)i81Ci-8numCi**+ B if)numEi88/+ count**C < if)count88#+ printf)DEd is a prime numberD"num+C else printf)DEd is not a prime numberD"num+C getch)+C < Write a c 'ro!ra# to %ind t"e roots o% a =uadratic e=uationS 6HJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ PROGRAM TO FIND TH2 ROOTS OF A _AADRATIC 2_AATION JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJH6 Kincude Kincude Kincude int #ain35 L int A.B.CO %oat disc.deno.<1.</O 'rint%3MNnJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJM5O 'rint%3MNnNn PROGRAM TO FIND TH2 ROOTS OF A _AADRATIC 2_AATION M5O 'rint%3MNnNnJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJM5O 'rint%3MNnNnNt 2NT2R TH2 DALA2S OF A.B.C(((M5O scan%3MWd.Wd.WdM.&A.&B.&C5O disc > 3BHB5J31HAHC5O deno > /HAO i%3disc B 95 L 'rint%3MNnNt TH2 ROOTS AR2 R2AL ROOTSM5O <1 > 3JB6deno543s=rt3disc56deno5O </ > 3JB6deno5J3s=rt3disc56deno5O 'rint%3MNnNnNt TH2 ROOTS AR2(((+ W% and W%NnM.<1.</5O P ese i%3disc >> 95 L 'rint%3MNnNt TH2 ROOTS AR2 R2P2AT2D ROOTSM5O <1 > JB6denoO 'rint%3MNnNnNt TH2 ROOT IS(((+ W%NnM.<15O P ese 'rint%3MNnNt TH2 ROOTS AR2 IMAGINAR: ROOTSNnM5O 'rint%3MNnJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJM5O !etc"35O P Or include-stdio.hA @include-conio.hA @include-math.hA @include-process.hA void main)+ B float a"b"c"41"4#"discC clrscr)+C printf)DEnter the coIefficientsKnD+C scanf)DEfEfEfD"Fa"Fb"Fc+C disc8bHbI%HaHcC,Hto find discriminantH, if)discA/+,Hdistinct rootsH, B 418)Ib*sqrt)disc++,)#Ha+C 4#8)IbIsqrt)disc++,)#Ha+C printf)D;he roots are distinctKnD+C e4it)/+C < if)disc88/+,HEqual rootsH, B 4184#8Ib,)#Ha+C printf)D;he roots are equalKnD+C printf)D418EfKn4#8EfKnD"41"4#+C e4it)/+C < 418Ib,)#Ha+C,Hcomple4 rootsH, 4#8sqrt)fabs)disc++,)#Ha+C printf)D;he roots are comple4KnD+C printf)D;he first root8Ef*iEfKnD"41"4#+C printf)D;he second root8EfIiEfKnD"41"4#+C getch)+C < JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ Write a C 'ro!ra# to arran!e X nu#;ers in ascendin! orderS ,H= Program to arrange P numbers in ascending orderH, @include-stdio.hA @include-conio.hA void main)+ B int i"J"temp"aLPMC clrscr)+C printf)DEnter P integer numbers: KnD+C for)i8/Ci-PCi**+ scanf)DEdD"FaLiM+C for )i8/Ci-PCi**+ B for)J8i*1CJ-PCJ**+ B if)aLiMAaLJM+ B temp8aLJMC aLJM8aLiMC aLiM8tempC < < < printf)DKnKn;he P numbers sorted in ascending order are: KnD+C for)i8/Ci-PCi**+ printf)DEdKtD"aLiM+C getch)+C < ,H Sample Rutput: Enter P integer numbers: P ? # G > $ % ;he P numbers sorted in ascending order are: # $ % > P G ? H, 'ro!ra# t"at in'uts 0 nu#;ers and s"oud dis'aC ar!est and s#aest in ter#s o% )aueS Kincude 7stdio("B Kde%ine NAMB2RS 0 int #ain3)oid5 L int nu#;ersFNAMB2RSGO int "i!"O int o&O int iO %or 3i > 9O i 7 NAMB2RSO i445 L 'rint%3MNnPease enter a nu#;er+ M5O scan%3MWdM. &nu#;ersFiG5O P o& > "i!" > nu#;ersF9GO %or 3i > 1O i 7 NAMB2RSO i445 L i% 3nu#;ersFiG 7 o&5 o& > nu#;ersFiGO ese i% 3nu#;ersFiG B "i!"5 "i!" > nu#;ersFiGO P 'rint%3MLo&+ Wd NnHi!"+ WdM. o&. "i!"5O return 9O P C Lan!ua!e Pro!ra# For Find T"e Lar!est Nu#;er A#on! T"ree Nu#;ers Kincude 7stdio("B )oid #ain35 L int A.B.CO crscr35O 'rint%3MNtNt PROGRAM FOR FIND TH2 LARG2ST NAMB2R AMONG THR22 NAMB2RSNNM5O 'rint%3MNn 2NT2R THR22 NAMB2RS + J NnM5O 'rint%3MNn A> M5O scan%3MWdM.&A5O 'rint%3MNn B>M5O scan%3MWdM. &B5O 'rint%3MNn C>M5O scan%3MWdM. &C5O i%3ABB5 L i%3ABC5 L 'rint%3MNn GRAT2ST NAMB2R IS A> Wd M.A5O P ese i%3CBA5 L 'rint%3MNn GR2AT2ST NAMB2R IS C> WdM.C5O P ese i%3C>>A5 L 'rint%3MNn GRAT2ST NAMB2R AR2 A > C > WdM.C5O P P ese i%3BBC5 L i%3BBA5 L 'rint%3MNn GRAT2ST NAMB2R IS B > WdM. B5O P i%3B>>A5 L 'rint%3MNn GRAT2ST NAMB2RS AR2 A > B > WdM. A5O P P ese i%3CBA5 L i%3B>>C5 L 'rint%3MNn GR2AT2ST NAMB2RS AR2 B>C>WdM. C5O P ese L 'rint%3MNn GR2AT2ST NAMB2RS IS C>WdM. C5O P P ese i%3A>>B && B>>C5 L L 'rint%3MNn ALL AR2 2_AAL A > B > C > WdM. C5O P P !etc"35O P Write c 'ro!ra# to %ind odd or e)en noS =lue: 1or any number n you could use )n E # 88 /+" which would be true for an even number" false for odd 6H To c"ec* *eCed nu#;er is a 'ri#e nu#;er or a e)en nu#;er or odd nu#;er H6
Kincude Kincude )oid c"*`nu#;er3int n5 L int c>9.iO %or3i>/Oi L i%3nWi>>95 c>1O P i%3nW/>>9 && c>>95 'rint%3MWd is a 'ri#e e)en nu#;erM.n5O ese i%3nW/@>9 && c>>95 'rint%3MWd is a odd 'ri#e nu#;erM.n5O ese i%3nW/>>9 && c@>95 'rint%3MWd is onC an e)en nu#;er.not 'ri#eM.n5O ese i%3nW/@>95 'rint%3MWd is onC an odd nu#;er.not 'ri#eM.n5O ese 'rint%3MWd is not a 'ri#e nu#;erM5O P )oid #ain35 L int nO crscr35O 'rint%3MNn 2nter a nu#;er +M5O scan%3MWdM.&n5O 'rint%3MNn Nature o% nu#;er NnM5O 'rint%3MNn JJJJJJJJJJJJJJJJJ NnM5O c"*`nu#;er3n5O !etc"35O P OATPAT 2nter a nu#;er +01 Nature o% nu#;er JJJJJJJJJJJJJJJJJ 01 is onC an e)en nu#;er.not 'ri#e 2nter a nu#;er +/0 Nature o% nu#;er JJJJJJJJJJJJJJJJJ /0 is a odd 'ri#e nu#;er 2nter a nu#;er +/ Nature o% nu#;er JJJJJJJJJJJJJJJJJ / is a 'ri#e e)en nu#;er 2nter a nu#;er +/8 Nature o% nu#;er JJJJJJJJJJJJJJJJJ /8 is onC an odd nu#;er.not 'ri#e JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ JJJJJJJJ La; J0 KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK TrC %oo&in! e<a#'e to understand a t"e arit"#etic o'erators( Co'C and 'aste %oo&in! C 'ro!ra# in test(c %ie and co#'ie and run t"is 'ro!ra#. main)+ B int a 8 #1C int b 8 1/C int c C c 8 a * bC printf)D2ine 1 I Yalue of c is EdKnD" c +C c 8 a I bC printf)D2ine # I Yalue of c is EdKnD" c +C c 8 a H bC printf)D2ine $ I Yalue of c is EdKnD" c +C c 8 a , bC printf)D2ine % I Yalue of c is EdKnD" c +C c 8 a E bC printf)D2ine . I Yalue of c is EdKnD" c +C c 8 a**C printf)D2ine > I Yalue of c is EdKnD" c +C c 8 aIIC printf)D2ine P I Yalue of c is EdKnD" c +C < T"is &i 'roduce %oo&in! resut Line 1 J Daue o% c is 01 Line / J Daue o% c is 11 Line 0 J Daue o% c is /19 Line 1 J Daue o% c is / Line 8 J Daue o% c is 1 Line V J Daue o% c is /1 Line X J Daue o% c is // O;ser)e %unction o% scan% main)+ B int a "bC printf)0Enter any value3+C scanf)0Ed3"Fa+C b8aC printf)b+C < e<a#'e %or 'uts and !ets @include-stdio.hA main) + B char nameL%/MC puts)DEnter your nameD+C gets)name+C puts)DXour name isD+C puts3na#e5O P