Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Exercise 24

C Exercise 24

100 Classic C Language Examples 100 Classic C Language Examples

**Problem:** There is a fraction sequence: 2/1, 3/2, 5/3, 8/5, 13/8, 21/13... Find the sum of the first 20 terms of this sequence.

**Analysis:** Pay attention to the pattern of change in the numerators and denominators.

Program source code:

Example

// Created by www.runoob.com on 15/11/9.

// Copyright (c) 2015 Runoob Tutorial. All rights reserved.

//

#include

<

stdio.h

int

main

(

)

{

int

i

,

t

;

float

sum

=

0

;

float

a

=

2

,

b

=

1

;

for

(

i

=

1

;

i

<=

20

;

i

++

)

{

sum

=

sum

a

/

b

;

t

=

a

;

a

=

a

b

;

b

=

t

;

}

printf

(

"

%9.6f

\

n

"

,

sum

)

;

}

The output of the above example is:



32.660263

100 Classic C Language Examples 100 Classic C Language Examples