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

C Exercise 18

C Exercise 18

100 Classic C Language Examples 100 Classic C Language Examples

**Problem:** Find the value of s=a+aa+aaa+aaaa+aa...a, where a is a digit. For example, 2+22+222+2222+22222 (5 numbers added together in this case); how many numbers are added is controlled by the keyboard.

**Analysis:** The key is to compute the value of each term.

Example

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

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

//

#include

<

stdio.h

int

main

(

)

{

int

sum

=

0

;

// Variable to store the result

int

base

,

terms

;

// base is the value of a, terms is the value of n

int

temp

;

// Temporary variable used to compute the value of the current term

// Prompt the user to enter values

printf

(

"

Please enter integers a and n (for example, 2 and 5):

\

n

"

)

;

scanf

(

"

%d%d

"

, &

base

, &

terms

)

;

// Initialize temp to a

temp

=

base

;

// Compute a + aa + aaa + ... through a loop

while

(

terms

0

)

{

sum

+=

temp

;

// Accumulate the current term

base

*=

10

;

// base shifts left by one digit each time (e.g., 2 -> 20 -> 200)

temp

+=

base

;

// Update temp to become the next term

terms

--;

// Decrease the number of remaining terms

}

// Output result

printf

(

"

The result is: %d

\

n

"

,

sum

)

;

return

0

;

}

The output of the above example is:



请输入整数 a 和 n(例如 2 和 5):

2 5

计算结果为:24690

Implemented using recursion:

Example

#include

// Recursive function to compute the value of each term

int

calculate_term

(

int

count

,

int

base

)

;

int

main

(

)

{

   

int

count

;

 

// Total number of terms

   

int

base

;

 

// Initial value

   

int

sum

=

0

;

// Accumulate the result

   

// Prompt the user to enter the initial value and total number of terms

   

printf

(

"Please enter the initial value (a):"

)

;

   

scanf

(

"%d"

,

&

base

)

;

   

printf

(

"Please enter the number of terms (n):"

)

;

   

scanf

(

"%d"

,

&

count

)

;

   

// Validate the input

   

if

(

count

<=

0

||

base

<=

0

)

{

       

printf

(

"The initial value and number of terms must be positive integers!\n"

)

;

       

return

1

;

// Return an error status

   

}

   

// Accumulate each term through a loop

   

for

(

int

i

=

1

;

i

<=

count

;

i

++

)

{

        sum

+=

calculate_term

(

i

,

base

)

;

// Call the recursive function to compute the value of the i-th term

   

}

   

// Output the final result

   

printf

(

"The result is: %d\n"

,

sum

)

;

   

return

0

;

}

// Recursive function to compute the value of the current term

int

calculate_term

(

int

count

,

int

base

)

{

   

if

(

count

==

1

)

{

       

return

base

;

// Recursion termination condition: the first term directly returns base

   

}

else

{

       

// Recursive computation: the current term equals the previous term times 10 plus base

       

return

calculate_term

(

count

1

,

base

)

10

base

;

   

}

}

The output of the above example is:

请输入初始值 (a):2

请输入项数 (n):5

计算结果为:24690

100 Classic C Language Examples 100 Classic C Language Examples