Write a program that reads a single number and prints the triple of its value.
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:
$ ./triple1
2
6
$ ./triple1
123
369
The input contains a single line with one integer x where
0 ≤ x ≤ 100 000.
The output should contain a single integer y where y = 3x. The output should be followed by a line break.
2
6
123
369
triple
functionYour program should be implemented using a triple
function
that receives one integer as argument and returns an integer.
Please refer to the information for your chosen language:
int triple(int x);
triple :: Int -> Int
def triple(x):
int triple(int x);
public static int Triple(int x)
inside a class Program
public static int triple(int x)
inside a public class Triple
function triple(x)
function triple(x)
def triple(x)
(define (triple x) ...)
(define (triple x) ...)
The triple
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:
triple
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 a number:
or The triple is 33.
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:
$ ./triple1 <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:\> triple1.exe
12
36
Easier exercises: If you have difficulty with this exercise, try the hello and repeat1 exercises first.
Beware of the data types.
Users of Python and dinamically-typed languages beware.
The triple
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 triple(123)
but never as triple("123")
.
Step-by-step tutorial. Section 3.3. Basic functions and operators of the Computer Science by Example book has step-by-step information on how to solve this exercise.
try first: repeat1
try also: inc1
try next: add1
Copyright © 2020-2023 Rudy Matela
This text is available under the CC BY-SA 4.0 license.
Originally available on cscx.org/triple1