Write a program that reads a pair of numbers and prints its sum.
Your program should read from the standard input and print to the standard output. The standard input and output devices are usually the keyboard and screen of a command line session. Here are a few example sessions:
$ ./add1
3 7
10
$ ./add1
1234 4321
5555
The input contains a single line with two integers x and y where 0 ≤ x, y ≤ 100 000.
The output should contain a single integer z where z = x + y.
3 7
10
1234 4321
5555
add
functionFor a full score,
in addition to producing the correct output,
the program should be implemented using an add
function
that receives two integers as arguments and returns an integer.
Please refer to the information for the chosen language:
int add(int x, int y);
def add(x,y):
add :: Int -> Int -> Int
int add(int x, int y);
public static int Add(int x, int y)
inside a class Program
public static int add(int x, int y)
inside a public class Add
function add(x, y)
function add(x, y)
def add(x,y)
(define (add x y) ...)
(define (add x y) ...)
The add
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 are confused by the above, 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:
add
functionAutomated 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 two numbers:
or The sum is 10.
Instead, just print the resulting number
followed by a line break
as in the example output.
Exit immediately:
Your program should print the addition result then exit immediately.
Do not use system("pause")
, sleep(1)
or anything of sorts.
Redirecting input: On most systems (Windows / Linux / OS X), it is possible to redirect the standard input and output of your program to files, like so:
$ ./add1 <inputfile.txt >outputfile.txt
If you create a plain text file with the “example input”, the above command should produce a plain text file with the “example output”.
Windows users:
On Windows, you should not use ./
to run a program in the current directory,
do instead:
C:\> add1.exe
12 21
33
There is no need to check boundaries: It is out of scope of this exercise to check boundaries. The boundaries for x and y are given for information only: 0 ≤ x, y ≤ 100 000. This just means that to get a full score, it is enough to write a program that works under these conditions, e.g.: two “C-int” fields will be enough to hold the values. There’s no need to check that x and y are in this interval.
Easier exercises: If you have difficulty with this exercise, try the hello exercise first.
try next: mult1
Copyright © 2020-2021 Rudy Matela
This text is available under the CC BY-SA 4.0 license.
Originally available on cscx.org/add1