Insert the missing code in the following code fragment- This fragment.docx

R

Insert the missing code in the following code fragment. This fragment is intended to recursively compute xn, where x and n are both non-negative integers: public int power(int x, int n) { if (n == 0) { ____________________ } else { return x * power(x, n - 1); } } a) return 1; b) return x; c) return power(x, n - 1); d) return x * power(x, n - 1); Solution And: a) return 1; public int power(int x, int n){ // base case, zero power of any number is 1 if(n==0){ return 1; } else{ return x*pow(x, n-1); } } .

Insert the missing code in the following code fragment. This fragment is intended to recursively
compute xn, where x and n are both non-negative integers:
public int power(int x, int n)
{
if (n == 0)
{
____________________
} else
{
return x * power(x, n - 1);
} }
a) return 1;
b) return x;
c) return power(x, n - 1);
d) return x * power(x, n - 1);
Solution
And: a) return 1;
public int power(int x, int n){
// base case, zero power of any number is 1
if(n==0){
return 1;
}
else{
return x*pow(x, n-1);
}
}

Contenu connexe

Plus de rtodd972(20)

Dernier(20)

Drama KS5 BreakdownDrama KS5 Breakdown
Drama KS5 Breakdown
WestHatch50 vues
ACTIVITY BOOK key water sports.pptxACTIVITY BOOK key water sports.pptx
ACTIVITY BOOK key water sports.pptx
Mar Caston Palacio132 vues
Education and Diversity.pptxEducation and Diversity.pptx
Education and Diversity.pptx
DrHafizKosar56 vues
Scope of Biochemistry.pptxScope of Biochemistry.pptx
Scope of Biochemistry.pptx
shoba shoba110 vues
Structure and Functions of Cell.pdfStructure and Functions of Cell.pdf
Structure and Functions of Cell.pdf
Nithya Murugan142 vues
STYP infopack.pdfSTYP infopack.pdf
STYP infopack.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego143 vues
Narration  ppt.pptxNarration  ppt.pptx
Narration ppt.pptx
Tariq KHAN62 vues
Industry4wrd.pptxIndustry4wrd.pptx
Industry4wrd.pptx
BC Chew153 vues
ANATOMY AND PHYSIOLOGY UNIT 1 { PART-1}ANATOMY AND PHYSIOLOGY UNIT 1 { PART-1}
ANATOMY AND PHYSIOLOGY UNIT 1 { PART-1}
DR .PALLAVI PATHANIA156 vues
Nico Baumbach IMR Media ComponentNico Baumbach IMR Media Component
Nico Baumbach IMR Media Component
InMediaRes1186 vues
Azure DevOps Pipeline setup for Mule APIs #36Azure DevOps Pipeline setup for Mule APIs #36
Azure DevOps Pipeline setup for Mule APIs #36
MysoreMuleSoftMeetup75 vues
AI Tools for Business and StartupsAI Tools for Business and Startups
AI Tools for Business and Startups
Svetlin Nakov57 vues

Insert the missing code in the following code fragment- This fragment.docx

  • 1. Insert the missing code in the following code fragment. This fragment is intended to recursively compute xn, where x and n are both non-negative integers: public int power(int x, int n) { if (n == 0) { ____________________ } else { return x * power(x, n - 1); } } a) return 1; b) return x; c) return power(x, n - 1); d) return x * power(x, n - 1); Solution And: a) return 1; public int power(int x, int n){
  • 2. // base case, zero power of any number is 1 if(n==0){ return 1; } else{ return x*pow(x, n-1); } }