Upload
others
View
21
Download
0
Embed Size (px)
Citation preview
MID TERM EX AM – 1
COMP UT ER PRO GRAMM ING (Sy l labus – Un it I )
Answer s for P ar t A ( Sh or t Answer Que st ion s) f r om Tutor ia l Que st ion Bank
1. L is t the t wo major co m ponents o f a co mputer sys te m ?
Ans : Th e major co mpon ents o f a co mputer sy st em a re : a . Hardwar e c ompon ent b . Soft war e Compon ent
The majo r hard war e co mponent s o f a c omput e r sy ste m ar e :
Proc es sor
Main me mory
Se condary m e mory
Input De vi ce s – e .g . k ey board , Mou se, Mic roph one, Bar cod e re ader
Output De v ic es – e .g . m onitor , Pr in ter , Spe aker Comput er so ft war e con s i st s o f bo th progr am s and data . A pro gra m co ns i st s o f inst ru ct ion s for th e pro ce s sor . Data can b e any in for mat i on that a pro gram n e eds .
2 . Ident i fy th e step s in cr eat ing and runn ing a C progra m ? Ans : Exe cut ing a C pro g ram wr i t t en in C in vo l ves a s er i e s o f s t ep s. The s e ar e
Cre at ing th e pro gra m
Comp i l in g the Prog ram
L ink ing th e pro gra m w it h funct ion s that a re n e eded f ro m C l ib ra ry
Execut in g th e progr am
3 . State th e key word whi c h help s to r eta in th e h i ghe st ac curacy o f data ? Ans : doub l e i s th e k ey word whi ch he lps to r e ta in th e h i ghe st accu racy o f data . Re a l f loat in g -po int type i s u sua l ly re fe rred to as a doub le -pre c i s ion f loat i ng -po int type.
Type Stor age S ize ( in byte s)
Value Rang e Pr ec i s ion
f loat 4 1 .2E -38 t o 3 . 4E+ 38 6 de c i mal p lace s
doub le 8 2 .3E -30 8 to 1 .7E +30 8 15 de c i mal p lace s
long doub l e 12 3 .4E -49 32 t o 1 .1E +49 32
19 de c i mal p lace s
4 . Wr ite th e var i ous c la s s es o f data typ e s AN SI C support s ?
Ans : Th er e ar e fou r dat a types in C languag e. T hey are
S. No. Types Data t ype s
1. Bas ic or funda menta l data types
in t , char , f loat , doub l e
2 . Enumera t ion data typ e E n u m
3. Der ived data typ e Po inte r , a r ray , s t ruc tur e, un ion
4 . Vo id data typ e vo id
5 . State whi ch of th e f o l lo wing a re va l id id ent i f i e rs . I f in va l id , s t ate th e rea son .
a . sa mple 1 b . data_7 c . return d . #f ine e . 91- 08 0- 10 0 f . name & ag e g . _v a l
Ans : sa mpl e1 : va l id data_7 : va l id return : in va l id (be caus e i t i s a key word ) #f ine : in va l id ( Id ent i f ie rs can ’ t s ta r t w i th a spe c ia l symb ol )
91- 08 0- 10 0: in va l id ( Id e nt i f i er s can ’ t s ta r t w i th a d ig i t ) name & ag e: in va l id ( Id ent i f ie rs can ’ t ha ve & s ymbo l ) _va l : va l id ( Id ent i f i er s can sta r t w i th an unde r sco re sy mbol )
6. F ind th e va lue o f x in t he fo l lo wing expr e s s ion ? x = 3 / 2 % 6 – 3 / 9 ; Ans : x = 3 / 2 % 6 – 3 / 9 = 1 % 6 – 3 /9 = 1 – 3 / 9 = 1 – 0 = 1 # inc lude < std io .h > vo id main ( ) { in t x ; x = 3 / 2 % 6 - 3 / 9 ; p r in t f (" \n va lue = %d",x ) ; } Output : 1
7 . Exp la in the output o f f o l lo wing stat e ment ?
pr int f (“% s” ,” I ARE -20 15 ”+5 ) ; Ans : The d i g i t 5 ind icat es th e po s i t ion in the s t r ing IARE -2 01 5, so th e pr in t f ( ) funct ion pr in t s the s t r ing f ro m 5
t h p os i t ion . Hen ce th e output i s 2 01 5.
8 . Wr ite th e s i z e and rang e of th e ba s i c data type s ? Ans :
S. No. Data t ype S iz e in bytes
Range
1. s ign ed cha r 1 -12 8 to 127
2 . uns ign ed cha r 1 0 to 25 5
3 . s ign ed in t 4 -21 47 48 364 8 to 2 14 748 364 7
4 . uns ign ed in t 4 0 to 42 94 967 29 5
5 . uns ign ed short in t 2 0 to 65 53 5
6 . f loat 4 1 .2E -38 t o 3 . 4E+ 38
7 . doub le 8 2 .3E -30 8 to 1 .7E +30 8
8 . long doub l e 12 3 .4E -49 32 to 1 .1E +4 93 2
9 . Sol ve th e exp re s s ion an d f ind output o f the fo l l owin g cod e?
vo id main ( ) { in t i = -3 , j = 2 , k = 0 , m ; m = + + i && + +j && ++k ; pr int f (“% 3d %3d %3 d%3d” , i , j , k , m ) ; } Ans : - 2 3 1 1
10 . Tak e x = 0 , y = 0 and z = 1 . F ind the va lu e o f x , y , and z a f te r e xe cut ing the fo l lo win g cod e ?
i f ( x ) i f (y ) z = 3 ; e l se
z = 2 ; Ans : 0 0 1 because x =0 , so th e i f con d i t ion i s f a l se and the re i s no e ls e part . The re for e the re i s no c hange to th e x , y and z va lu e s .
11 . F ind th e output o f th e f o l lo wing code ? vo id main ( ) { doub le k =0 ; for (k = 0 .0 , k < 3 .0 ; k++ ) pr int f (“H el lo” ) ;
} Ans : He l lo Hel lo Hel lo
12 . Sol ve th e exp re s s ion an d f ind output o f the fo l l owin g cod e? vo id main ( ) { in t x = !5 – 4 + 2 * 5 ;
pr int f (“%d” , x ) ; } Ans : 6
13 . Wr ite th e ba s i c e scap e sequ enc e cha ract er s an d i t s me an ing with exa mpl e? Ans :
\ ’ S ing l e quot e
\” Doub le quo te
\a Aud ib le b el l
\b Back spac e
\ f For m f eed
\n New l in e
\ r Carr i ag e re turn
\s A spa ce o r wh ite spa ce.
\ t Hor izonta l t ab
14 . F ind th e output o f c , d , e and f in the b elo w co de?
f loat c = 15/ 10. 0 ; in t d = 1 5/ 10; f loat e = 15 /1 0;
f loat f = 15 .0/ 10. 0 ; Ans : 1 . 500 00 0 1 1 .00 00 00
15 . F ind th e output o f th e f o l lo wing code ? in t main ( ) { pr int f (“%d” +1, 12 3) ; return 0 ; } Ans : d
16 . F ind th e output o f th e f o l lo wing code ? in t main ( ) { pr int f (“%d” , p r in t f ( “H i !” ) + pr in t f ( “By e”) ) ; return 0 ;
} Ans : H i ! Bye 6
17 . F ind th e output o f th e f o l lo wing code ? in t main ( ) { pr int f (“Work” “ Har d”) ; return 0 ; } Ans : WorkHa rd
18 . F ind th e output o f th e f o l lo wing code ? in t main ( ) { in t v = 1 0; pr int f (“%d” , v+ +, “ %d” , v - - ) ;
return 0 ; } Ans : 9
19 . F ind th e output o f th e f o l lo wing code ? Note: A ssu m e tw o va lu es a re ent er ed by the u se r ar e st ored in the va r iab le s v and n re spe ct i ve ly . in t main ( ) { in t v = 5 , n ; pr int f (“%d” , s canf (“ %d%d”, & v, &n )) ; return 0 ;
} Ans : 2
20 . F ind th e output o f th e f o l lo wing code ? in t main ( ) { in t a = 500, b = 1 00 , c = 3 0, d = 40, e = 19 ; a + = b - = c * = d / = e %= 5 ; pr int f (“% 2d% 2d %2d %2d %2d” , a , b , c , d , e ) ; return 0 ; }
Ans : 30 0 -20 0 300 10 4
21 . F ind th e va lue o f x , y , z for a = 9 , b = 1 2, c = 3 (as su me a l l a r e d ec la re d as f loat data type) a . x = a – b / 3 + c * 2 – 1 ; b . y = a – b / ( 3 + c ) * ( 2 – 1 ) ; c . z = a – (b / (3 + c ) * 2) – 1 ; Ans : 10 7 4
22 . F ind th e output o f th e f o l lo wing code ? in t ma in ( ) { in t a ; a = 0 15 + 0x 15 + 5 ; pr int f (“%d” , a ) ; return 0 ; } Ans : 39
23 . F ind th e output o f th e f o l lo wing code ? in t main ( ) { pr int f (“% 2d %2d %2 d” , s i z eof (3 .14 ) , s i z eof ( 3 .14 f ) , s i zeo f ( 3 .1 4L) ) ; return 0 ; } Ans : 8 4 12
24 . F ind th e output o f th e f o l lo wing code ? in t main ( ) { in t i = 5 ,a ; a = ++ i + + + i + + + i ; pr int f (“%d” , a ) ; return 0 ; } Ans : 22
25 . F ind th e output o f th e f o l lo wing code ?
# inc lude < std io .h > in t main ( ) { in t i = 1 ; for ( ; i < 4 ; i ++ ) ; p r in t f (" %d", i ) ; return 0 ; } Ans : 4
26 . F ind th e output o f th e f o l lo wing code ? in t main ( ) { in t a , b ; for (a = 0 ; a < 1 0; a + +) ; for (b = 2 5; b > 9 ; b -= 3 ) ; pr int f (“%d %d”, a , b ) ;
return 0 ; } Ans : 10 7
27 . F ind th e output o f th e f o l lo wing code ? in t main ( ) { in t a ; for (a = 5 ; - - a ; ) p r in t f (" %d", a ) ; return 0 ; } Ans : 43 21
30 . F ind the ou tput o f th e f o l lo wing code ? in t main ( ) { in t a = 1 , b = 2 , c = 3 , d = 4 , e ; i f ( e = (a & b | c ^ d ) ) p r in t f (" %d", e) ; return 0 ; } Ans : 7
31 . F ind the ou tput o f th e f o l lo wing code ? vo id main ( ) { in t a= 1,b= 2,c =3,d =4; i f (d > c ) i f ( c > b ) pr in t f (" %d %d", d , c ) ; e l se i f ( c > a ) pr in t f (" %d %d", c , d ) ; i f ( c > a ) i f (b < a ) p r in t f (" %d %d", c , a ) ; e l se i f (b < c ) p r in t f (" %d %d", b , c ) ; } Ans : 4 3 2 3
32 . F ind the ou tput o f th e f o l lo wing code ? vo id main ( ) { in t cho i ce = 3 ; s witch (cho i c e) { defau l t : p r in t f ("d ef au l t " ) ; cas e 1 : pr in t f ( "cho ic e 1") ; b r eak ; cas e 2 : pr in t f ( "cho ic e 2") ; b r eak ; } } Ans : de fau l t cho ic e 1
33 . F ind the ou tput o f th e f o l lo wing code ? vo id main ( ) { char c = 1 25; d o pr in t f (" %d", c ) ; wh i l e( c+ + ) ; } Ans : 12 5 1 26 1 27 -1 28 - 127 . . . . -2 -1 0
34 . F ind the ou tput o f th e f o l lo wing code ? vo id main ( )
{ for ( ; ; ) { p r in t f (" %d", 10 ) ; } } Ans : 10 10 10 1 0 . . . . . . . . . . . . in f in i t e ly
35 . F ind the ou t put o f th e f o l lo wing code ? vo id main ( ) { p r in t f ("h i !" ) ; i f ! (0 ) p r in t f ("by e" ) ; } Ans : Syntax err or
36 . F ind the ou tput o f th e f o l lo wing code ? vo id main ( )
{
in t a = 1;
i f (a )
p r in t f ("t e st" ) ;
e l se ;
p r in t f ("a ga in ") ;
}
Ans : te sta ga in
37 . F ind the ou tput o f th e f o l lo wing code ? vo id main ( )
{
in t i = 1;
i f ( i+ +, ++ i , i - - , - - i )
p r in t f (" %d \n ", i ) ;
}
Ans : 1
38 . F ind the ou tput o f th e f o l lo wing code ? vo id main ( )
{
f loat i ;
for ( i = 0 .1 ; i < 0 . 4 ; i + = 0 .1)
p r in t f (" %.1 f \n " , i ) ;
}
Ans : 0 .1
0 .2
0 .3
39 . F ind the ou tput o f th e f o l lo wing code ? vo id main ( )
{
in t i ;
for ( i = 2 ; i + = 2 ; i <= 9 ; i + =2 )
p r in t f (" %d \n ", i ) ;
}
Ans : Syntax Er ror
40 . F ind the ou tput o f th e f o l lo wing code ? vo id main ( )
{
in t i = 3 ;
for ( i - - ; i < 7 ; i = 7 )
p r in t f (" %d", i+ +) ;
}
Ans : 2
Answer s for P ar t B (Lo ng An swer Que st ions ) f r om Tutor ia l Quest ion Bank
3 ) Wr ite a C pr o gr am t o pr int the odd num ber s f r om X to Y u s in g do whi l e loop ? #inc lude < std io .h > main ( ) { in t X , Y , a ; p r in t f ( "Ent er va lu es o f X and Y ") ; scan f ( " %d %d ", &X, & Y ) ; for (a = X ; a < =Y ; a ++ ) { i f ( a % 2 = = 1 ) p r in t f ( " %d \n ", a ) ; } } 5) Wr i te a C pr ogr am t o d isp l ay your br anch nam e based upon the br anch code us ing sw it ch statem ent ? # inc lude < std io .h > main ( ) { in t a ; p r in t f ( "Ent er B rach Co de “ )
pr int f ( “ \n 2 . EEE \n 3 . MECH \n 4 . ECE \n 5 . C SE \n " ) ; pr int f (“ Enter you r cho ice ” ) ;
scan f ( " %d ” , & a) ; s witch ( a )
{ cas e 2 : pr int f (“ Your Br anch i s EEE” ) ; b r eak ; cas e 3 : pr in t f (“ Your Br anch i s MECH ”) ; b reak ; cas e 4 : pr in t f (“ Your Br anch i s E CE”) ; b r eak ; cas e 5 : pr in t f (“ Your Br anch i s C SE”) ; b reak ; defau l t : p r in t f (“ In va l id Branch Code ”) ; }
} 6) Ca lcu l ate the L CM a nd GCD o f two 2 - d ig i t num ber s? # inc lude < std io .h > main ( ) { in t a , b , x , y , t , gcd , l c m; p r in t f ("Ent er t wo in t e ger s \n ") ; scan f ( " %d%d ", &x , &y) ; a = x ; b = y ; wh i l e (b != 0) { t = b ; b = a % b ; a = t ; }
gcd = a ; l cm = (x *y)/ gcd ; p r in t f (" Gr eat est co mm on d iv i so r o f %d and %d = %d \n", x , y , gcd ) ; p r in t f ("L ea st co m mon mult ip le o f %d and %d = %d \n", x , y , l c m) ; } 7) Wr ite a C pr o gr am t o ident i f y an e l ig ib l e v oter as per Ind ian e lect ion pr o cess us in g ne ste d – i f s tatem ent ? # inc lude < std io .h > main ( ) { in t age ; char g ender ; pr int f ( “ Enter th e c i t i z ens a ge and g ender “ ) ; scan f ( “ %d %c “ , &a ge , &gend er ) ; i f ( gend er = = ‘m’ || gen der = = ‘ M’ ) { i f ( a ge > = 1 8 ) pr int f ( “ C i t i z en i s e l ig i b le f or vot ing “ ) ; e l se pr int f ( “ C i t i z en i s not e l i g ib l e f or vot ing “ ) ; } el se i f (g ender = = ‘ f ’ || gend er = = ‘ F ’ ) { i f ( a ge > = 2 1 ) pr int f ( “ C i t i z en i s e l ig i b le f or vot ing “ ) ; e l se pr int f ( “ C i t i z en i s not e l i g ib l e f or vot ing “ ) ; } e l se pr int f ( “ Y ou have ente red inc orr ect gend er “ ) ; } 8) Wr ite a C pr ogr am t o ca lcu lat e com m is s ion for the input va lue of sa le s am ount . Com m i ss ion is ca lcu lated a s per the fo l low ing r u les :
Com m is s ion is n i l for s a les am ount Rs 500 0/ .
Com m is s ion is 2% for sa le s when s a le s am o unt is gr e ater than 50 00 and le ss than equal to 1 00 00 .
Com m is s ion is 5% for s a les am ount gr ea ter than 100 00 . # inc lude < std io .h > main ( ) {
f loat sa l e s , co m m.; pr int f ( “Enter Sa l e s A m ount” ) ; scan f ( “ % f “ , &s a le s ) ; i f ( sa l e s > 10 00 0) com m = 5 ; e l se i f ( s a le s > 5 00 0 & & sa l e s < =1 00 00 ) com m = 2 ; e l se com m = 0 ; pr int f ( “ Th e c om mi s s i on perc entag e i s % f “ , com m ) ; pr int f ( “ Th e c om mi s s i on amount i s %f “ , co m m/ 100 * sa l e s ) ;
}
9) A char act er i s ente r ed thr ough keyboar d . Wr ite a C pr ogr am to deter m ine whether the char acter enter ed is a cap ita l le t ter , a sm a l l case let ter , a d i g i t or a spec ia l s ym bol us ing i f -e lse and sw itch case . The fo l lo win g tab le shows the r ange of AS CI I v a lue s for var i ou s char acter s . Char acter s AS CI I va lues A – Z 65 – 90 a – z 97 – 12 2 0 – 9 48 – 57 Spec i a l sym bo ls 0 – 47 , 5 8 – 64 , 91 – 96 , 1 23 - 1 27 inc lude < std io .h> main ( ) {
char ch ; pr int f ( “Enter any char acter ” ) ; scan f ( “ %c “ , &ch ) ; i f ( ch > = 6 5 && ch < = 9 0 ) pr int f ( “ I t i s an uppe rc ase a lphabe t “ ) ; e l se i f ( ch >= 97 && ch <= 12 2 ) pr int f ( “ I t i s an lo w erc ase a lphabe t “ ) ; e l se i f ( ch >= 48 && ch <= 57 ) pr int f ( “ I t i s a d i g i t “ ) ; e l se i f ( ( ch > = 0 && ch <= 47 ) | | ( ch > = 5 8 & & ch < = 6 4 ) ||
( ch > = 91 && ch < = 9 6 ) | | ( ch > = 1 23 && ch < = 1 27 ) ) pr int f ( “ I t i s a spe c ia l symbo l “ ) ;
}
10) I f co st pr ice and se l l ing pr ice of an i t em S input thr ough the ke y boar d , wr i te a pr ogr am to deter m ine whether the se l l er has m ade pr of i t or incur r ed loss . Wr ite a C pr ogr am to deter m ine how m uch pr of i t or lo ss incur r ed in per centage. inc lude < std io .h> main ( ) {
f loat cp , sp , los s , p ro f i t ; pr int f ( “ Enter Co st Pr i ce and Se l l ing Pr i ce “ ) ; scan f ( “ % f % f “ , &cp , &sp ) ; i f ( cp < sp ) { pr int f ( “ S e l l er ha s ma de a pro f i t o f % f \n “ , sp – cp ) ; p rof i t = ( sp – cp ) / cp * 1 00 ; pr int f ( “ Th e pro f i t p er centa ge i s % f “ , p ro f i t ) ; } e l se i f ( cp > sp ) { pr int f ( “ S e l l er ha s ma de a lo s s o f % f \n “ , c p – sp ) ; los s = ( cp – sp ) / cp * 100 ;
pr int f ( “ Th e pro f i t p er centa ge i s % f “ , lo s s ) ; }
} 11) Wr ite a C pr ogr am t o pr oduce the fo l lo win g output?
1 3 5 7 9 1 1 13 1 5 1 7 19
# inc lude < std io .h > main ( ) { in t x , y , va l = 1 ;
for ( x = 1 ; x < = 4 ; x ++ ) { for ( y = 1 ; y < = x ; y+ + ) { pr int f ( “ %d \ t “ , va l ) ;
va l = va l + 2 ; } pr int f ( “ \n “ ) ; } } 12) Wr ite a C pr ogr am t o d isp l ay N
t h F ibonacc i num ber .
#inc lude < std io .h > main ( ) { in t N , a , b , c , x ; pr int f ( “ Enter va lue o f N “ ) ; scan f ( “ %d “ , &N ) ;
a = 0 ; b = 1 ; i f ( N = = 1 ) pr int f (“Th e %d th F ib o nacc i Nu mbe r i s %d “ , N , a ) ; e l se i f ( N = = 2 ) pr int f (“Th e %d th F ib o nacc i Nu mbe r i s %d “ , N , b ) ;
e l se {
for ( x = 3 ; x < = N ; x+ + ) { c = a + b ; a = b ; b = c ; } pr int f ( “ Th e Nth F ibon acc i N u mbe r i s %d “ , c ) ; } } 13) Wr ite a C pr ogr am t o pr int the num ber s in t r iangu lar for m . 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 # inc lude < std io .h > main ( ) { in t x , y ; for ( x = 1 ; x < = 5 ; x ++ ) { for ( y = 1 ; y < = x ; y+ + ) { pr int f ( “ %d \ t “ , y ) ; } pr int f ( “ \n “ ) ; } } 14) Wr ite a C pr ogr am to r ead in two num ber s , x and n , and then com pute the sum of th is geom etr ic pr ogr e ss ion 1+ x+ x
2+x
3+. . .x
n. For e xam ple : i f n i s 3 and x is 5 , then th e pr ogr am
com putes 1+ 5+ 25 +1 25 . Pr int x , n , the sum . P er for m er r or check ing. For exam pl e the for m ula does not m ake sense f or negat i ve Exponents – i f n i s le ss than 0 . H ave your pr ogr am pr int an er r or m essage i f n<0 , t hen go back and r ead i n the nest pa ir o f num ber s of w ithout com put ing the sum . Ar e any va lue s of x a l so i l leg a l ? I f so , test for them too. # inc lude < std io .h > # inc lude < math .h > main ( ) { in t x , n , a , su m ; up : pr int f ( “ Enter va lue s o f x and n “ ) ; scan f ( “ %d %d “ , &x , &n ) ; i f ( n < 0 )
{ pr int f ( “ n va lue canno t be ne gat i ve \n “ ) ; goto up ; } sum = 1 ; for ( a = 1 ; a < = n ; a ++ ) { sum = su m + po w ( x , a ) ; } pr int f ( “Th e sum i s %d “ , sum ) ; } 15) Wr ite a C pr ogr am f or swappin g of t wo g i v en num ber s
a ) us in g a t em por ar y v ar iab le & b ) without us ing t em por ar y var iab le .
a ) us in g a tem por ar y v ar iab le # inc lude < std io .h > main ( ) { in t x , y , t ; pr int f (“ Enter 2 numb e rs x and y “ ) ; scan f ( “ %d %d “ , &x , &y ) ; t = x ; x = y ; y = t ; pr int f ( “ x a nd y va lu es a f te r s wapp in g ar e %d and %d “ , x , y ) ; } b) without us ing t em por ar y var iab le # inc lude < std io .h > main ( ) { in t x , y , t ; p r in t f ( " Ent er 2 nu mb e rs x and y " ) ; scan f ( " %d %d " , &x , &y ) ; x = x + y ; y = x - y ; x = x - y ; p r in t f ( " x and y va lue s a f te r s wapp in g ar e %d and %d " , x , y ) ; }
16) Wr ite a C pr ogr am to pr int Ar m str ong num ber s between 1 to n wher e n va lue is enter ed by the user . Ar m str ong num ber is def ined as th e sum of cubes of ind i v idua l d i g i t s o f a num b er . e .g . 37 1 = 3
3 + 7
3 + 1
3.
# inc lude < std io .h > # inc lude < math .h > main ( ) { in t n , x , su m , t ; p r in t f ( " Ent er any nu m ber " ) ; scan f ( " %d " , &n ) ; x = n ; sum = 0 ;
wh i l e ( x ! = 0 ) { sum = su m + po w ( x % 10 , 3 ) ; x = x /10 ; } i f ( su m = = n ) p r in t f ( " %d i s an A rm s trong Numb er " , n ) ; e l se p r in t f ( " %d i s not an A rm stron g Numb er " , n ) ; } 17) Wr ite a C pr o gr a m to gener ate a l l p r i m e num ber s between 1 and n , wher e n va l ue is suppl ied b y the user . # inc lude < std io .h > main ( ) {
in t n , p , fact , q ; p r in t f ( " Ent er any nu m ber : " ) ; scan f ( " %d " , & n ) ;
for ( p = 1 ; p <= n ; p + + ) {
fact = 0 ; for ( q = 1 ; q <= p ; q + + ) {
i f ( p % q == 0 ) fact + + ;
} i f ( fac t = = 2 )
p r in t f ( " \n %d " , p ) ; }
} 18) Wr ite a C pr ogr a m to pr int f i r s t n l ine s of the Pasc a l ’s Tr ian gle . Pa sca l ’ s t r ian gl e i s a t r iangu lar ar r a y of the b inom ia l coe ff ic i ents . For exam p le when n = 4 , the Pasc a l t r ian gl e wi l l be as fo l lows; 1 1 1 1 2 1 1 3 3 1 # inc lude < std io .h > main ( ) { in t row , b , x , y , z ; b = 1 ; y = 0 ; p r in t f ( " \n Enter th e n umbe r o f r ow s for th e p asca l ' s t ian g l e: " ) ;
scan f ( " %d " , &ro w ) ; wh i l e ( y < ro w ) { for ( x = ( ro w -1) *3 - (y* 3) ; x > 0 ; x - - ) p r in t f ( " " ) ; for ( z = 0 ; z < = y ; z ++ ) { i f ( ( z == 0 ) || ( y = = 0 ) ) b = 1 ; e l se b = ( b * ( y -z +1 ) ) / z ; p r in t f ( " %6d " , b ) ; } p r in t f ( " \n " ) ; y = y + 1 ; } } 19) Wr ite a C pr ogr am t o pr int f i r s t n l ines o f F loyd ’s Tr ian g le . 1 2 3 4 5 6 7 8 9 10 11 1 2 1 3 14 1 5 #inc lude < std io .h > main ( ) { in t x , y , va l = 1 ; for ( x = 1 ; x < = 5 ; x ++ ) { for ( y = 1 ; y < = x ; y+ + ) { p r in t f ( " %d \ t " , va l ) ; va l = va l + 1 ; } p r in t f ( " \n " ) ; } } 20) Wr ite a C pr ogr am t o pr int the fo l low in g se r ies 1/1! + 2/ 2! + 3/ 3! + …… …… … # inc lude < std io .h > ma in ( ) { in t n , x ; f loat fact , su m; p r in t f ( " Ent er va lu e of n " ) ; scan f ( " %d " , &n ) ; sum = 0 ; fact = 1 ; for ( x = 1 ; x < = n ; x+ + ) { fact = f act * x ; sum = su m + x / fact ; } p r in t f ( " Th e su m i s %f " , su m) ;
}
Answers to PART – C (PROBLEM SOLVING AND CRITICAL THINKING QUESTIONS) f r om Tutor ia l Quest io n
Bank
1 . Code 1: for ( i = 0 , j = 0 , i = j ; i+ + , j++ ) pr int f (“%d” , i ) ; Code 2: for ( i = 0 , j = 0 , i = = j ; i ++ , j + +) pr int f (“%d” , i ) ; Ana lyz e the abo ve t wo code s and wr i t e th e out put with va l id j ust i f i cat ion? Ans : Syntax Er ror (d is cu s s about the gen era l fo rmat o f th e “ fo r” loop a nd ana lyze ho w the above cod e i s v io lat ing the synta x o f for loop )
2 . vo id main ( ) { in t i = 5 , su m = 0 ; for ( i ; i ; i +5 ) sum = su m + i ; p r in t f (" Sum = %d", su m) ; } Ana lyz e the abo ve cod e and pred ic t the output f ro m pr int f ( ) s tate m en t . Ans : Th er e wi l l b e no o utput d isp lay ed be cau s e the for loop wi l l run i n f in i t e ly and do not stop , so
p r in t f (" Sum = %d", su m );
wi l l ne ver g et ex ecut ed . (D i s cus s about th e g e nera l for mat o f the “ for ” loop and ana lyz e how in f in i t e loop s can be w r i t t en us ing for loo p . A l so , d i scu s s how for loop in ab o ve progra m i s an in f in i t e l oop )
3 . vo id ma in ( )
{
in t i= 5 , j= 10,k =1 ;
i f ( + + i || ++ j )
k= i + j ;
e l se
k= i - j ;
p r in t f (" %3d %3d %3d ", i , j , k ) ;
}
Ev alu ate the f ina l va lue of i , j , k f ro m th e above cod e ?
Ans : i = 6 j = 1 0 k = 16
(Wr i t e a d es cr ip t ion a n d t race ho w th e pro gra m g et s e xe cuted and ho w the s e va lu es o f i , j , k a re obta ined )
4 . for ( i = 1 ; i < 3 ; i + +)
{ for ( j = 1 ; j < 3 ; j ++ ) { for (k = 1 ; k < 3 ; k+ +) { i f ( j = = k ) b reak ; e l se {
p r in t f (" %d %d%d ", i , j , k ) ; cont inue ; } } } } Pr ed ict th e output o f t he abo ve code Ans :12 12 21 (Wr i t e a d es cr ip t ion a n d t race ho w th e pro gra m g et s e xe cuted )
5 . s witch (N % 6) { cas e 3 : pr in t f ( "W edne s day") ; defau l t : p r in t f (" Sunday ") ; cas e 5 :pr in t f ( "F r iday" ) ; } In the abo ve cod e i f N = 27, then pr ed ict th e ou tput o f the cod e? Ans : W edne sdaySunday Fr iday (Wr i t e a d es cr ip t ion a n d t race ho w th e pro gra m g et s e xe cuted )