Using Recursion to Add Odd Numbers

Sum of odd numbers using recursion

Explanations in comment:

 int recursiveSumNOdd(int n) {
if (n == 1 || n == 0)// first "if" in a recursive is its stop condition
return n;
return 2 * n - 1 + recursiveSumNOdd(n-1); // formula for 2->3, 3->5 etc
}


int main(void) {
printf("%d\n", recursiveSumNOdd(3));
return 0;
}

NB: You may want to handle integer overflow

NB2: You can have a mathematics formula to return instantly the result, it is way better, but I guess it was to understand better recursion?

return n * n; // the sum of odd numbers is the square of user's input

Recursive algorithm for the sum of odd number positive integers

One small improvement that might help is defining it with tail recursion. Tail recursion happens when the very last thing to execute is the recursive call. To make this tail recursive, use a helper method and pass the running sum as a parameter. I'm pretty sure the pseudo code below is tail recursive since, regardless of the result of the (if odd) check, the final step is the recursive call (the math happens before the recursive call).

procedure SumOdds(n)
return SumOddsHelper(n, 0)

procedure SumOddsHelper(n, sum)
if n = 1 return 1

if n is odd return SumOddsHelper(n-1, sum + n)
else return SumOddsHelper(n-1, sum)

Recursive function that sums the odd digits of an integer

Integer divison by ten "cuts off" the last digit: I.e. 1234/10 results in 123.

Modulo 10 returns the last digit: i.e. 1234%10 results in 4.

Thus, the above code considers always the last digit. If the last digit is odd (hence the %2==1 stuff) it will be counted, otherwise not. So, if it should count the digit, it takes the last digit (the % 10-stuff) and continues computing with the remaining digits (the recursion with the /10-stuff) and adding them to the digit. If the current digit shall not be counted, it continues just with the remaining digits (thus the recursion and the /10-stuff) without adding it to the current digit.

If the argument is 0, this means that the whole number is traversed, thus the function terminates with returning 0.

recursive Function, to find even or odd digits inside given number

odd and even variables are local in your code, so they are initialized by zero every time.
I think they should be declared at caller of the recursive function, or be declared as global variables.

#include <stdio.h>
void digits(int n, int *even, int *odd)//function
{
int r;
r = n % 10;
if (r % 2 == 0)//check if given number is even
{
*even = *even + 1;
}
else //otherwise, its odd
{
*odd = *odd + 1;
}

n /= 10;
if (n != 0)
{
digits(n, even, odd);//supposed to reset function if n!=0 dividing it by 10
}
}

int main()
{
int n, even = 0, odd = 0;
printf("type number in:\n ");
scanf("%d", &n);

digits(n, &even, &odd);

printf("even: %d\n", even);
printf("odd: %d\n", odd);

return 0;
}

Recursive function that returns sum of all odd numbers within a range

Since B decreases in recursion, there's no need for the for-loop. Then to ensure the recursion ends, you need to specify what to return when B becomes equal to A.

def addOdds(A,B):
if A == B:
return 0
else:
if B % 2 == 0:
return addOdds(A, B-1)
else:
return B + addOdds(A, B-1)

print(addOdds(2, 125))
# 3968

Recursive methode which sums up odd numbers

To know if your solution is right, you need to run it and print the result.
To print, use System.out.println(..)

public static void main(String[] args) {

int s = sum(10);
System.out.println(s);
}

static int sum(int n)
{
if(n % 2 == 0) {
n--;
}
if (n <= 1)
{
return 0;
}
return n + sum(n-2);
}

execute it and see the result to see if it gives right output.



Related Topics



Leave a reply



Submit