PROBLEM: Write a recursive method named SumEvens that takes an integer X and returns the sum of even digits in X. Please comment on every line of code.
EXISTING CODE:
int numTwos(int n)
{
int right = n % 10;
int remain = n / 10;
if(n==0)
{
return 0 ;
}
else if(right ==2)
{
//rightmost digit is 2
return 1 + numTwos(remain);
else
{
return 0 + numTwos(remain);
}
}
int SumDigits(int X) {
if (X == 0) // base case
return 0;
else // recursive case
return X % 10 + SumDigits(X / 10); //make recursive call
}
int NumDigits(int X){
if(X<10){
return 1;
}
else{
return 1+NumDigits(X/10);
}
}
int LargestDigit(int X) {
if (X == 0) // base case
return 0;
else // recursive case
{
int d = LargestDigit(X / 10); //make recursive call
if (X%10 > d) {
d = X%10;
}
return d;
}
}
int LogY(int X, int Y) {
if (X <= 1) // base case
return 0;
else // recursive case
return 1 + LogY(X/Y, Y); //make recursive call
}
int Log2(int X) {
if (X <= 1) // base case
return 0;
else // recursive case
return 1 + Log2(X/2); //make recursive call
}
int Division(int X, int Y) {
if (X < Y) // base case
return 0;
else // recursive case
return 1 + Division(X-Y, Y); //make recursive call
}
int Exponentiation(int X, int Y) {
if (Y == 0) // base case
return 1;
else // recursive case
return X * Exponentiation(X, Y - 1); //make recursive call
}
int Multiply(int X, int Y){
if(Y == 0){
return 0;
}
else{
return X + Multiply(X, Y-1);
}
}
int MultiplyRange(int X, int Y){
if(Y == 0){
return 0;
}
else{
return X + MultiplyRange(X, Y-1);
}
}
int Subtraction(int X, int Y) {
if (Y == 0) // base case
return X;
else // recursive case
return Subtraction(X, Y - 1) - 1; //make recursive call
}
int Addition(int X, int Y) {
if (Y == 0) // base case
return X;
else // recursive case
return 1 + Addition(X, Y - 1); //make recursive call
}
int AddToN(int n) {
if (n == 0) { // base case
return 0;
} else { // recursive case
return n + AddToN(n-1); //make recursive call
}
}#include <iostream>
using namespace std;
int SumEvens(int X) {
if (X == 0) // base case
return 0;
else // recursive case
{
int sum = SumEvens(X / 10); //make recursive call
if ((X%10) % 2 == 0)
sum += X%10;
return sum;
}
}
int main() {
cout << SumEvens(81023) << endl;
cout << SumEvens(1923) << endl;
cout << SumEvens(247) << endl;
return 0;
}
PROBLEM: Write a recursive method named SumEvens that takes an integer X and returns the sum...
PROBLEM: Write a recursive method named Division that takes two integers X and Y returns the result of integer division (i.e., 8 / 3 = 2). Please comment on every line of code. EXISITNG CODE: int Exponentiation(int X, int Y) { if (Y == 0) // base case return 1; else // recursive case return X * Exponentiation(X, Y - 1); //make recursive call } int Multiply(int X, int Y){ if(Y == 0){ return 0; } else{ return X +...
PROBLEM: Write a recursive method named Addition that takes two integers X and Y returns their sum. Please comment on every line of code. EXISTING CODE: int AddToN(int n) { if (n == 0) { // base case return 0; } else { // recursive case return n + AddToN(n-1); //make recursive call } } void printStars (int n) { if (n==0 ) { //base case cout <<""; } else{ printStars(n-1); cout << ""; } } } int sumRange (...
PROBLEM: Write a recursive method named AddToN that adds up all the numbers between 1 and an integer N. Please add comments to every line of code. EXISTING CODE: void printStars (int n) { if (n==0 ) { //base case cout <<""; } else{ printStars(n-1); cout << ""; } } } int sumRange ( int min, int max) { if (min == max){ //base case return min; } else{ //recursive case return max + sumRange(min,max-1); } } } int sumDigits(int...
The following recursive method factRecursive computes the factorial of positive integer n. Demonstrate that this method is recursive. public static int factRecursive(int n) { int result = 0; if (n == 0) { result = 1; } else { result = n * factRecursive(n - 1); } return result; }
In Java Write a recursive method called sumUpto(n) which will return the sum of the first n integers. For example, sumUpto(4) will return 10 because 1+2+3+4 = 10. sumUpto(5) will return 15 because 1+2+3+4+5=15. So you can see that sumUpto(5) = sumUpto(4) + 5. Make this more general for n : sumUpto(n) = sumUpto(??) + n Figure out the base case and write the method……….. import java.util.Scanner; public class Lab12Num1 { public static int sumUpto(int num) { } public...
This code is based on creating a recursive funciton. You cannot alter the code besides adding your solution in the particular area. Write code to complete PrintFactorial()'s recursive case. Sample output if userVal is 5: 5! = 5 * 4 * 3 * 2 * 1 = 120 #include void PrintFactorial(int factCounter, int factValue){ int nextCounter = 0; int nextValue = 0; if (factCounter == 0) { // Base case: 0! = 1 printf("1\n"); }...
Recursive Tracing. For each call to the following method, indicate what value is returned: public static int mystery(int n) { if (n < 0) { return -mystery(-n); } else if (n == 0) { return 0; } else { return mystery(n / 10) * 10 + 9 - (n % 10); } Call Value Returned mystery(0) mystery(5) mystery(13) mystery(297) mystery(-3456) } Can any one help me with it?
X266: Recursion Programming Exercise: log For function log, write the missing base case condition and the recursive call This function computes the log of n to the base b.As an example: log 8 to the base 2 equals 3 since 8 = 2*2*2. We can find this by dividing 8 by 2 until we reach 1, and we count the number of divisions we make. You should assume that n is exactly b to some integer power. Examples: log(2, 4)...
C++ Recursion Practice! 1)Multiplication #include <iostream.h> int Multiply(int M, int N) //Performs multiplication using the + operator. //Pre : M and N are defined and N > 0. //Post: Returns M x N { int Prod; if (N == 1) Prod = M; //base case else Prod = M + Multiply(M, N - 1); //recursive step return Prod; } 2) Reverse #include <iostream.h> void Reverse(int N) //Displays string of length N in the reverse order //Pre : N...
Why might the following method have infinite recursion? public int infiniteRecursion(int n) { if (n > 0) { return infiniteRecursion(n) - 2; } else { return 0; } } Because the base case will never be true None of these are correct, there is no infinite recursion in this method Because there is no base case Because the recursive call does not move the parameter closer to the base case