Kotlin program: Print 'Hello' and name on separate lines
Write a Kotlin program to print 'Hello' on screen and your name on a separate line.
Pre-Knowledge (Before you start!)
- Basic Kotlin Syntax: Familiarity with writing and running Kotlin programs.
- Kotlin Functions: Understanding the structure of the main() function, which serves as the entry point of a Kotlin program.
- Printing Output: Knowledge of the println() function to print text on the screen.
- Escape Sequences: Understanding how to use escape sequences like \n for creating a new line in output.
- String Literals: Ability to write string literals inside double quotes (" ") in Kotlin.
Hints (Try before looking at the solution!)
- Use the main() Function: Start by defining the main() function, which is the starting point of any Kotlin program.
- Print 'Hello': Use the println() function to print the word "Hello".
- Add Your Name on a New Line: Use the escape sequence \n within the string to move your name to the next line.
- Example: println("Hello\nYourName").
- Replace Placeholder: Replace "YourName" with your actual name in the code.
- Run the Program: Execute the program to see the output displayed on two separate lines.
- Common Errors to Avoid:
- Forgetting to include the escape sequence \n, which may result in both texts appearing on the same line.
- Misplacing quotes or parentheses, leading to syntax errors.
Sample Solution:
Kotlin Code:
fun main() {
println("Hello\nRenata Evangelina")
}
Sample Output:
Hello Renata Evangelina
Kotlin Editor:
Previous: Kotlin Basic Exercises Home.
Next: Find out your Kotlin version.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics