On the HackerRank Coding environment, most of your programs require to read input and write the output using the Standard Input stream (STDIN) and the Standard Output stream (STDOUT) methods. You must use the language-specific input and output statements in your code to read and write the values, respectively.
Note: In a scenario where you are involved in writing a complete program to answer a coding Question, the information provided in this article helps you understand how to read input into your program and print the resulting output.
For example, if you are coding in C, you must use the scanf() statement to read input into your program and printf() to write the output.
In your Hacker Tests, you can access a ready-reckoner to know the programming language specific STDIN and STDOUT methods to use for reading input and writing the output from your code.
- In the Test, click the icon on the left, and select F.A.Q. Scroll down in the page to refer the input-output methods to be used for coding in different programming languages.
- Alternatively, in the code editor, access the section shown below for help, and select the Click here link.
This is the standard stream to provide or read input values to a program. For example, consider a HackerRank sample question to read two integers, say a and b, and return their sum as the output.
- If coding in Java, you must use the Scanner or BufferReader classes to read input values from Test Cases into the variables a and b.
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
- If coding in C, you must use the scanf() statement to read the integer value inputs to your program.
scanf("%d,%d", &num1, &num2)
STDOUT - This is the standard stream to write or print the output from your code. For example, for the above-mentioned coding question, you need to write the output from your program.
- If coding in Java, you must use the System.out.println() statement to print the output value.
- If coding in C, you must use the printf() statement to write the output value from your program.
You can access the HackerRank Sample Test to practice coding and familiarize yourself with the environment before taking up the actual Test.
In your HackerRank coding tests, to execute your code successfully and return the expected output, it is essential that you provide the expected input(s) to your program and print the output value(s) in the exact expected format. For detailed information, refer to the following topics: