Complete C# Tutorial

C# Practice Exercises: Variables, Data Types & Type Conversion

Hey there! 👋 Ready to practice some C# basics? Today, we’re going to play around with variables, data types, and type conversion. These exercises are super simple, but they’ll help you understand how data works in C#.

Don’t worry if you’re new to coding. Just take your time and try solving each problem step by step. If you get stuck, go back to the hints. And most importantly, have fun while coding! 🚀

Let’s get started! 💻😃

1. Store and Print Your Age

Description:

Write a program to store your age in a variable and print it.

Hints:

  • Use an int to store whole numbers.
  • Use Console.WriteLine() to print.

2. Swap Two Numbers

Description:

Write a program that takes two numbers, swaps their values, and prints them.

Hints:

  • Use a temporary variable to swap values.
  • Use int for whole numbers.

3. Multiply Two Floating-Point Numbers

Description:

Write a program to take two decimal numbers (like 3.5 and 2.2), multiply them, and print the result.

Hints:

  • Use double for decimal numbers.
  • Use * for multiplication.

4. Convert Integer to String

Description:

Take an integer and convert it into a string, then print it.

Hints:

  • Use ToString() or Convert.ToString().
  • Make sure to store the result in a string variable.

5. Get First Character of a String

Description:

Write a program that takes a string and prints its first character.

Hints:

  • Strings are like an array of characters.
  • Use stringVariable[0] to get the first letter.

6. Find the Length of a String

Description:

Write a program that takes a string and prints the total number of characters in it.

Hints:

  • Use stringVariable.Length to get the length.

7. Convert a Double to an Integer

Description:

Write a program that takes a decimal number, converts it to an integer, and prints both.

Hints:

  • Use (int) for explicit conversion.
  • Use Math.Round() if you want proper rounding.

8. Create and Print an Array

Description:

Create an array of 3 fruits and print each one on a new line.

Hints:

  • Use string[] to create an array of words.
  • Use Console.WriteLine() inside a loop to print each item.

9. Compare Two Numbers

Description:

Write a program that takes two numbers and checks which one is larger.

Hints:

  • Use if-else to compare the numbers.
  • Use > and < operators for comparison.

10. Create a Simple Class and Object

Description:

Create a Person class with a name variable. Then, create an object of the class and assign a name to it. Finally, print the name.

Hints:

  • Use the class keyword to define a class.
  • Use new to create an object.
  • Access the variable using objectName.variableName.

Final Tip:

Try solving each one without looking up the answer right away. If you get stuck, think logically and break the problem into smaller steps. Keep practicing, and you’ll get better at C#! 💪🔥

Conclusion

And that’s it! 🎉 You just practiced some important C# concepts like storing values, converting data types, and working with classes and arrays.

By now, you should feel more comfortable with declaring variables, using different data types, and even handling conversions. If you found some exercises tricky, don’t worry! The best way to get better is to keep practicing and experiment with your own code.

Keep coding, stay curious, and don’t be afraid to make mistakes! That’s how we all learn. 💪🔥

Leave a Comment

Share this Doc

Programming Exercises

Or copy link