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

C Exercise 75

C Exercise 75

100 Classic C Language Examples 100 Classic C Language Examples

**Problem:** Input an integer, reverse it, and output the result.

**Analysis:** None.

Example - Using strlen()

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

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

//

#include

<

stdio.h

int

main

(

)

{

int

n

,

reversedNumber

=

0

,

remainder

;

printf

(

"

Enter an integer:

"

)

;

scanf

(

"

%d

"

, &

n

)

;

while

(

n

!=

0

)

{

remainder

=

n

%

10

;

reversedNumber

=

reversedNumber

10

remainder

;

n

/=

10

;

}

printf

(

"

The reversed integer: %d

"

,

reversedNumber

)

;

return

0

;

}

The above example produces the following output:

输入一个整数: 12345

反转后的整数: 54321

100 Classic C Language Examples 100 Classic C Language Examples