Question

# write a recursive racket function "sum-alternate" that takes a positive integer x as a parameter. The...

write a recursive racket function "sum-alternate" that takes a positive integer x as a parameter. The function should return the sum of all the integers x, x-2, x-4, x-6, etc. as long as the numbers are positive. For example, [sum-alternate 5] should evaluate to 5 + 3 + 1, and [sum-alternate 6] should evaluate to 6+4+2.

C++ Recursive Solution:

``````#include <iostream>
using namespace std;

//Recursive function

int sumAlternate(int n, int i){

//if we reach to zero simply return 2 as we have to reached to zero only if n
// is at 2. and on substracting 2 it will become zero.
if(n == 0) return 2;

// if n is less than 0 return zero as we donot want to add this value
if(n< 0) return 0;

//or simply do recursion sum with substracting n by n-2;
return n + sumAlternate(n-2*i, i+1);

}

//Main Method
int main() {

int n;
cout<<"Enter Number:";
//take input value n
cin>>n;

//call recursive sumAlternate method to generate sum_alternate.
//here 'i' have taken 1 as initial value so as to make formula
//and 'i' will increase by 1 continously
// x= x + x-(2*1) + x-(2*2) + x-(2*3) + .......
cout<<"Alternate Sum is: "<<sumAlternate(n, 1);
return 0;
}

``````

Output:1.

output 2.

#### Earn Coins

Coins can be redeemed for fabulous gifts.