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

C Language Examples - Printing Odd/Even Numbers in a Range

C Language Examples - Printing Odd/Even Numbers in a Range

C Language Examples C Language Examples

Printing odd/even numbers in a range in a loop can be determined by the remainder when divided by 2.

The following example prints the even numbers in a specified range using a loop.

Example

#include

<

stdio.h

int

main

(

)

{

int

i

;

for

(

i

=

1

;

i

<=

10

;

i

++

)

{

if

(

i

%

2

==

0

)

printf

(

"

%2d

\

n

"

,

i

)

;

}

return

0

;

}

Running result:



2

4

6

8

10

The following example prints the odd numbers in a specified range using a loop.

Example

#include

<

stdio.h

int

main

(

)

{

int

i

;

for

(

i

=

1

;

i

<=

10

;

i

++

)

{

if

(

i

%

2

!=

0

)

printf

(

"

%d

\

n

"

,

i

)

;

}

return

0

;

}

Running result:



1

3

5

7

9

C Language Examples C Language Examples