C Language: Recursive small examples

Source: Internet
Author: User

Recursive definition:

a a process or function there is a way to invoke itself directly or indirectly in its definition or description.

Recursive conditions:

(1) recursion is the invocation of itself in a process or function ;

(2) When using a recursive strategy, there must be a definite recursive end condition called a recursive exit.

The use of recursion can simplify the program and reduce code writing, see the example below.

1. Obtain each digit of the decimal number and output it as a character.

#include <stdio.h>

#include <stdlib.h>

void binary_to_ascii (unsigned int value)

{

int num = value% 10; Modulo 10 can get every bit of decimal number

if (value> 9)//recursive end condition

Binary_to_ascii (VALUE/10); Remove the last one, recursively call itself

printf ("%c", num + ' 0 '); The corresponding character is obtained after adding the ASCII value of the character 0

}

int main ()

{

BINARY_TO_ASCII (1234);

System ("pause");

return 0;

}

2. Do not use temporary variables to get string lengths, using Recursive functions

#include <stdio.h>

int my_strlen1 (const char *STR)//How to use temporary variables

{

int count = 0;

while (*STR)

{

count++;

str++;

}

}

int My_strlen (char *str)//using Recursive functions

{

if (*str = = ')//Determine if the string is empty

return 0;

Else

Return 1 + my_strlen (str+1); Pointer to string plus 1 return value plus 1

}//NOTE: Str+1 here cannot be replaced with str++ or ++STR

int main ()//The former pointer will always point to the same character before it is combined

{//The latter, although it is possible to implement the pointer backwards by 1, but

Char *p = "Bit-tech"; has side effects, it changes the contents of the pointer

printf ("%d\n", My_strlen (p));

System ("pause");

return 0;

}

3. Write a recursive function digitsum (n), enter a non-negative integer, return the sum of the numbers that make up it, for example, call Digitsum (1729), then you should return 1+7+2+9, which is 19

#include <stdio.h>


int digitsum (int num) {

int sum = 0;

int tmp = num% 10;

if (num > 0)

sum = Tmp+digitsum (NUM/10);

return sum;

}

int main () {

int num = 0;

printf ("Please enter a non-negative integer \ n");

scanf_s ("%d", &num);

int ret = digitsum (num);

printf ("%d\n", ret);

return 0;

}

4. Write a function implementation n^k, using recursive implementation

#include <stdio.h>


int calculate (int num, int k) {

if (k = = 0)//index is 0

return 1;

The Else//index is not 0

Return num * calculate (num, k-1);//Calculate the power of K, recursively call k-1 times

}

int main () {

int num = 0;

int k = 0;

printf ("Please enter the number and power you want to calculate \ n");

scanf_s ("%d%d", &num, &k);

printf ("%d\n", Calculate (num, k));

return 0;

}

This article is from the "Molova" blog, make sure to keep this source http://molova.blog.51cto.com/10594266/1688240

C Language: Recursive small examples

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.