index | submit | rank | book

inc – Increment

Write a program that reads several numbers and prints their increment, i.e. their value added to one.

2 plus 1 is 3

Input and Output

The input contains several lines, each with a single integer x where - 100 000 000 ≤ x ≤ 100 000 000.

The output should contain several lines each with a single corresponding integer y where y = x + 1.

The number x may be given in the input with leading zeroes. The number y should be given in the output without leading zeroes. Input is terminated by the end-of-file (EOF)

Example input 1


Example output 1


The inc function

Your program should be implemented using an inc function that receives one integer as argument and returns an integer. Please refer to the information for the chosen language:

The inc function should not print anything. It should just perform the computation and return an integer. The function and input/output processing must exist in the same program. Create a single submission with the function and main program. If you’re confused, try earning a partial score first.

Specifically for this exercise when using Python, JavaScript, Lua or Ruby, avoid using sys.exit(), process.exit(), os.exit() or exit, as your program is appended with some extra assertions in one of the test sets.


Submit your solution to be graded according to the following list:


If you do not know where to start, read the Computer Science by Example book. First, setup your environment then learn the programming basics. Here are some hints:

  1. Automated judge: Keep in mind that when your program is submitted it will not be run by a human but instead by an automated judge. Instructions should be followed exactly or the judge will not give you a full score.

    Your program should not print messages like Please type a number: or The increment is:. Instead, just print the result followed by a line break as in the example output.

  2. Produce output as you go: You do not need to accumulate input and then produce everything at the end. It is enough to produce output as you go. As soon as you read a number write its increment to standard output.

  3. Detecting the end of file. In this exercise, input is terminated by the end-of-file (EOF). Here are ways to detect EOF in C, Python and Haskell:

    • In C. The scanf function returns the numbers of items read from stdin. Since this exercise requires you to read a number each line, you can compare scanf’s result to one as a while condition:

        while (scanf(...)==1) {

      Which translates to, “while you’re able to read one item from standard input, do …”

    • In Python. The pattern for line in sys.stdin: can be used to create a loop where a file is processed line by line until the end-of-file (EOF).

    • In Haskell. You can use interact to declare the main function and implement your solution as a function from String to String:

        io :: String -> String
        io = ...
        main :: IO
        main = interact io

      EOF is then represented as the nil list constructor ("" or []) at the end of the argument String.

    On the terminal, you can simulate the end-of-file (EOF) by holding “Ctrl” and pressing “D”, i.e., Ctrl-D.

  4. Beware of leading zeroes. C users should beware of leading zeroes. Use %d instead of %i to avoid treating numbers with leading zeroes as octals. Test your program with 010, 023 and 035. Does it print 11, 24 and 36?

  5. Beware of the data types. Users of Python and dinamically-typed languages beware. The inc function will be tested with proper integer values as its argument and not with integers encoded in strings. For example, it will be tested as inc(123) but never as inc("123").

  6. Further information. Sections 4.4. Functions and 4.5. Operators of the Computer Science by Example book

try first: repeat inc1

try also: triple

try next: add

index | submit | rank | book

Copyright © 2020-2023 Rudy Matela
All rights reserved