Latest web development tutorials

C Exercise Example 28

100 cases of classic C language 100 cases of classic C language

Title: Five people sitting together, the fifth person to ask how old? He said that over the first four individuals 2 years older. Q. The first four individuals age, he said, is larger than the first three individuals 2 years of age. Asked a third person, said the NPC than the second two years. Q. The first two individuals, say two years older than the first man. Finally, ask the first person, he said, he is 10 years old. Will the fifth person how much?

Program analysis: recursive method, recursive and recursive pushed back into two stages.To know the fifth person age, the need to know the age of the fourth person, and so on, to push the first person (10 years old), Zaiwang Hui push. .

Source Code:

//  Created by www.w3big.com on 15/11/9.
//  Copyright © 2015年 本教程. All rights reserved.
//

#include <stdio.h>

int age(n)
int n;
{
    int c;
    if(n==1) c=10;
    else c=age(n-1)+2;
    return(c);
}
int main()
{
    printf("%d\n",age(5));
}

The above example output is:

18

100 cases of classic C language 100 cases of classic C language