Sie sind auf Seite 1von 24

chapter 5 part 1]let us c [SOLVED]

[D] Answer the following: (a)Write a function to calculate the factorial value of any integer entered through the keyboard.
hide Answer:#include ain() !

int i" f# int factorial()# $rintf(%&nter the nu scanf (%'d%" (i)# f)factorial(i)# $rintf(%'d* ) 'd+n%" i" f)# , factorial(int nu ! int te $" fact# ) ber to evaluate its factorial:%)#

for (te $)-"fact)-#te return (fact)# ,

$.)nu

# te

$//) ! fact)fact0te

$# continue# ,

(b) Write a function $ower ( a" b )" to calculate the value of a raised to b
hide Answer : #include ain() !

int $ower (a"b)# int a" b" result# $rintf(%&nter the value of a and b:%)#

scanf (%'d 'd%" (a" (b)# result)$ower(a"b)# $rintf(%'d raised to 'd is 'd%" a" b" result)# , $ower (int a" int b) ! int calculation)-" calc# for (calc)-# calc .)b# calc//) ! calculation)calculation0a# continue# , return(calculation)# ,

(c) Write a general1$ur$ose function to convert any given year into its ro an e2uivalent. 3he following table shows the ro an e2uivalents of deci al nu bers: Deci al:........4o -.....................i 5....................v -6..................7 56..................l -66................c 566...............d -666............. an

&7a $le: 4o ane2uivalent of -899 is 4o an e2uivalent of -5:5 is


hide Answer:#include ain() !

dccccl777viii d77v

int year# int convert (int year)#

$rintf(%;ote:&nter a four year digit year.+n+n%)# $rintf(%&nter the year that you wanna convert to 4o scanf (%'d%" (year)# if (year< -888) ! $rintf(%=nvalid >ear.?lease enter again.+n+n%)# , , convert(year)# , an: % )#

convert(int year) ! int i# $rintf(%+n>ear converted to 4o an:%)#

i)(year@-666)# @@thousands $lace if(i))-) ! $rintf(% , %)#

i)((year@-66)'-6)# @@hundreds $lace switch (i) ! case -: $rintf(%c%)# break# case :: $rintf(%cc%)#

break# case A: $rintf(%ccc%)# break# case B: $rintf(%cd%)# break# case 5: $rintf(%d%)# break# case C: $rintf(%dc%)# break# case D: $rintf(%dcc%)# break# case 9: $rintf(%dccc%)# break# case 8: $rintf(%dcccc%)# @@this $art you ay think is wrong..8 1< c the e7a $le in the 2uestion.

break# @@but i have taken a hint fro ,

i)((year@-6)'-6)# @@tens $lace switch(i) ! case -:

$rintf(%7%)# break# case :: $rintf(%77%)# break# case A: $rintf(%777%)# break# case B: $rintf(%7l%)# break# case 5: $rintf(%l%)# break# case C: $rintf(%l7%)# break# case D: $rintf(%l77%)# break# case 9: $rintf(%l777%)# break# case 8: $rintf(%l7777%)# @@had it not been for this e7a break# , $le" it would have been 7c

i)year'-6# @@ones $lace switch(i) ! case -: $rintf(%i%)# break# case :: $rintf(%ii%)# break# case A: $rintf(%iii%)# break# case B: $rintf(%iv%)# break# case 5: $rintf(%v%)# break# case C: $rintf(%vi%)# break# case D: $rintf(%vii%)# break# case 9: $rintf(%viii%)# break# case 8: $rintf(%i7%)#

break# , $rintf (%+n+n%)# return 6# ,

(d) Any year is entered through the keyboard. Write a function to deter ine whether the year is a lea$ year or not.
hide Answer: #include ain() !

int lea$Eyear(year)# int year" l$# $rintf(%&nter the year:%)# scanf (%'d%" (year)# l$)lea$Eyear(year)# if (l$) ! $rintf(%+n3he entered year is a lea$ year.%)# , else ! $rintf(%+n3he entered year is not a lea$ year.%)# , , lea$Eyear(int y) ! int l$#

if (y'B))6) ! l$)-# , else l$)6# return(l$)# ,

(e) A $ositive integer is entered through the keyboard. Write a function to obtain the $ri e factors of this nu ber. For e7a $le" $ri e factors of :B are :" :" : and A" whereas $ri e factors of A5 are 5 and D.
hide Answer:#include ain() !

int nu ber# int $ri e(int nu int $ri

ber)# ber)# ber whose $ri e factors are to be calculated:%)#

efactor(int nu

$rintf(%&nter the nu scanf (%'d%" (nu $ri , efactor(nu

ber)# ber)#

@@3he following function detects a ?ri $ri e(int nu ) ! int i" if$ri e# for (i):# i.)nu ! if (nu 'i))6) ! 1-# i//)

e nu

ber.

if$ri e)6# , else if$ri e)-# , return (if$ri e)# , @@3he following function $rints the $ri e factors of a nu ber.

$ri efactor(int nu ) ! int factor"if$ri e# for (factor):# factor.)nu # ) ! $ri e(factor)# @@so that the factors are only $ri e and nothing else. if (if$ri e) ! if(nu 'factor))6) @@diving by all the $ri e nu bers less than the nu itself. ! $rintf(%'d %" factor)# nu )nu @factor# continue# , else ! factor//#@@this cannot be ade a $art of the for loo$ , , , return 6# ,

ber

/* j (k) Write a function to compute the greatest common divisor given by Euclid's algorithm, e emplified for ! " #$%&, ' " #(#) as follo*s+ #$%& / #(#) " # , #$%& , # * #(#) " -(#(#) / -(- " . , #(#) , . * -(- " #(/ -(- / #(/ " 0 , -(- , 0 * #(/ " -/ / -- " / , #(/ , / * -- " & 1hus, the greatest common divisor is --2 */ 3include4stdio2h5 3include4conio2h5 int func(int j,int k)6 int main() 7 int j,k,t,r,86 printf(9:nEnter t*o numbers+ 9)6 scanf(9;d;d9,<j,<k)6 8"func(j,k)6 printf(9:n 1he greatest common divisor among t*o numbers is ;d+ 9,8)6 getch()6 return &6 = func(int j,int k) 7 int r"#,m,n"&,t6 if(k5j) 7 t"j6 j"k6 k"t6 = if(j""k) 7 return j6 = *hile(#) 7 r"j/k6 m"j,(r*k)6 if(>(j;k)) n"k6 if(m""&) break6 j"k6 k"m6 n"m6 = return n6 =

/* understanding problem + 1he greatest no2 *hich can divide both value2 for eg + % #( the ans is %22as % and #( are divided by %2

*/ j (j) Write a function to compute the distance bet*een t*o points and use it to develop another function that *ill compute the area of the triangle *hose vertices are ?( #, y#), @( 0, y0), and A( -, y-)2 Bse these functions to develop a function *hich returns a value # if the point ( , y) lines inside the triangle ?@A, other*ise a value &2 */ 3include4stdio2h5 3include4conio2h5 3include4math2h5 float geodis(int #,int y#,int 0,int y0)6 int pcheck(int a,int b,int c,int d,int e,int f,int g,int h,float area)6 float areat(float a,float b,float c)6 int main() 7 int a,b,c,d,e,f,g,h,i"&6 float dis#,dis0,dis-,area6 printf(9:nCnput the - coordinates #,y#, 0,y0, -,y-+ 9)6 scanf(9;d;d;d;d;d;d9,<a,<b,<c,<d,<e,<f)6 //calculating distance bet*een points dis#"geodis(a,b,c,d)6 dis0"geodis(a,b,e,f)6 dis-"geodis(c,d,e,f)6 //calculating area by function area"areat(dis#,dis0,dis-)6 printf(9:n1he area of the triangle is ;f+ 9,area)6 printf(9:nEnter a point( ,y) to check if it lies in the triangle2 9)6 scanf(9;d;d9,<g,<h)6 i"pcheck(a,b,c,d,e,f,g,h,area)6 printf(9:n 1he value of i no* is ;d 9,i)6 if(i""#) printf(9:n 1he point lies inside the triangle+ 9)6 else printf(9:n 1he point lies outside the triangle+ 9)6 getch()6 return &6 = //function for finding distance bet*een points

float geodis(int #,int y#,int 0,int y0) 7 float distance, -,y-6 -" 0, #6 y-"y0,y#6 distance"sDrt(po*( -,0)Epo*(y-,0))6 return(distance)6 = //function for calculating area float areat(float a,float b,float c) 7 float area,s6 s"(aEbEc)/02&6 area"sDrt(s*(s,a)*(s,b)*(s,c))6 return(area)6 = //function for detecting *heather point is inside triangle pcheck(int a,int b,int c,int d,int e,int f,int g,int h,float area) 7 float area#,area0,area-6 float dis#,dis0,dis-,dis.,dis/,dis(6 int total6 dis#"geodis(a,b,e,f)6 dis0"geodis(c,d,e,f)6 dis-"geodis(a,b,c,d)6 dis."geodis(a,b,g,h)6 dis/"geodis(e,f,g,h)6 dis("geodis(c,d,g,h)6 area#"areat(dis.,dis-,dis()6 area0"areat(dis0,dis/,dis()6 area-"areat(dis#,dis/,dis.)6 printf(9:n:n ?rea in pcheck is ;f+ 9,area)6 printf(9:n ?rea #,0,- is ;f+ 9,area#Earea0Earea-)6 total"area#Earea0Earea-,area6 printf(9:n 1otal area is ;d9,total)6 if(total""&) return(#)6 else return(&)6 =

/* understanding problem +

*/

* j (i) Cf the lengths of the sides of a triangle are denoted by a, b, and c, then area of triangle is given by area"sDuareroot of(s(s,a)(s,b)(s,c) *here, F " ( a E b E c ) / 0 */ 3include4stdio2h5 3include4conio2h5 3include4math2h5 float areat(float a,float b,float c)6 int main() 7 float ,y,8,area6 printf(9:n Cnput the three sides of the triangle+ 9)6 scanf(9;f;f;f9,< ,<y,<8)6 area"areat( ,y,8)6 printf(9:n 1he area of the triangle is ;f+ 9,area)6 getch()6 return &6 = float areat(float a,float b,float c) 7 float s,area6 s"(aEbEc)/06 area"sDrt(s*(s,a)*(s,b)*(s,c))6 return(area)6 = /* understanding problem +

*/ /* j (h) Write a function to find the binary eDuivalent of a given decimal integer and display it2 */ 3include4stdio2h5 3include4conio2h5 3include4math2h5 int dec0bin(int n)6 int main() 7 int n6 printf(9Enter a decimal no to find binary eDuivalent+ 9)6 scanf(9;d9,<n)6 dec0bin(n)6 getch()6 return &6 = dec0bin(int n) 7

int bin6 printf(91he binary eDuivalent is from right to left+:n 9)6 *hile(n>"&) 7 bin"n;06 printf(9;d 9,bin)6 n"n/06 = = /* understanding problem +

*/ * j (g) Given three variables , y, 8 *rite a function to circularly shift their values to right2 Cn other *ords if " /, y " %, 8 " #& after circular shift y " /, 8 " %, "#& after circular shift y " /, 8 " % and " #&2 Aall the function *ith variables a, b, c to circularly shift values2 */ 3include4stdio2h5 3include4conio2h5 void fun(int ,int y,int 8)6 int main() 7 int ,y,86 printf(9Enter the values of , y, 8+ 9)6 scanf(9;d;d;d9,< ,<y,<8)6 fun( ,y,8)6 getch()6 return &6 = void fun(int ,int y, int 8) 7 int i,t6 for(i"&6i4"06iEE) 7 t"86 8"y6 y" 6 "t6 printf(9:n ?fte r right shifting values ;d time+ 9,iE#)6 printf(9:n ";d y";d 8";d9, ,y,8)6 = = /* understanding problem +

*/ /* j (f) Write a A function to evaluate the series sin( )" ,( H-/->)E( H///>),( H)/)>)E22222 to five significant digits2 */ 3include4stdio2h5 3include4conio2h5 float numerator(float #,int j)6 float denominator(int j)6 int main() 7 float n, ,a,b,sum"&6 int i,j6 printf(9Enter a number + 9)6 scanf(9;f9,< )6 for(i"#,j"#6j4"#$6j"jE0) 7 a"numerator( ,j)6 //for finding numerator value b"denominator(j)6 //for finding denominator value n"a/b6 //n takes float value if(i;0""&) 7 sum"sum,n6 = else 7 sum"sumEn6 = = printf(9 sin(;f) " ;f9, ,sum)6 getch()6 return &6 = float numerator(float #,int j) //function for finding numerator value 7 float k"#6 int m6 for(m"#6m4"j6mEE) k"k* #6 return(k)6 = float denominator(int j) //function for finding denominator value 7 int m6 float h"#26 for(m"#6m4"j6mEE) h"h*m6 return(h)6 =

/*

understanding problem +

*/ /* j (d) ? positive integer is entered through the keyboard, *rite a function to find the binary eDuivalent of this number using recursion2 */ 3include4stdio2h5 3include4conio2h5 3include4math2h5 int dec0bin(int n)6 int main() 7 int n6 printf(9Enter a decimal no to find binary eDuivalent+ 9)6 scanf(9;d9,<n)6 dec0bin(n)6 getch()6 return &6

= dec0bin(int n) 7 int bin6 printf(91he binary eDuivalent is from right to left+ :n 9)6 *hile(n>"&) 7 bin"n;06 printf(9;d 9,bin)6 n"n/06 = = /* understanding problem +

*/ /* j (a)? /,digit positive integer is entered through the keyboard, *rite a function to calculate sum of digits of the /,digit number+ (#) Without using recursion (0) Bsing recursion*/ 3include4stdio2h5 3include4conio2h5 int calc(int num)6 int sum(int n)6 int main() 7 int num,total,recItotal6 printf(9Enter /,digit positive integer + :n9)6

scanf(9;d9,<num)6 total"calc(num)6 printf(9:n:n using recursion sum is+ 9)6 recItotal"sum(num)6 printf(9;d9,recItotal)6 getch()6 return &6 = int calc (int num) 7 int total,ne t,last6 last " num;#&6 total " last6 ne t " (num/#&);#&6 total " totalEne t6 ne t " (num/#&&);#&6 total " totalEne t6 ne t " (num/#&&&);#&6 total " totalEne t6 ne t " (num/#&&&&);#&6 total " totalEne t6 printf(9Fum of digit is+ ;d 9,total)6 = int sum(int n) 7 if(n""&) return &6 else return(n;#&Esum(n/#&))6 = /*

*/ * f (c)Write a function that receives marks received by a student in subjects and returns the average and percentage of these marks2 Aall this function from main( ) and print the results in main( )2*/ 3include4stdio2h5 3include4conio2h5 int calc(int i,int j,int k,int full,float *avg,float *per)6 int main() 7 int a,b,c,full6 float avg,per6 printf(9:nEnter the marks received by the student in - subjects :n and the : total possible marks for # paper+ :n9)6

scanf(9;d;d;d;d9,<a,<b,<c,<full)6 calc(a,b,c,full,<avg,<per)6 printf(9:n1he average marks is ;f2:n1he percentage is ;f9,avg,per)6 getch()6 return &6 = int calc(int i,int j,int k,int full,float *avg,float *per) 7 *avg"(iEjEk)/-2&6 *per"(*avg/full)*#&&2&6 = /*

*/ /* f (b)Write a function that receives / integers and returns the sum, average and standard deviation of these numbers2 Aall this function from main( ) and print the results in main( )2*/ 3include4stdio2h5 3include4conio2h5 int calc (float a, float b, float c, float d, float e, float *sum, float *avg,float *sd)6 int main() 7 float a, b, c, d, e, sum"&2&, avg"&2&6 float sd"&2&6 printf(9Enter Jive Kumbers to find sum average and standard deviation+ 9)6 scanf(9;f ;f ;f ;f ;f9,<a,<b,<c,<d,<e)6 calc (a, b, c, d, e, <sum, <avg, <sd)6//here address of sum //avg and sd is sent printf(9:nFum " ;f 9, sum)6 printf(9:n?verage " ;f 9, avg)6 printf(9:nFtandard Leviation(sd) " ;f :n9, sd)6 getch()6 return &6 = int calc (float a, float b, float c, float d, float e, float *sum, float *avg, float *sd) 7 float Aalc"&2&6 *sum " aEbEcEdEe6 *avg " *sum / /2&6 Aalc " Aalc E ( a , *avg) * ( a , *avg)6 Aalc " Aalc E( c , *avg) * ( c , *avg)6 Aalc " Aalc E ( d , *avg) * ( d , *avg)6 Aalc " Aalc E ( e , *avg) * ( e , *avg)6 *sd " sDrt((double)Aalc//2&)6 = /*

*/ /* f (a) Write a function *hich receives a float and an int from main(), finds the product of these t*o and returns the product *hich is printed through main()2*/ 3include4stdio2h5 3include4conio2h5 float product(int ,float y)6 int main() 7 int i6 float j, prod6 printf(9Enter the no in int and float +9)6 scanf (9;d ;f9, <i, <j)6 prod " product(i,j)6 printf(9Mroduct is + ;f9, prod)6 getch()6 return &6 = float product (int 7 float product6 product " *y6 return (product)6 = /*

, float y)

*/ /* . (e) ? positive integer is entered through the keyboard2 Write a function to obtain the prime factors of this number2*/ 3include4stdio2h5 3include4conio2h5 void factor(int n)6 int main() 7 int num6 printf(9Enter a num+ 9)6 scanf(9;d9,<num)6 printf(9:t:tprime factors are+ 9)6 factor(num)6 getch()6 return &6 = void factor(int n) 7 static int i"06//last value of i *ill be used for every function repeated2

if (i4"n) 7 if (n;i""&) 7 printf(9 ;d, 9,i)6 n"n/i6 = else iEE6 factor(n)6 = return6 = /*

*/ * . (d) ?ny year is entered through the keyboard2 Write a function to determine *hether the year is a leap year or not2*/ 3include4stdio2h5 3include4conio2h5 int po*er(int a,int b)6 int main() 7 int year6 printf (9Enter the year to be checked for a Neap Oear+ 9)6 scanf (9;d9, <year)6 if (year;.""&) 7 printf (91he year is a Neap Oear2 :n9)6 = else 7 printf(91he year is not a Neap Oear2 :n9)6 = getch()6 return &6 = /* ? leap year is a year containing one additional day (or, in the case of lunisolar calendars, a month) in order to keep the calendar year synchroni8ed *ith the astronomical or seasonal year

*/ . (c) Write a general,purpose function to convert any given year into its roman eDuivalent2 1he follo*ing table sho*s the roman eDuivalents of decimal numbers+ E ample+ Poman eDuivalent of #$%% is mdccccl viii Poman eDuivalent

of #/0/ is md v */ 3include4stdio2h5 3include4conio2h5 int po*er(int a,int b)6 int main() 7 int year6 printf(9Enter the year in ?L to convert to roman+ 9)6 scanf(9;d9,<year)6 printf(91he eDuivalent year in roman is+ 9)6 roman(year)6 = roman(int a) 7 int ,p,D,r,s,t6 "a6 p" /#&&&6 //finding #st digit among . digit of year " ;#&&&6 D" /#&&6 //finding 0nd digit among . digit of year " ;#&&6 r" /#&6 //finding -rd digit among . digit of year s" ;#&6 for (6p5"#6p,,) printf (9:n m9)6 if (D5/) 7 t " D ,/6 printf(9d9)6 for (6t5"#6t,,) printf(9c9)6 = else if (D4/) 7 for (6D5"#6D,,) printf(9c9)6 = else printf (9d9)6 if (r5/) 7 t " r,/6 printf(9l9)6 for (6t5"#6t,,) printf(9 9)6 = else if (r4/) 7 for(6r5"#6r,,) printf(9 9)6 = else printf (9l9)6 if (s5/) 7 t"s,/6 printf(9v9)6 for(6t5"#6t,,)

printf(9i9)6 = else if (s4/) 7 for(6s5"#6s,,) printf(9i9)6 = else printf(9v9)6 getch()6 return &6 = /*

*/ * . (b) Write a function po*er ( a, b ), to calculate the value of a raised to b2 */ 3include4stdio2h5 3include4conio2h5 int po*er(int a,int b)6 int main() 7 int a,b,value6 printf(9enter the value of a and b+ 9)6 scanf(9;d;d9,<a,<b)6 value"po*er(a,b)6 printf(9the value of a " ;d raised to b " ;d is ;d9,a,b,value)6 getch()6 return &6 = int po*er(int a,int b) 7 int i,value6 value"a6 for(i"#6i4b6iEE) 7 value"value*a6 = return value6 = /* understanding problem + value of a is multiplied b times through loop inside function2 */ . (a) Write a function to calculate the factorial value of any integer entered through the keyboard2 */ 3include4stdio2h5 3include4conio2h5 int factorial(int n)6

int main() 7 int n,fact6 printf(9enter no2 to find factorial 9)6 scanf(9;d9,<n)6 fact"factorial(n)6 printf(9the factorial of ;d is ;d9,n,fact)6 getch()6 return &6 = int factorial(int no) 7 int fact6 fact"no6 if(no""&) 7 fact"#6 return(fact)6 = else 7 fact"fact*factorial(no,#)6 //recursive function22it goes on until no""& at last fact"#22 //it goes as fact*(fact,#)*(fact, 0)*2222# return fact6 = = /* understanding problem + factoria of & is # so, if(no""&) fact"#6 for no -+ step#+ fact"(fact"-)*factorial(0) step0+ fact"(fact"-)*(fact"0)*factorial(#) step-+ fact"(fact"-)*(fact"0)*(fact"#)*factorial(&) since no""& fact "# returns so finally, fact"-*0*#*# *hich is results

*/

Das könnte Ihnen auch gefallen