Previous Next
0 / 7
Module DEMO-32-58DEMO-C-32

Print Even Numbers

DEMO-C-32 • Programming in C

Browser-only practice

Problem Statement

Write a C program that reads a positive integer n from the user and prints all even numbers from 1 to n (inclusive), each on a new line.

An even number is any integer that is divisible by 2 with no remainder.

For example, if n is 10, the even numbers from 1 to 10 are: 2, 4, 6, 8, 10.

If n is less than 2, no even numbers should be printed since there are no even numbers in that range.

Constraints

1 ≤ n ≤ 1000 Only use basic C constructs (loops, conditionals, variables) Do not use arrays or functions other than those in stdio.h

Input Format

A single line containing a positive integer n (1 ≤ n ≤ 1000).

Output Format

Print each even number from 1 to n on a separate line. If there are no even numbers in the range, print nothing.

Sample Testcases

Submit runs every public testcase in this browser. Results and code never leave this device.

Sample #1
Public sample
10
2
4
6
8
10
Sample case showing even numbers from 1 to 10.
Sample #2
Public sample
15
2
4
6
8
10
12
14
Hidden case with odd n value to test boundary handling.
Sample #3
Public sample
1
Sample case with n=1, no even numbers to print.
Sample #4
Public sample
2
2
Hidden case with n=2, only one even number to print.
Sample #5
Public sample
20
2
4
6
8
10
12
14
16
18
20
Sample case with larger n value.
Sample #6
Public sample
4
2
4
Sample case with small even n value.

Web terminal

C, C++, Java, and Python run locally in a browser VM. No worker or visualizer is used.

Saved in this browser
Editor settings