1 / 7

CS1010E TUTORIAL 11

CS1010E TUTORIAL 11. RECURSION P SANDEEP A13. Question 1(a). int exponent ( int num, int k) { //Base condition if(k==0) return 1; //Recursive condition else return ( num * exponent(num,k-1) ); }. Example. Let num=2;k=3 Answer:.

argus
Download Presentation

CS1010E TUTORIAL 11

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1010E TUTORIAL 11 RECURSION P SANDEEP A13

  2. Question 1(a) • int exponent (int num, int k) { //Base condition if(k==0) return 1; //Recursive condition else return ( num * exponent(num,k-1) ); }

  3. Example • Let num=2;k=3 • Answer: 2* exponent(2, 2) 2* exponent(2, 1) 2* exponent(2, 0) 2* 2*2 2* 2 2* 1 8

  4. Question 1(b) double exponent2 (int num, int k) { //Base condition double y; if(k==0) return 1; //Recursive condition else { y=exponent(num, k/2); // Function in 1(a) if(k%2==0) return y*y; else return (y*y*num); } }

  5. Example • Let num=2;k=3 • k is odd • Answer: • Let num=2;k=4 • k is even • Answer: exponent2(2, 3) exponent(2, 1) y=2 2* 2*2 8 exponent2(2, 4) exponent(2, 2) y=4 4*4 16

  6. Question 2 • intmul (inta,int b) • { • if (b==0) • return 0; • else • return (a + mul(a,b-1)); • }

  7. Example • Let a=2;b=3 • Answer: 2 + mul (2, 2) 2 + mul (2, 1) 2 + mul (2, 0) 2+2+2 2+2 2 6

More Related