🔢 Day 1: Mastering Data Types and Input/Output in C# – A HackerRank Practice Challenge

If you're just getting started with C#, this HackerRank challenge is an ideal warm-up. It introduces key concepts like data types, console input/output, and string operations — all foundational for any aspiring C# developer.
Let’s break down the challenge and explore why it matters — with improved depth, real-world considerations, and a quick exercise at the end.
📌 Objective
You're given three predefined variables:
int i = 4;
double d = 4.0;
string s = "HackerRank ";
Your task is to:
Declare three new variables: one int, one double, and one string.
Read user input for each variable.
Perform the following operations:
Add your int to i
Add your double to d (print with 1 decimal place)
Concatenate your string with s and print the result
📥 Sample Input
12
4.0
is the best place to learn and practice coding!
📤 Sample Output
16
8.0
HackerRank is the best place to learn and practice coding!
🧪 Step-by-Step Solution in C#
Here’s the full, working solution with inline comments and clarity:
using System;
class Solution {
static void Main(String[] args) {
// Predefined variables
int i = 4;
double d = 4.0;
string s = "HackerRank ";
// Declare second integer, double, and string variables
int i2;
double d2;
string s2;
// Read and convert inputs from string to appropriate types
i2 = Convert.ToInt32(Console.ReadLine()); // Converts string to int
d2 = Convert.ToDouble(Console.ReadLine()); // Converts string to double
s2 = Console.ReadLine(); // Reads string as-is
// Print the sum of integers
Console.WriteLine(i + i2);
// Print the sum of doubles, formatted to 1 decimal place
Console.WriteLine((d + d2).ToString("F1"));
// Concatenate and print strings
Console.WriteLine(s + s2);
}
}
❓ Why Type Conversion Is Necessary
All input from Console.ReadLine() is read as a string, even if the user types a number. So, to perform math, you must explicitly convert it.
// User enters: "12"
int num = Convert.ToInt32("12"); // Now it's usable in math
🛑 Note: If a user enters invalid data (e.g., "abc"), Convert.ToInt32() or Convert.ToDouble() will throw a runtime exception.
🔒 You can catch these using a try-catch block (more on that in the mini challenge!).
🔍 Key Learnings
✅ Declare and use different data types in C#
✅ Read input from the console using Console.ReadLine()
✅ Convert strings to numbers with Convert.ToInt32() and Convert.ToDouble()
✅ Format floating-point output using .ToString("F1")
✅ Concatenate strings using the + operator
💡 Bonus Tip
In coding platforms like HackerRank, always format double values to match the exact output format.
double result = 4.0 + 4.0;
Console.WriteLine(result.ToString("F1")); // Output: 8.0
This prevents test case mismatches like printing 8 instead of 8.0.
🧠 Mini Challenge for You
Try this quick variation on your own:
You're given:
int i = 7;
double d = 3.14;
string s = "Learning ";
🎯 Task:
Read input values from the user.
Add and concatenate as before.
Print the results using the same format.
✨ Bonus: Try wrapping your input in try-catch to handle invalid input gracefully.
Share your code or output in the comments below!
👨💻 Final Thoughts
This challenge might look basic, but it’s crucial. Reading input, converting types, and formatting output are skills you’ll use in nearly every program.
💪 Small, consistent practice like this builds the confidence needed to tackle bigger real-world projects.
✅ Did this help?
If so, leave a ❤️ or share your variation in the comments!
Let’s keep leveling up together — one problem at a time.
Subscribe to my newsletter
Read articles from Priya directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
